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.
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:
str-),
strn-),
and
mem-).
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.
memcpy function
memmove function
strcpy function
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.
strcat function
strncat function
Note that this function may add n+1 characters to the string.
memcmp functionSee §4.11.1.
strcmp function
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.
strncmp function
strxfrm functionSee §4.11.4.3.
memchr functionSee §4.11.1.
strchr function
strcspn function
strpbrk function
strrchr function
strspn function
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.
strtok functionThis function has been included to provide a convenient solution to many simple problems of lexical analysis, such as scanning command line arguments.
memset function
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.
strlen function
This function is now specified as returning a value of type size_t.
(See §3.3.3.4.)