Skip to Main Content United States    
PRODUCTS SUPPORT SOLUTIONS SERVICES
COMPAQ SOFTWARE
Compaq C

Compaq C
Language Reference Manual


Previous Contents Index

Searching and Sorting Utilities


void *bsearch(const void *key, const void *base,   
  size_t nmemb, size_t size, int (*compar) 
  (const void *, const void *)); 

Searches an array of nmemb objects for an element that matches the object pointed to by key. The first element of the array is pointed to by base; the size of each element is specified by size.
You must first sort the array in ascending order according to the function pointed to by compar. The bsearch function calls the specified comparison function pointed to by compar with two arguments that point to the objects being compared (the key object and an array element). The comparison function returns:
  • An integer less than 0, if the first argument is less than the second argument
  • An integer greater than 0, if the first argument is greater than the second argument
  • An integer equal to 0, if the first argument equals the second argument

The bsearch function returns a pointer to the matching element of the array, or a null pointer if no match is found.


void qsort(void *base, size_t nmemb, 
size_t size, int (*compar) (const void *, 
const void *)); 

Sorts an array of nmemb objects in place. The first element of the array is pointed to by base; the size of each element is specified by size.
The contents of the array are sorted in ascending order according to a comparison function pointed to by compar , which is called with two arguments that point to the objects being compared. The comparison function returns:
  • An integer less than 0, if the first argument is less than the second argument
  • An integer greater than 0, if the first argument is greater than the second argument
  • An integer equal to 0, if the first argument equals the second argument

If two compared elements are equal, their order in the sorted array is unspecified.
The qsort function returns no value.

Integer Arithmetic Functions

int abs(int j);

Returns the absolute value of an integer j.

div_t div(int numer, int denom);

Computes the quotient and remainder of the division of numer by denom. The div function returns a structure of type div_t containing the quotient and remainder:


int quot;    /* quotient  */ 
int rem;     /* remainder */ 

long int labs(long int j);

Returns the absolute value of a long integer j.

ldiv_t ldiv(long int numer, long int denom);

Similar to the div function, except that the arguments and the members of the returned structure (which has type ldiv_t ) all have type long int .

Multibyte Character Functions

int mblen(const char *s, size_t n);

If s is not a null pointer, mblen determines the number of bytes comprising the multibyte character pointed to by s. The mblen function is equivalent to the following, except that the shift state of the mbtowc is not affected:


mbtowc((wchar_t *)0, s, n); 

If s is a null pointer, the mblen function returns a nonzero value if multibyte character encodings have state-dependent encodings, and 0 if they do not.
If s is not a null pointer, the mblen function returns one of the following values:

  • 0, if s points to the null character
  • The number of bytes that comprise the multibyte character, if the next n or fewer bytes form a valid multibyte character
  • --1, if they do not form a valid multibyte character

int mbtowc(wchar_t *pwc, const char *s, size_t n);

If s is not a null pointer, mbtowc determines the number of bytes comprising the multibyte character pointed to by s. It then determines the code for the value of type wchar_t that corresponds to that multibyte character. (The value of the code corresponding to the null character is 0.) If the multibyte character is valid and pwc is not a null pointer, mbtowc stores the code in the object pointed to by pwc. At most, n bytes of the array pointed to by s are examined.
If s is a null pointer, the mbtowc function returns a nonzero value if multibyte character encodings have state-dependent encodings, and 0 if they do not.
If s is not a null pointer, the mbtowc function returns one of the following values:
  • 0, if s points to the null character
  • The number of bytes that comprise the converted multibyte character, if the next n or fewer bytes form a valid multibyte character
  • --1, if they do not form a valid multibyte character

int wctomb(char *s, wchar_t wchar);

Determines the number of bytes needed to represent the multibyte character corresponding to the code whose value is wchar, including any change in shift state. This function then stores the multibyte character representation in the array object pointed to by s, if s is not a null pointer. At most, MB_CUR_MAX characters are stored. If the value of wchar is 0, the wctomb function is left in the initial shift state.
If s is a null pointer, the wctomb function returns a nonzero value if multibyte character encodings have state-dependent encodings, and 0 if they do not.
If s is not a null pointer, the wctomb function returns one of the following values:
  • --1, if the value of wchar does not correspond to a valid multibyte character
  • the number of bytes that comprise the multibyte character corresponding to the value of wchar

Multibyte String Functions

size_t mbstowcs(wchar_t *pwcs, const char *s, size_t n);

Converts a sequence of multibyte characters that begin in the initial shift state from the array pointed to by s into a sequence of corresponding codes, and stores not more than n codes into the array pointed to by pwcs. A null character is converted to a code value of zero. No multibyte characters that follow a null character are examined or converted. Each multibyte character is converted as if by a call to mbtowc , except that the shift state of mbtowc is not affected.
If an invalid multibyte character is encountered, the mbstowcs function returns (size_t) - 1 . Otherwise, it returns the number of array elements modified, not including a terminating zero code, if any.

size_t wcstombs(char *s, const wchar_t *pwcs, size_t n);

Converts a sequence of codes that correspond to multibyte characters from the array pointed to by pwcs into a sequence of multibyte characters that begins in the initial shift state, and stores these multibyte characters into the array pointed to by s. The conversion stops if a multibyte character would exceed the limit of n total bytes or if a null character is stored.
Each code is converted as if by a call to wctomb , except that the shift state of wctomb is not affected.
If a code is encountered that does not correspond to a valid multibyte character, the wcstombs function returns (size_t) - 1 . Otherwise, it returns the number of bytes modified, not including a terminating null character, if any.

9.15 String Processing (<string.h>)

The <string.h> header file declares one type and several functions, and defines one macro useful for manipulating character arrays that other objects treat as character arrays.

There are two kinds of string functions declared. The first, with names beginning with str , manipulate character arrays; the second, with names beginning with mem , manipulate other objects treated as character arrays. Except for memmove , function behavior is undefined if copying takes place between overlapping objects.

Type

size_t

An unsigned integral type of the result of the sizeof operator.

Macro

NULL

Expands to an implementation-defined null pointer constant.

Functions

void *memcpy(void *s1, const void *s2, size_t n);

Copies n characters from the object pointed to by s2 to the object pointed to by s1. The function returns s1.

void *memmove(void *s1, const void *s2, size_t n);

Copies n characters from the object pointed to by s2 to the object pointed to by s1. Copying takes place as if the n characters from the object pointed to by s2 are first copied into a temporary array of n characters that does not overlap the object pointed to by s1 and s2, and then the n characters from the temporary array are copied into the object pointed to by s1. The memmove function returns s1.

void *memchr(const void *s, int c, size_t n);

Locates the first occurrence of c (converted to an unsigned char ) in the first n unsigned characters of the object pointed to by s. The memchr function returns a pointer to the located character, or a null pointer if the character was not found.

int memcmp(const void *s1, const void *s2, size_t n);

Compares the first n characters of the object pointed to by s1 to the first n characters of the object pointed to by s2. The memcmp function returns an integer less than, equal to, or greater than 0, depending on whether the object pointed to by s1 is less than, equal to, or greater than the object pointed to by s2.

void *memset(void *s, int c, size_t n);

Copies the value of c (converted to an unsigned char) into each of the first n characters pointed to by s. The function returns s.

char *strcpy(char *s1, const char *s2);

Copies the string pointed to by s2 (including the terminating null character) to the string pointed to by s1. The strcpy function returns s1.

char *strncpy(char *s1, const char *s2, size_t n);

Copies no more than n characters from the string pointed to by s2 to the string pointed to by s1, up to but not including the null terminator of the string pointed to by s2; returns s1. If the string pointed to by s2 is less than n characters, strncpy pads the copy with null characters.

char *strcat(char *s1, const char *s2);

Appends a copy of the the string pointed to by s2 (including the terminating null character) to the end of the string pointed to by s1. The strcat function returns s1. The first character of s2 overwrites the null character of s1.

char *strncat(char *s1, const char *s2, size_t n);

Appends no more than n characters from the string pointed to by s2 (up to but not including a null character) to the string pointed to by s1. The strncat function returns s1. The first character of s2 overwrites the null character of s1. A terminating null character is appended to the result. The first character of s2 overwrites the null character of s1.

int strcmp(const char *s1, const char *s2);

Compares the string pointed to by s1 to the string pointed to by s2. The strcmp function returns an integer less than, equal to, or greater than 0, depending on whether the string pointed to by s1 is less than, equal to, or greater than the string pointed to by s2.

int strcoll(const char *s1, const char *s2);

Compares the string pointed to by s1 to the string pointed to by s2, both interpreted as appropriate to the LC_COLLATE category of the current locale (see Section 9.6). The strcoll function returns an integer less than, equal to, or greater than 0, depending on whether the string pointed to by s1 is less than, equal to, or greater than the string pointed to by s2, when both are interpreted as appropriate to the current locale.

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

Compares no more than n characters from the string pointed to by s1 to the string pointed to by s2. The strings are compared until a null character is encountered, the strings differ, or n is reached. The strncmp function returns an integer less than, equal to, or greater than 0, depending on whether the string pointed to by s1 is less than, equal to, or greater than the string pointed to by s2.

size_t strxfrm(char *s1, const char *s2, size_t n);

Transforms the string pointed to by s2 and places the resulting string into the array pointed to by s1.
See your Compaq C library routine documentation for a detailed description of this function.

char *strchr(const char *s, int c);

Locates the first occurrence of c (converted to a char ) in the string pointed to by s. The terminating null character is considered to be part of the string. The function returns a pointer to the located character, or a null pointer if the character was not found.

size_t strcspn(const char *s1, const char *s2);

Computes the length of the maximum initial segment of the string pointed to by s1 that consists entirely of characters not found in the string pointed to by s2. The strcspn function returns the length of the segment.

char *strpbrk(const char *s1, const char *s2);

Locates the first occurrence in the string pointed to by s1 of any character from the string pointed to by s2. The function returns a pointer to the character, or a null pointer if no character in s1 occurs in s2.

char *strrchr(const char *s, int c);

Locates the last occurrence of c (converted to a char ) in the string pointed to by s. The terminating null character is considered to be part of the string. The function returns a pointer to the located character, or a null pointer if the character was not found.

size_t strspn(const char *s1, const char *s2);

Computes the length of the maximum initial segment of the string pointed to by s1 that consists entirely of characters from the string pointed to by s2. The strspn function returns the length of the segment.

char *strstr(const char *s1, const char *s2);

Locates the first occurrence in the string pointed to by s1 of the sequence of characters (excluding the terminal null character) in the string pointed to by s2. The strstr function returns a pointer to the located string, or a null pointer if the string was not found. If s2 points to a string of zero length, the function returns s1.

char *strtok(const char *s1, char *s2);

Breaks the string pointed to by s1 into a sequence of tokens, each of which is delimited by a character from the string pointed to by s2. The first call to strtok () skips characters, looking for the first one that is not in s2. The function keeps track of its position in the string pointed to by s1 between calls and, as successive calls are made, the function works through this string, identifying the text token following the one identified by the previous call. When the function finds a character in s1 that matches a character in s2, it replaces the character in s1 with a null character. The strtok function returns a pointer to the first character of the token, or a null pointer if there is no token.

char *strerror(int errnum);

Maps the error number in errnum to an error message string; returns a pointer to the string. The string pointed to must not be modified by the program, but can be overwritten by a subsequent call to strerror .

size_t strlen(const char *s);

Computes the length of the string pointed to by s. The function returns the number of characters that precede the terminating null character.

9.16 Type-generic Math (<tgmath.h>)

The header <tgmath.h> includes the headers <math.h> and <complex.h> and defines several type-generic macros.

Of the <math.h> and <complex.h> functions without an f ( float ) or l ( long double ) suffix, several have one or more parameters whose corresponding real type is double . For each such function, except modf , there is a corresponding type-generic macro.1 The parameters whose corresponding real type is double in the function synopsis are generic parameters. Use of the macro invokes a function whose corresponding real type and type domain are determined by the arguments for the generic parameters.2

Note

The type-generic implementation of the absolute value function ( fabs ) is not available for complex types in this release. You must use the type-specific names ( cabs , cabsf , cabsl ) instead.

Note

1 Like other function-like macros in Standard libraries, each type-generic macro can be suppressed to make available the corresponding ordinary function.

2 If the type of the argument is not compatible with the type of the parameter for the selected function, the behavior is undefined.


Previous Next Contents Index
Buy Online or Call 1.800.888.0220      privacy statement and legal notices 
STORES CONTACT US SEARCH PRODUCTS SOLUTIONS OPTIONS DEVELOPERS