Data Structures · Pointers and Strings
String Comparison with strcmp and strncmp
Overview
strcmp is a standard string library function that compares two strings lexicographically, character by character, using ASCII value subtraction.
Its integer return value tells whether the first string is less than, equal to, or greater than the second, and the magnitude of that value is the ASCII difference of the first differing characters.
Key concepts
strcmp(s, t): A standard string library function that compares two strings. It takes twochar *arguments (the strings pointed to bysandt) and returns an integer indicating the lexicographic relationship between them.- Character-by-character comparison: At each position
i, the characterss[i]andt[i]are compared by computings[i] - t[i]from their ASCII values. - Return value meaning:
- Negative value →
sis less thant: the first differing character inshas a smaller ASCII value than the corresponding character int. 0→ both strings are equal.- Positive value →
sis greater thant: the first differing character inshas a larger ASCII value than the corresponding character int.
- Negative value →
- "s is less than t": While comparing, a different character was found at the same position in
sandt, and the character insis less than the character int. - First difference decides: The first differing character determines the result; characters after it are not examined.
How it works
- Take the character at position
ifromsand the character at positionifromt. - Compute
s[i] - t[i]using their ASCII values; the result is the difference of the two ASCII values. - If the difference is
0(characters are equal), move to the next position and repeat. - Continue comparing position by position as long as the difference is
0. - As soon as a position gives a non-zero difference,
strcmpreturns that difference value immediately. - If all characters are equal (both strings end together), it returns
0.
The null terminator is present at the end of each string; the comparison stops when a difference is found.
Formulas
- Comparison at each position:
diff = s[i] - t[i]— the ASCII value ofs[i]minus the ASCII value oft[i]. - Return value:
diffat the first position wherediff != 0; otherwise0if all characters match.
Worked examples
Let s point to the string a b c d and t point to the string a b c c.
| Position | s[i] | t[i] | s[i] - t[i] | Result |
|---|---|---|---|---|
| 0 | a | a | 0 | equal, continue |
| 1 | b | b | 0 | equal, continue |
| 2 | c | c | 0 | equal, continue |
| 3 | d | c | 1 (ASCII of d is one greater than c) | return 1 |
At position 3, d - c = 1 because the ASCII value of d is one greater than the ASCII value of c. Since the result is non-zero, strcmp returns 1 (a positive value), meaning s > t. The comparison stops here, before reaching the null terminator.
Revision summary
strcmp(s, t)compares two strings character by character using ASCII value subtractions[i] - t[i].- It returns the difference at the first mismatching position; it returns
0only if all characters are equal. - The return value is negative, zero, or positive: negative means
s < t, zero meanss == t, positive meanss > t. - The sign of the returned value (negative, zero, positive) is guaranteed; the magnitude is implementation-defined, though many implementations return the ASCII difference of the first differing characters.
- The first differing character decides the result; characters after it are not examined.
- The comparison stops as soon as a non-zero difference is found, so the null terminator may never be reached.
Previous-year exam insights
- strlen returns size_t, an unsigned type; in mixed expressions like strlen(s) - strlen(t), a negative difference wraps around to a huge positive unsigned value, so the comparison with an unsigned or signed constant can give an unexpected result.
Asked inGATE 2017 - strcmp and strncmp are declared in <string.h>; strncmp(s, t, n) compares at most n characters and stops at a null terminator or after n characters, so it can report equality for strings that differ only after the first n characters.
- strcmp compares the strings pointed to, not the pointer values;
comparing pointers with == or < compares addresses, not string contents. - The comparison is case-sensitive: uppercase letters have smaller ASCII values than lowercase letters, so 'Z' < 'a'.
- strcmp stops at the first null terminator, so characters stored after '\0' in the array are never compared.
- A common trap: strcmp returns 0 for equal strings, so 'if (strcmp(s,t))' is true when the strings are different, and 'if (!strcmp(s,t))' tests equality.