Go to main content

man pages section 3: Basic Library Functions

Exit Print View

Updated: Wednesday, July 27, 2022
 
 

strcmp(3C)

Name

strcmp, strncmp, strcasecmp, strncasecmp, strcasecmp_l, strncasecmp_l - string comparison

Synopsis

#include <string.h>

int strcmp(const char *s1, const char *s2);
int strncmp(const char *s1, const char *s2, size_t n);
#include <strings.h>

int strcasecmp(const char *s1, const char *s2);
int strncasecmp(const char *s1, const char *s2, size_t n);
int strcasecmp_l(const char *s1, const char *s2, locale_t locale);
int strncasecmp_l(const char *s1, const char *s2, size_t n,
     locale_t locale);

Description

These functions compare two strings (arrays of characters), and return an integer greater than, equal to, or less than 0, if the string pointed to by s1 is greater than, equal to, or less than the string pointed to by s2 respectively. The strcmp(), strcasecmp(), and strcasecmp_l() functions require the strings to be terminated by a '\0' character. The strncmp(), strncasecmp(), and strncasecmp_l() functions limit their comparisons to no more than n bytes, allowing comparions of either an initial subset of the strings or of non-terminated strings.

strcmp(), strncmp()

The strcmp() function compares two strings byte-by-byte, according to the ordering of the "C" locale's character set. The function returns an integer greater than, equal to, or less than 0, if the string pointed to by s1 is greater than, equal to, or less than the string pointed to by s2 respectively. The sign of a non-zero return value is determined by the sign of the difference between the values of the first pair of bytes that differ in the strings being compared. The strncmp() function makes the same comparison but looks at a maximum of n bytes. Bytes following a null byte are not compared.

strcasecmp(), strncasecmp()

The strcasecmp() and strncasecmp() functions are case-insensitive versions of strcmp() and strncmp() respectively. They ignore differences in case when comparing lowercase and uppercase characters, using the current locale of the process to determine the case of the characters.

strcasecmp_l(), strncasecmp_l()

The strcasecmp_l() and strncasecmp_l() functions are versions of strcasecmp() and strncasecmp() respectively. They use the locale represented by locale, instead of the current locale of the process.

The behavior is undefined if the locale argument is the special locale object LC_GLOBAL_LOCALE or is not a valid locale object handle.

Attributes

See attributes(7) for descriptions of the following attributes:

ATTRIBUTE TYPE
ATTRIBUTE VALUE
Interface Stability
Committed
MT-Level
Async-Signal-Safe
Standard
See below

Standard

See standards(7) for descriptions of the following standards:

INTERFACES
APPLICABLE STANDARDS
strcmp(), strncmp()
  • C89 through C11,
  • POSIX.1-1990 through 2008,
  • SUS through SUSv4,
  • XPG1 through XPG7
strcasecmp(), strncasecmp()
  • POSIX.1-2001 through 2008,
  • SUSv3 through SUSv4,
  • XPG6 through XPG7
strcasecmp_l(), strncasecmp_l()
  • POSIX.1-2008,
  • SUSv4,
  • XPG7

See Also

memcmp(3C), newlocale(3C), setlocale(3C), strcoll(3C), string(3C), strxfrm(3C), u8_strcmp(3C), wcscmp(3C), wcscoll(3C), attributes(7), standards(7)

Notes

For some locales, strxfrm(3C) should be applied to the strings before they are passed to the functions.

History

The strcasecmp_l() and strncasecmp_l() functions were added to Oracle Solaris in the Solaris 11.4.0 release.

The strcmp(), strcasecmp(), strncasecmp(), strncmp() functions have been included in all Sun and Oracle releases of Solaris.