In C, strcmp() compares two C-strings character by character. Letter case matters, so "Cat" and "cat" are different.
You pass both strings. Both are required. The result is 0 when they match, less than 0 when the first comes first, or greater than 0 when the first comes after. Include <string.h>.
Syntax
Pass both strings. Start with this call.
C
strcmp("apple", "apricot");
Call
What it does
strcmp("apple", "apricot")
Compares the two words from left to right.
Include <string.h>. Do not use == on two char* values. That compares addresses, not text.
Library declarations
C
int strcmp(const char *s1, const char *s2);
Parameters
Parameter
Description
first
Required. The first C-string.
second
Required. The second C-string.
Return Value
Return Value
Description
0
The two strings are equal.
Less than 0
The first string comes before the second.
Greater than 0
The first string comes after the second.
C Examples
Example 1 — Check If Two Strings Match
Never compare C-strings with == on the pointers. Use strcmp and test for zero.