A structured lesson workspace with readable content, hands-on examples, and a clean path to completion.
You cannot compare C strings with ==. The == operator compares pointer values, not the characters they point to. Two different char arrays holding "hello" will have different addresses, so == returns false even though the text is identical. strcmp walks both strings character by character, comparing ASCII values, and returns an integer that tells you the lexicographic relationship. Negative means the first string sorts before the second. Zero means they are equal. Positive means the first sorts after.
#include <stdio.h>#include <string.h>int main(void) {char a[] = "hello";char b[] = "hello";printf("a == b: %d\n", a == b);printf("strcmp(a, b): %d\n", strcmp(a, b));if (strcmp(a, b) == 0) {printf("Strings are equal.\n");}return 0;}
a == b is 0 (false) because a and b are different arrays at different stack addresses. strcmp returns 0 because the character sequences are identical. The == compared addresses. strcmp compared content. This is the single most common string bug in beginner C code, and it appears in production systems more often than anyone wants to admit.
#include <stdio.h>#include <string.h>int main(void) {printf("strcmp(\"apple\", \"banana\"): %d\n", strcmp("apple", "banana"));printf("strcmp(\"banana\", \"apple\"): %d\n", strcmp("banana", "apple"));printf("strcmp(\"apple\", \"apple\"): %d\n", strcmp("apple", "apple"));printf("strcmp(\"app\", \"apple\"): %d\n", strcmp("app", "apple"));printf("strcmp(\"Apple\", \"apple\"): %d\n", strcmp("Apple", "apple"));return 0;}
The last line surprises people. 'A' is ASCII 65. 'a' is ASCII 97. The difference is -32. strcmp is case-sensitive and operates on raw byte values. There is no locale awareness. There is no Unicode handling. It compares bytes. If you need case-insensitive comparison, use strcasecmp on POSIX or _stricmp on Windows. Neither is part of the C standard.
#include <stdio.h>#include <stdlib.h>#include <string.h>int compare_strings(const void *a, const void *b) {return strcmp(*(const char **)a, *(const char **)b);}int main(void) {const char *fruits[] = {"banana", "apple", "cherry", "date"};int n = 4;qsort(fruits, n, sizeof(char *), compare_strings);for (int i = 0; i < n; i++) {printf("%s\n", fruits[i]);}return 0;}
The comparator receives pointers to the array elements. Since the array holds char* values, the elements are char**, and we dereference to get the char* that strcmp expects. This double-pointer dance is standard for qsort with string arrays. Get it wrong and you compare pointer addresses instead of string content, which gives you a sorted list of addresses rather than a sorted list of words.
strcmp is the foundation of string comparison in C. Every lookup table, every dictionary, every config parser uses it or a variant. Next: strpbrk, which searches for any character from a set, useful for finding delimiters and special characters.
Finish the lesson once you have worked through the material. This awards ★ 20 XP.