4.10 General Utilities  -< ANSI C Rationale  -> 4.12 Date and Time              Index 

4.11  String Handling  <string.h>

The Committee felt that the functions in this section were all excellent candidates for replacement by high-performance built-in operations.  Hence many simple functions have been retained, and several added, just to leave the door open for better implementations of these common operations. 

The Standard reserves function names beginning with str or mem for possible future use. 

4.11.1  String function conventions

memcpy, memset, memcmp, and memchr have been adopted from several existing implementations.  The general goal was to provide equivalent capabilities for three types of byte sequences:

4.11.2  Copying functions

A block copy routine should be ``right'': it should work correctly even if the blocks being copied overlap.  Otherwise it is more difficult to correctly code such overlapping copy operations, and portability suffers because the optimal C-coded algorithm on one machine may be horribly slow on another. 

A block copy routine should be ``fast'': it should be implementable as a few inline instructions which take maximum advantage of any block copy provisions of the hardware.  Checking for overlapping copies produces too much code for convenient inlining in many implementations.  The programmer knows in a great many cases that the two blocks cannot possibly overlap, so the space and time overhead are for naught. 

These arguments are contradictory but each is compelling.  Therefore the Standard mandates two block copy functions: memmove is required to work correctly even if the source and destination overlap, while memcpy can presume nonoverlapping operands and be optimized accordingly.  The memcpy function  The memmove function  The strcpy function  The strncpy function

strncpy was initially introduced into the C library to deal with fixed-length name fields in structures such as directory entries.  Such fields are not used in the same way as strings: the trailing null is unnecessary for a maximum-length field, and setting trailing bytes for shorter names to null assures efficient field-wise comparisons.  strncpy is not by origin a ``bounded strcpy,'' and the Committee has preferred to recognize existing practice rather than alter the function to better suit it to such use. 

4.11.3  Concatenation functions  The strcat function  The strncat function

Note that this function may add n+1 characters to the string. 

4.11.4  Comparison functions  The memcmp function

See §4.11.1  The strcmp function  The strcoll function

strcoll and strxfrm provide for locale-specific string sorting.  strcoll is intended for applications in which the number of comparisons is small; strxfrm is more appropriate when items are to be compared a number of times --- the cost of transformation is then only paid once.  The strncmp function  The strxfrm function

See §

4.11.5  Search functions  The memchr function

See §4.11.1  The strchr function  The strcspn function  The strpbrk function  The strrchr function  The strspn function  The strstr function

The strstr function is an invention of the Committee.  It is included as a hook for efficient substring algorithms, or for built-in substring instructions.  The strtok function

This function has been included to provide a convenient solution to many simple problems of lexical analysis, such as scanning command line arguments. 

4.11.6  Miscellaneous functions  The memset function

See §4.11.1, and §  The strerror function

This function is a descendant of perror (see §  It is defined such that it can return a pointer to an in-memory read-only string, or can copy a string into a static buffer on each call.  The strlen function

This function is now specified as returning a value of type size_t (See § 

4.10 General Utilities  -< ANSI C Rationale  -> 4.12 Date and Time              Index