C Academy · Lesson

strcmp and Comparison

Comparing strings.

Lesson 2 of 413 steps

strcmp and Comparison is a free C Academy lesson on CoddyKit. This is lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C Academy learning path, and your progress syncs across the web and the CoddyKit app. The C Academy course includes 4 lessons in total.

Comparing strings

You cannot compare strings with == in C. That only compares the addresses of two arrays, not their contents.

Use strcmp(a, b) from <string.h> to compare character by character.

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

int main(void) {
    char a[] = "apple";
    char b[] = "apple";
    if (strcmp(a, b) == 0)
        printf("Strings are equal\n");
    return 0;
}

What strcmp returns

strcmp returns an int:

  • 0 if the strings are equal
  • a negative value if the first differing char in a is less than in b
  • a positive value otherwise
#include <stdio.h>
#include <string.h>

int main(void) {
    printf("%d\n", strcmp("abc", "abc"));
    printf("%d\n", strcmp("abc", "abd"));
    printf("%d\n", strcmp("abd", "abc"));
    return 0;
}

The zero-means-equal trap

A common bug: writing if (strcmp(a, b)) expecting it to mean equal.

Remember that strcmp returns 0 (falsy) when equal, so that condition is true when they differ. Always compare against 0 explicitly.

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

int main(void) {
    char input[] = "yes";
    if (strcmp(input, "yes") == 0)
        printf("Confirmed\n");
    else
        printf("Not confirmed\n");
    return 0;
}

How comparison works

strcmp compares the ASCII (byte) values of characters one at a time until they differ or a terminator is reached.

Uppercase letters (A=65) come before lowercase (a=97), so "Zebra" sorts before "apple".

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

int main(void) {
    printf("%d\n", strcmp("Zebra", "apple"));
    return 0;
}

Lexicographic ordering

The sign of strcmp lets you sort strings alphabetically. A negative result means the first argument comes earlier in dictionary order.

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

int main(void) {
    const char *x = "banana";
    const char *y = "cherry";
    if (strcmp(x, y) < 0)
        printf("%s comes before %s\n", x, y);
    return 0;
}

strncmp: limited comparison

strncmp(a, b, n) compares at most n characters. This is useful to check prefixes.

For example, testing whether a string starts with "http".

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

int main(void) {
    char url[] = "https://site.com";
    if (strncmp(url, "https", 5) == 0)
        printf("Secure URL\n");
    return 0;
}

Case-insensitive comparison

Standard strcmp is case-sensitive. To ignore case you can convert characters with tolower from <ctype.h> while comparing.

(POSIX systems also offer strcasecmp, but it is not in the C standard.)

#include <stdio.h>
#include <ctype.h>

int ci_cmp(const char *a, const char *b) {
    while (*a && (tolower(*a) == tolower(*b))) { a++; b++; }
    return tolower(*a) - tolower(*b);
}

int main(void) {
    printf("%d\n", ci_cmp("Hello", "hello"));
    return 0;
}

Searching with comparison

You can loop over an array of strings and use strcmp to find a match, like a simple lookup table.

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

int main(void) {
    const char *fruits[] = {"apple", "banana", "cherry"};
    const char *target = "banana";
    for (int i = 0; i < 3; i++) {
        if (strcmp(fruits[i], target) == 0) {
            printf("Found at index %d\n", i);
        }
    }
    return 0;
}

Sorting strings

Using strcmp as the engine, you can sort string arrays. Here is a tiny bubble sort that swaps pointers based on order.

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

int main(void) {
    const char *names[] = {"Carol", "Alice", "Bob"};
    int n = 3;
    for (int i = 0; i < n - 1; i++)
        for (int j = 0; j < n - 1 - i; j++)
            if (strcmp(names[j], names[j+1]) > 0) {
                const char *t = names[j];
                names[j] = names[j+1];
                names[j+1] = t;
            }
    for (int i = 0; i < n; i++) printf("%s\n", names[i]);
    return 0;
}

Don't compare with subtraction directly

You might try writing your own comparison with a[i] - b[i], but with raw char values, sign behavior is implementation-defined and can overflow.

Prefer the library strcmp, which handles these edge cases correctly.

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

int main(void) {
    int r = strcmp("file1", "file2");
    printf("Result sign: %s\n", r < 0 ? "negative" : "non-negative");
    return 0;
}

Comparing user input

A real-world use is checking a password or menu choice. Read input, then compare it against expected values with strcmp.

Always compare against 0 to test for equality.

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

int main(void) {
    char choice[] = "quit";
    if (strcmp(choice, "quit") == 0)
        printf("Exiting program\n");
    return 0;
}

Quick Check

Test your understanding of strcmp return values.

Recap

You learned how to compare strings in C:

  • strcmp returns 0 for equal, negative or positive for ordering.
  • Never use == on strings; it compares addresses.
  • Always test == 0 for equality (the zero-is-falsy trap).
  • strncmp compares a prefix; case-insensitive needs tolower.
Free to start

Learn C with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
39
Lessons
144

Frequently Asked Questions

Is the “strcmp and Comparison” lesson free?

Yes — the full text of “strcmp and Comparison” is free to read here on the web. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C Academy course, upgrade to CoddyKit PRO. The C Academy course includes 4 lessons in total.

What will I learn in “strcmp and Comparison”?

Comparing strings. You practise C Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start C Academy?

No prior experience is required. C Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 2 of 4.

How long does the “strcmp and Comparison” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this C Academy lesson?

Yes. Every C Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. strlen, strcpy, strcat
  2. strcmp and Comparison
  3. strtok and Tokenizing
  4. memcpy and memset
← Back to C Academy