C strcasecmp() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
strings.h

Definition

In C, strcasecmp() compares two C-strings and ignores letter case. strcasecmp("Hi", "hi") is 0. It is common on POSIX systems and is not part of ISO C string.h.

You pass two C-strings. Both are required. The result uses the same 0 / less than 0 / greater than 0 idea as strcmp(). Include <strings.h> where it is available.

Syntax

Pass both strings. Start with this call.

C
strcasecmp("Hi", "hi");
CallWhat it does
strcasecmp("Hi", "hi")Returns 0 because the letters match when case is ignored.

This is a POSIX function. On Windows, people often use _stricmp() instead.

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

Parameters

ParameterDescription
s1Required. The first C-string.
s2Required. The second C-string.

Return Value

Return ValueDescription
0The strings match when letter case is ignored.
Less than 0The first string comes first.
Greater than 0The first string comes after.

C Examples

Example 1 — Check Equality Ignoring Case

Test whether two strings match when letter case is ignored. Prefer this over folding both strings by hand with tolower.

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

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

    if (strcasecmp(a, b) == 0) {
        printf("equal (ignore case)\n");
    } else {
        printf("different\n");
    }

    return 0;
}

How It Works

  1. Include strings.h. That is where POSIX declares strcasecmp.
  2. Compare with == 0. Zero means the strings match ignoring case.
  3. Case is folded. 'H' and 'h' count as the same letter.
  4. Do not use == 1. Only the sign of the return value is portable for ordering.

Example 2 — Match a User Command

Accept yes, YES, or Yes the same way. Ideal for prompts and simple CLI commands.

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

int main(void) {
    const char *answer = "YES";

    if (strcasecmp(answer, "yes") == 0) {
        printf("confirmed\n");
    } else {
        printf("not yes\n");
    }

    return 0;
}

How It Works

  1. User text varies. People type yes, Yes, or YES.
  2. One compare handles all. strcasecmp(answer, "yes") folds both sides.
  3. Branch on zero. A match prints confirmed.
  4. Keep secrets exact. Do not use case-insensitive compare for passwords.

Example 3 — Case-Insensitive Order

Use the sign of the result to see which string comes first when case is ignored—handy before sorting names.

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

int main(void) {
    int r = strcasecmp("apple", "Banana");

    if (r < 0) {
        printf("apple comes first\n");
    } else if (r > 0) {
        printf("Banana comes first\n");
    } else {
        printf("same (ignore case)\n");
    }

    return 0;
}

How It Works

  1. Fold then compare. 'a' vs 'b' (from Banana) decides the order.
  2. Negative means first. r < 0 means "apple" sorts before "Banana".
  3. Ignore the exact number. Portably check only < 0, == 0, or > 0.
  4. Contrast with strcmp. Case-sensitive strcmp would put "Banana" first because 'B' < 'a'.

Frequently Asked Questions

strcasecmp() compares two C-strings without caring about letter case. Matching letters return 0.
Both are required. s1 and s2 are the C-strings to compare.
0 when they match ignoring case. Otherwise the sign tells which comes first.
On POSIX systems, include <strings.h>. It is not in ISO C string.h.
No. strcmp() treats 'A' and 'a' as different. strcasecmp() ignores that case difference.

Next: stricmp()

Learn the Windows-style case-insensitive string compare.

stricmp() tutorial →

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.

9 people found this page helpful