Skip to content
Tutorials Center
My learning

Topics

C Reference ยท string.h

C strcmp() and strncmp() functions

Reference for C strcmp() and strncmp(): compare strings lexicographically, interpret the sign of the result, and use them with qsort and prefix tests.

strcmp() compares two null-terminated strings character by character and returns a number that is negative, zero or positive. Zero means the strings are equal. strncmp() does the same but looks at no more than n characters, which makes it the usual way to test a prefix.

Never compare strings with ==. That compares the addresses of the first characters, not their contents.

C
#include <string.h>

int strcmp(const char *s1, const char *s2);
int strncmp(const char *s1, const char *s2, size_t n);

Try it

C Equality, order and prefixes

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

C Sorting strings with qsort

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Syntax

C
int strcmp(const char *s1, const char *s2);
int strncmp(const char *s1, const char *s2, size_t n);
Name Description
s1, s2 The strings to compare.
n (strncmp) The maximum number of characters to compare.

Returns: zero if the strings are equal, a negative value if s1 sorts before s2, and a positive value if it sorts after. The comparison treats characters as unsigned char.

Errors / undefined behavior: both arguments must point to terminated strings (for strncmp, at least n readable bytes unless a terminator comes first). A NULL argument is undefined.

Usage notes

  • Only the sign is specified. The magnitude of the result differs between implementations (some return the byte difference, others -1 and 1). Compare it with 0, < 0 or > 0, never with a specific number.
  • Order is by byte value, not by dictionary order or locale. In ASCII, uppercase letters sort before lowercase ones. For locale-aware ordering use strcoll.
  • Case-insensitive comparison: standard C has none. POSIX provides strcasecmp() and strncasecmp() in <strings.h>. Otherwise compare after converting with tolower.
  • A shorter string is smaller when it is a prefix of the other, because its terminator (value 0) is compared with a real character.
  • Prefix test: strncmp(s, "prefix", strlen("prefix")) == 0. The compiler folds the constant length.
  • Returning the result directly from a qsort comparator is fine, since only the sign matters. Do not subtract integers for numeric comparators, because it can overflow.
  • Not for binary data. Embedded null bytes end the comparison. Use memcmp with an explicit length.
  • Timing: strcmp stops at the first difference, so it leaks how many leading characters matched. Do not use it to compare secrets such as password hashes or tokens; use a constant-time comparison.