[ Next Article | Previous Article | Book Contents | Library Home | Legal | Search ]
Base Operating System and Extensions Technical Reference, 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.

Implementation Specifics

These subroutines are part of Base Operating System (BOS) Runtime.

Related Information

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

List of String Manipulation Services, National Language Support Overview for Programming, Understanding Multibyte and Wide Character String Collation Subroutines, Understanding Multibyte and Wide Character String Comparison Subroutines, Subroutines Overview in AIX Version 4.3 General Programming Concepts: Writing and Debugging Programs.


[ Next Article | Previous Article | Book Contents | Library Home | Legal | Search ]