C String strcasecmp() Function

Beginner
⏱️ 9 min read
📚 Updated: Jul 2026
🎯 5 Examples
<strings.h>

What You’ll Learn

The strcasecmp() function compares two C-strings without regard to letter case. "Hello" and "hello" match, which is ideal for user commands, yes/no prompts, and filename checks where capitalization should not matter.

01

Case-Insensitive

Ignore A vs a.

02

Like strcmp

Same return rules.

03

0 = Equal

Match ignoring case.

04

POSIX

<strings.h>

05

Windows

_stricmp()

06

vs strcmp()

Exact vs fold.

Definition and Usage

On POSIX systems (Linux, macOS, BSD), strcasecmp() is declared in <strings.h>. It walks both strings in parallel, comparing corresponding bytes while treating uppercase ASCII letters as equal to their lowercase forms.

The return value follows the same convention as strcmp(): zero means equal (ignoring case), a negative value means s1 sorts before s2, and a positive value means s1 sorts after s2. It is not part of ISO C—you will not find it in every embedded toolchain without an extension library.

💡
Beginner Tip

Test equality with strcasecmp(s1, s2) == 0. On Windows with Microsoft's compiler, the equivalent is _stricmp(s1, s2) == 0. Both strings must be valid null-terminated C-strings.

📝 Syntax

POSIX declaration:

C
int strcasecmp(const char *s1, const char *s2);

Headers and Portability

  • Linux / macOS / BSD#include <strings.h>
  • Windows (MSVC)#include <string.h> and use _stricmp(s1, s2)
  • Relatedstrncasecmp(s1, s2, n) compares at most n bytes

Return Value

  • 0 — strings equal ignoring case.
  • < 0s1 is less than s2 (case-insensitive order).
  • > 0s1 is greater than s2.

⚡ Quick Reference

s1s2strcasecmp result
"Hello, World!""hello, world!"0 (equal)
"Hello""hello"0 (equal)
"apple""Banana"< 0 (apple before Banana)
"yes""no"> 0
equality testany pairstrcasecmp(a, b) == 0
Equal?
strcasecmp(a, b) == 0

Ignore case

POSIX
#include <strings.h>

Not string.h

Windows
_stricmp(a, b)

MSVC equivalent

Exact match
strcmp(a, b)

Case-sensitive

Examples Gallery

Compile on Linux or macOS with gcc strcasecmp.c -std=c11 -o strcasecmp. Examples use POSIX <strings.h>; see the portability notes for Windows.

📚 Getting Started

Compare two strings and check whether they match ignoring case.

Example 1 — Case-Insensitive Equality

"Hello, World!" and "hello, world!" compare as equal.

C
#include <stdio.h>
#include <strings.h>

int main(void) {
    const char *str1 = "Hello, World!";
    const char *str2 = "hello, world!";

    int result = strcasecmp(str1, str2);

    if (result == 0) {
        printf("The strings are equal (ignoring case).\n");
    } else if (result < 0) {
        printf("str1 is less than str2.\n");
    } else {
        printf("str1 is greater than str2.\n");
    }

    return 0;
}

How It Works

Each character pair is compared with case folding for ASCII letters. Punctuation and spaces must still match exactly—only letter case is ignored.

Example 2 — Different Text Still Fails

Case insensitivity does not make different words equal.

C
#include <stdio.h>
#include <strings.h>

int main(void) {
    const char *a = "Hello";
    const char *b = "Help";

    if (strcasecmp(a, b) == 0) {
        printf("Match\n");
    } else {
        printf("No match (characters differ at 'l' vs 'p').\n");
    }

    return 0;
}

How It Works

After the matching "Hel", 'l' and 'p' differ even ignoring case, so the result is non-zero.

📈 Practical Patterns

User input, ordering, and comparison with strcmp().

Example 3 — Match a User Command

Accept quit, QUIT, or Quit equally.

C
#include <stdio.h>
#include <strings.h>

int main(void) {
    const char *input = "QuIt";

    if (strcasecmp(input, "quit") == 0) {
        printf("Goodbye!\n");
    } else if (strcasecmp(input, "help") == 0) {
        printf("Showing help...\n");
    } else {
        printf("Unknown command: %s\n", input);
    }

    return 0;
}

How It Works

Console programs often compare user text to fixed commands. Case-insensitive matching avoids frustrating “unknown command” errors when Caps Lock is on.

Example 4 — Case-Insensitive Sort Order

Use the sign of the return value for ordering, like strcmp.

C
#include <stdio.h>
#include <strings.h>

int main(void) {
    const char *words[] = { "apple", "Banana", "cherry" };
    int i, j;

    for (i = 0; i < 3; i++) {
        for (j = i + 1; j < 3; j++) {
            if (strcasecmp(words[i], words[j]) > 0) {
                const char *tmp = words[i];
                words[i] = words[j];
                words[j] = tmp;
            }
        }
    }

    for (i = 0; i < 3; i++) {
        printf("%s\n", words[i]);
    }

    return 0;
}

How It Works

A simple bubble sort swaps pointers when strcasecmp reports the first word is greater. "Banana" sorts after "apple" because case-insensitive comparison treats the leading letters as equal and continues until a difference appears.

Example 5 — strcasecmp() vs strcmp()

See how case sensitivity changes the result.

C
#include <stdio.h>
#include <string.h>
#include <strings.h>

int main(void) {
    const char *a = "OpenAI";
    const char *b = "openai";

    printf("strcmp:     %d\n", strcmp(a, b));
    printf("strcasecmp: %d\n", strcasecmp(a, b));

    return 0;
}

How It Works

strcmp sees 'O' (79) vs 'o' (111) and returns negative. strcasecmp folds case and reports equality. Use strcmp for passwords and exact tokens; use strcasecmp for human-entered text.

🚀 Common Use Cases

  • CLI commands — accept help, HELP, or Help.
  • Yes/No prompts — match y, Y, yes, YES.
  • Filename checks — compare names on case-insensitive file systems.
  • HTTP headers — header names are case-insensitive (often handled at a higher layer).
  • Sorting names — build case-insensitive indexes for display lists.

🧠 How strcasecmp() Works

1

You pass s1 and s2

Both must be null-terminated C-strings.

Input
2

Compare byte pairs

Letters are folded to the same case before comparing; other bytes match exactly.

Fold
3

Stop at difference or \0

Return 0 if both end together with all pairs matching.

Result
=

int result

0 for equal ignoring case; otherwise less or greater like strcmp.

📝 Notes

  • Include <strings.h> on POSIX—not the usual <string.h> alone.
  • Not ISO C standard; verify availability on your platform or provide a fallback.
  • ASCII-centric case rules—not full Unicode or locale-aware collation.
  • Do not pass NULL pointers—behavior is undefined.
  • For password comparison, prefer exact strcmp and constant-time helpers where security matters.

⚡ Optimization

Library implementations of strcasecmp() are tuned for typical string lengths. For hot paths comparing known short tokens, a hand-written loop may inline well, but for most programs calling the library function is clear and fast enough.

Conclusion

strcasecmp() is the POSIX way to compare strings without caring about letter case. Remember <strings.h>, test with == 0, and use strcmp() when exact bytes matter.

Practice the examples, then continue to strcat() for joining C-strings together.

💡 Best Practices

✅ Do

  • Use strcasecmp(a, b) == 0 for case-insensitive equality
  • Include <strings.h> on Linux and macOS
  • Use for user commands and human-entered text
  • Wrap with _stricmp on Windows for portability
  • Keep using strcmp for exact matching when required

❌ Don’t

  • Assume strcasecmp exists on every C compiler
  • Compare passwords case-insensitively
  • Expect locale-aware sorting for accented characters
  • Pass non-null-terminated buffers
  • Confuse with strncasecmp length limits

Key Takeaways

Knowledge Unlocked

Five things to remember about strcasecmp()

Use these points when comparing strings in C.

5
Core concepts
📝 02

0 = Match

Like strcmp.

Return
🔢 03

strings.h

POSIX header.

Include
📈 04

User Input

Commands, yes/no.

Use case
💬 05

vs strcmp

Exact bytes.

Compare

❓ Frequently Asked Questions

strcasecmp() compares two null-terminated C-strings without regard to letter case. It returns 0 when the strings match ignoring case, a negative value if s1 is less than s2, or a positive value if s1 is greater—using the same sign convention as strcmp().
On POSIX systems (Linux, macOS), include <strings.h>. It is not part of standard C or guaranteed in <string.h>. On Windows with MSVC, use _stricmp() from <string.h> instead.
strcmp() is case-sensitive: "Hello" and "hello" differ. strcasecmp() ignores case: those strings compare as equal. Use strcmp when exact bytes matter; use strcasecmp for user-facing text like commands or names.
0 if strings are equal ignoring case. Less than 0 if s1 would sort before s2. Greater than 0 if s1 would sort after s2. Only the sign is reliably portable for ordering—not the exact numeric value.
strcasecmp() itself is a POSIX function. Microsoft C provides _stricmp() (and stricmp as an older alias) for the same behavior. Check your compiler docs or use a portability wrapper in cross-platform code.
No. It compares bytes using a simple ASCII-style case fold for A–Z. It does not understand UTF-8 multibyte rules or locale-specific collation. For international text, use dedicated locale or Unicode libraries.
Did you know?

The BSD function was historically named strcasecmp to distinguish it from strcmp. GNU systems also provide strncasecmp() to cap how many bytes are compared—useful when matching prefixes like "HTTP/" without reading the entire header value first.

Continue the String Functions Series

Master case-insensitive comparison with strcasecmp(), then learn string concatenation with strcat().

Next: strcat() →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

6 people found this page helpful