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. 

4.11.2.1  The memcpy function

4.11.2.2  The memmove function

4.11.2.3  The strcpy function

4.11.2.4  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

4.11.3.1  The strcat function

4.11.3.2  The strncat function

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

4.11.4  Comparison functions

4.11.4.1  The memcmp function

See §4.11.1

4.11.4.2  The strcmp function

4.11.4.3  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. 

4.11.4.4  The strncmp function

4.11.4.5  The strxfrm function

See §4.11.4.3

4.11.5  Search functions

4.11.5.1  The memchr function

See §4.11.1

4.11.5.2  The strchr function

4.11.5.3  The strcspn function

4.11.5.4  The strpbrk function

4.11.5.5  The strrchr function

4.11.5.6  The strspn function

4.11.5.7  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. 

4.11.5.8  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

4.11.6.1  The memset function

See §4.11.1, and §4.10.3.1

4.11.6.2  The strerror function

This function is a descendant of perror (see §4.9.10.4).  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. 

4.11.6.3  The strlen function

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


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