Previous Table of Contents Next


memchr

void *memchr(const void *p, int c, size_t n)

memchr searches n characters in the block pointed to by p for the first character with the value c, and returns either a pointer to that character or a null pointer if it is not found.

memset

void *memset(void *p, int c, size_t n)

memset sets n bytes pointed to by p all to the character value c, and returns p.

3.10.2.6. Miscellaneous Operations

strtok

char *strtok(char *s, const char *sep)

A series of calls to strtok breaks up the string pointed to by s into a set of substrings, where substrings are delimited by any of the characters found in the string pointed to by sep. The first call takes the form

p = strtok(s, sep);

which picks off the first substring, terminates it by overwriting the first separator character with \0, remembers its position, and returns a pointer to the first substring. Succeeding calls take the form

p = strtok(NULL, sep);

where the null pointer argument indicates to strtok that it should resume scanning at the remembered position and break off, terminate, and return the next substring. A null pointer is returned when there are no more substrings.

For example, this fragment prints whitespace-separated substrings from a test string:

char teststr[] = “ a b cd\tefg”;
char *p;
for(p = strtok(teststr, “ \t”); p != NULL; p = strtok(NULL, “ \t”))
       printf(“%s\n”, p);

Because strtok necessarily uses some internal state, it is not possible to use it to break up a new string while another is still being processed.

strerror

char *strerror(int e)

strerror returns a human-readable error string corresponding to the error number e, which is typically a value deposited in the global errno after a system call or library function returns an error. See also section 3.10.13.

3.10.3. <ctype.h>

The functions declared in <ctype.h> allow testing for various classes of characters, and converting between upper- and lowercase characters.

3.10.3.1. Character Classification

int isupper(int c)
int islower(int c)
int isalpha(int c)
int isdigit(int c)
int isalnum(int c)
int isxdigit(int c)
int isspace(int c)
int ispunct(int c)
int isprint(int c)
int isgraph(int c)
int iscntrl(int c)

These functions all return nonzero if their argument character c meets some classification. isupper and islower test for upper- and lowercase alphabetic characters. isalpha tests for any alphabetic character; isdigit tests for any digit; isalnum tests for the union of the two. isxdigit tests for any hexadecimal digit (09 and af or AF). isspace tests for whitespace characters (space, \t, \n, \r, \f, or \v). ispunct tests for punctuation characters (neither alphanumeric nor whitespace). isprint tests for any printing character, including space; isgraph tests for any printing character with a graphical representation (i.e., all except space). iscntrl tests for nonprinting “control characters.”

In all cases, the value returned is 0 if the argument character does not meet the classification or nonzero (but not necessarily 1) if it does. Therefore, do not write if(isalpha(c) == 1) or if(isalpha(c) == TRUE), no matter how TRUE is defined. Simply write if(isalpha(c)) instead.

These functions all accept character values as if they were unsigned. The characters in C’s standard character set all have positive values, but the values in extended character sets may not. If a character c might have a negative value, it is important to call, for example, isalpha((unsigned char)c), so that a negative number will not be passed to isalpha on machines where characters are signed by default.

3.10.3.2. Character Conversion

int toupper(int c)
int tolower(int c)

toupper converts a lowercase letter to its uppercase equivalent and leaves any other character alone. Similarly, tolower converts an uppercase letter to lowercase.


Previous Table of Contents Next