[ Bottom of Page | Previous Page | Next Page | Contents | Index | Library Home | Legal | Search ]

Technical Reference: Base Operating System and Extensions, Volume 2

strcmp, strncmp, strcasecmp, strncasecmp, or strcoll 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;

#include <strings.h>

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

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

Description

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

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:

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:

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

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.

Error Codes

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

EFAULT A string parameter is an invalid address.

In addition, the strcoll subroutine fails if:

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

Related Information

The memccpy, memchr, memcmp, memcpy, or memmove subroutine, setlocale (setlocale Subroutine) subroutine, strcat, strncat, strxfrm, strcpy, strncpy, or strdup (strcat, strncat, strxfrm, strcpy, strncpy, or strdup Subroutine) subroutine, strlen, strchr, strrchr, strpbrk, strspn, strcspn, strstr, or strtok (strlen, strchr, strrchr, strpbrk, strspn, strcspn, strstr, strtok, or strsep Subroutine) subroutine, swab (swab Subroutine) subroutine.

List of String Manipulation Subroutines and Subroutines, Example Programs, and Libraries in AIX 5L Version 5.2 General Programming Concepts: Writing and Debugging Programs.

National Language Support Overview, Multibyte and Wide Character String Collation Subroutines, and Multibyte and Wide Character String Comparison Subroutines AIX 5L Version 5.2 National Language Support Guide and Reference

[ Top of Page | Previous Page | Next Page | Contents | Index | Library Home | Legal | Search ]