strcmp, strncmp, strcasecmp, strcasecmp_l , strncasecmp, strncasecmp_l, strcoll, or strcoll_l Subroutine

Purpose

Compares strings in memory.

Library

Standard C Library (libc.a)

Syntax

#include <string.h>

int  strcmp ( String1,  String2) const char *String1, *String2;

int  strncmp (String1, String2,  Number) const char *String1, *String2; size_t Number;

int strcoll (String1, String2) const char *String1, *String2;

int strcoll_l (String1, String2,Locale) const char *String1, *String2;locale_t Locale;

#include <strings.h>

int  strcasecmp (String1, String2) const char *String1, *String2;

int  strcasecmp_l (String1, String2,Locale) const char *String1, *String2;locale_t Locale;

int  strncasecmp (String1, String2, Number) const char *String1, *String2; size_t Number;

int  strncasecmp_l (String1, String2,Number,Locale) const char *String1, *String2;size_t Number;locale_t Locale;

Description

The strcmp, strncmp, strcasecmp, strcasecmp_l, strncasecmp, strncasecmp_l, strcoll, and strcoll_l subroutines compare strings in memory.

The strcasecmp_l(), strncasecmp_l(), and strcol_ll() functions are the same as strcasecmp(), strncasecmp(), and strcoll() functions except that they use the locale represented by Locale to determine the case of the characters instead of the current locale.

The String1 and String2 parameters point to strings. A string is an array of characters terminated by a null character.

The strcmp subroutine performs a case-sensitive comparison of the string pointed to by the String1 parameter and the string pointed to by the String2 parameter, and analyzes the extended ASCII character set values of the characters in each string. The strcmp subroutine compares unsigned char data types. The strcmp subroutine then returns a value that is:

  • Less than 0 if the value of string String1 is lexicographically less than string String2.
  • Equal to 0 if the value of string String1 is lexicographically equal to string String2.
  • Greater than 0 if the value of string String1 is lexicographically greater than string String2.

The strncmp subroutine makes the same comparison as the strcmp subroutine, but compares up to the maximum number of pairs of bytes specified by the Number parameter.

The strcasecmp subroutine performs a character-by-character comparison similar to the strcmp subroutine. However, the strcasecmp subroutine is not case-sensitive. Uppercase and lowercase letters are mapped to the same character set value. The sum of the mapped character set values of each string is used to return a value that is:

  • Less than 0 if the value of string String1 is lexicographically less than string String2.
  • Equal to 0 if the value of string String1 is lexicographically equal to string String2.
  • Greater than 0 if the value of string String1 is lexicographically greater than string String2.

The strncasecmp subroutine makes the same comparison as the strcasecmp subroutine, but compares up to the maximum number of pairs of bytes specified by the Number parameter.

Note: Both the strcasecmp and strncasecmp subroutines only work with 7-bit ASCII characters.

The strcoll subroutine works the same as the strcmp subroutine, except that the comparison is based on a collating sequence determined by the LC_COLLATE category. If the strcmp subroutine is used on transformed strings, it returns the same result as the strcoll subroutine for the corresponding untransformed strings.

Parameters

Item Description
Number The number of bytes in a string to be examined.
String1 Points to a string which is compared.
String2 Points to a string which serves as the source for comparison.
Locale Points to the locale in which the strings are compared.

Error Codes

The strcmp, strncmp, strcasecmp, strncasecmp, strcoll, strcasecmp_l, strncasecmp_l, and strcoll_l subroutines fail if the following occurs:

Item Description
EFAULT A string parameter is an invalid address.

In addition, the strcoll, and strcoll_l subroutines fails if:

Item Description
EINVAL A string parameter contains characters outside the domain of the collating sequence.