1. Gyanastra
  2. Data Structures
  3. Pointers and Strings
  4. String Comparison with strcmp and strncmp
Free lesson, no account needed 4 min read 7 practice questions after it
Asked in past papers1 time
  • GATE CSE2017

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 two char * arguments (the strings pointed to by s and t) and returns an integer indicating the lexicographic relationship between them.
  • Character-by-character comparison: At each position i, the characters s[i] and t[i] are compared by computing s[i] - t[i] from their ASCII values.
  • Return value meaning:
    • Negative value → s is less than t: the first differing character in s has a smaller ASCII value than the corresponding character in t.
    • 0 → both strings are equal.
    • Positive value → s is greater than t: the first differing character in s has a larger ASCII value than the corresponding character in t.
  • "s is less than t": While comparing, a different character was found at the same position in s and t, and the character in s is less than the character in t.
  • First difference decides: The first differing character determines the result; characters after it are not examined.

How it works

  1. Take the character at position i from s and the character at position i from t.
  2. Compute s[i] - t[i] using their ASCII values; the result is the difference of the two ASCII values.
  3. If the difference is 0 (characters are equal), move to the next position and repeat.
  4. Continue comparing position by position as long as the difference is 0.
  5. As soon as a position gives a non-zero difference, strcmp returns that difference value immediately.
  6. 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 of s[i] minus the ASCII value of t[i].
  • Return value: diff at the first position where diff != 0; otherwise 0 if 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.

Positions[i]t[i]s[i] - t[i]Result
0aa0equal, continue
1bb0equal, continue
2cc0equal, continue
3dc1 (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 subtraction s[i] - t[i].
  • It returns the difference at the first mismatching position; it returns 0 only if all characters are equal.
  • The return value is negative, zero, or positive: negative means s < t, zero means s == t, positive means s > 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.
Pages 1–2 of 2