Standard C Libraries. More...
Go to the source code of this file.
Macros | |
#define | size_t size_t |
#define | NULL (0) |
Functions | |
void * | memchr (const void *s, int c, size_t n) |
int | memcmp (const void *s1, const void *s2, size_t n) |
void * | memcpy (void *dst, const void *src, size_t n) |
void * | memmove (void *s1, const void *s2, size_t n) |
void * | memset (void *s, int c, size_t n) |
char * | strcat (char *s1, const char *s2) |
char * | strchr (const char *s, int c) |
int | strcmp (const char *s1, const char *s2) |
int | strcoll (const char *s1, const char *s2) |
char * | strcpy (char *s1, const char *s2) |
size_t | strcspn (const char *s1, const char *s2) |
char * | strerror (int errcode) |
size_t | strlen (const char *s) |
char * | strncat (char *s1, const char *s2, size_t n) |
int | strncmp (const char *s1, const char *s2, size_t n) |
char * | strncpy (char *s1, const char *s2, size_t n) |
char * | strpbrk (const char *s1, const char *s2) |
char * | strrchr (const char *s, int c) |
size_t | strspn (const char *s1, const char *s2) |
char * | strstr (const char *s1, const char *s2) |
char * | strtok (char *s1, const char *s2) |
size_t | strxfrm (char *s1, const char *s2, size_t n) |
Standard C Libraries.
The header file, string.h, consists of types, macros and functions that provide tools to manipulate strings.
The documentation in this header file has been copied from the documentation provided with the Microchip MPLAB XC16 compiler. The original lincense agreement included with the XC16 compiler applies!
#define NULL (0) |
Description: The value of a null pointer constant.
Include: <string.h>
Description: The type of the result of the sizeof operator.
Include: <string.h>
void* memchr | ( | const void * | s, |
int | c, | ||
size_t | n | ||
) |
Description: Locates a character in a buffer.
Include: <string.h>
s | pointer to the buffer |
c | character to search for |
n | number of characters to check |
Remarks:
memchr stops when it finds the first occurrence of c or after searching n number of characters.
Example:
Output:
buf1 : What time is it?
i found at position 7
y not found
int memcmp | ( | const void * | s1, |
const void * | s2, | ||
size_t | n | ||
) |
Description: Compare the contents of two buffers.
Include: <string.h>
s1 | first buffer |
s2 | second buffer |
n | number of characters to compare |
Remarks:
This function compares the first n characters in s1 to the first n characters in s2 and returns a value indicating whether the buffers are less than, equal to or greater than each other.
Example:
Output:
buf1 : Where is the time?
buf2 : Where did they go?
buf3 : Why?
6 characters of buf1 and buf2 are equal
buf2 comes before buf1
buf1 comes before buf3
void* memcpy | ( | void * | dst, |
const void * | src, | ||
size_t | n | ||
) |
Description: Copies characters from one buffer to another.
Include: <string.h>
dst | buffer to copy characters to |
src | buffer to copy characters from |
n | number of characters to copy |
Remarks:
memcpy copies n characters from the source buffer src to the destination buffer dst. If the buffers overlap, the behavior is undefined.
Example:
Output:
buf1 :
buf2 : Where is the time?
buf3 : Why?
buf1 after memcpy of 6 chars of buf2:
Where
buf1 after memcpy of 5 chars of buf3:
Why?
void* memmove | ( | void * | s1, |
const void * | s2, | ||
size_t | n | ||
) |
Description: Copies n characters of the source buffer into the destination buffer, even if the regions overlap.
Include: <string.h>
s1 | buffer to copy characters to (destination) |
s2 | buffer to copy characters from (source) |
n | number of characters to copy from s2 to s1 |
Remarks:
If the buffers overlap, the effect is as if the characters are read first from s2, then written to s1, so the buffer is not corrupted.
Example:
Output:
buf1 : When time marches on
buf2 : Where is the time?
buf3 : Why?
buf1 after memmove of 6 chars of buf2:
Where ime marches on
buf1 after memmove of 5 chars of buf3:
Why?
void* memset | ( | void * | s, |
int | c, | ||
size_t | n | ||
) |
Description: Copies the specified character into the destination buffer.
Include: <string.h>
s | buffer |
c | character to put in buffer |
n | number of times |
Remarks:
The character, c, is written to the buffer n times.
Example:
Output:
memset("What time is it?", '?',4);
buf1 after memset: ???? time is it?
memset("", 'y',10);
buf2 after memset: yyyyyyyyyy
char* strcat | ( | char * | s1, |
const char * | s2 | ||
) |
Description: Appends a copy of the source string to the end of the destination string.
Include: <string.h>
s1 | null terminated destination string to copy to |
s2 | null terminated source string to be copied |
Remarks:
This function appends the source string (including the terminating null character) to the end of the destination string. The initial character of the source string overwrites the null character at the end of the destination string. If the buffers overlap, the behavior is undefined.
Example:
Output:
buf1 : We're here
(10 characters)
buf2 : Where is the time?
(18 characters)
buf1 after strcat of buf2:
We're hereWhere is the time?
(28 characters)
buf1 after strcat of "Why?":
We're hereWhere is the time?Why?
(32 characters)
char* strchr | ( | const char * | s, |
int | c | ||
) |
Description: Locates the first occurrence of a specified character in a string.
Include: <string.h>
s | pointer to the string |
c | character to search for |
Remarks:
This function searches the string s to find the first occurrence of the character, c.
Example:
Output:
buf1 : What time is it?
m found at position 8
y not found
int strcmp | ( | const char * | s1, |
const char * | s2 | ||
) |
Description: Compares two strings.
Include: <string.h>
s1 | first string |
s2 | second string |
Remarks:
This function compares successive characters from s1 and s2 until they are not equal or the null terminator is reached.
Example:
Output:
buf1 : Where is the time?
buf2 : Where did they go?
buf3 : Why?
buf2 comes before buf1
buf1 comes before buf3
"Why?" and buf3 are equal
int strcoll | ( | const char * | s1, |
const char * | s2 | ||
) |
Description: Compares one string to another. (See Remarks.)
Include: <string.h>
s1 | first string |
s2 | second string |
Remarks:
Since the 16-bit compiler does not support alternate locales, this function is equivalent to strcmp.
char* strcpy | ( | char * | s1, |
const char * | s2 | ||
) |
Description: Copy the source string into the destination string.
Include: <string.h>
s1 | destination string to copy to |
s2 | source string to copy from |
Remarks:
All characters of s2 are copied, including the null terminating character. If the strings overlap, the behavior is undefined.
Example:
Output:
buf1 : We're here
buf2 : Where is the time?
buf3 : Why?
buf1 after strcpy of buf2:
Where is the time?
buf1 after strcpy of buf3:
Why?
size_t strcspn | ( | const char * | s1, |
const char * | s2 | ||
) |
Description: Calculate the number of consecutive characters at the beginning of a string that are not contained in a set of characters.
Include: <string.h>
s1 | pointer to the string to be searched |
s2 | pointer to characters to search for |
Remarks:
This function will determine the number of consecutive characters from the beginning of s1 that are not contained in s2.
Example:
Output:
strcspn("hello", "aeiou") = 1
strcspn("animal", "aeiou") = 0
strcspn("animal", "xyz") = 6
Explanation:
In the first result, e is in s2 so it stops counting after h.
In the second result, a is in s2.
In the third result, none of the characters of s1 are in s2 so all characters are counted.
char* strerror | ( | int | errcode) |
Description: Gets an internal error message.
Include: <string.h>
errcode | number of the error code |
Remarks:
The array pointed to by strerror may be overwritten by a subsequent call to this function.
Example:
Output:
Cannot open samp.fil: file open error
size_t strlen | ( | const char * | s) |
Description: Finds the length of a string.
Include: <string.h>
s | the string |
Remarks:
This function determines the length of the string, not including the terminating null character.
Example:
Output:
str1 : We are here
(string length = 11 characters)
str2 :
(string length = 0 characters)
str3 : Why me?
(string length = 7 characters)
char* strncat | ( | char * | s1, |
const char * | s2, | ||
size_t | n | ||
) |
Description: Append a specified number of characters from the source string to the destination string.
Include: <string.h>
s1 | destination string to copy to |
s2 | source string to copy from |
n | number of characters to append |
Remarks:
This function appends up to n characters (a null character and characters that follow it are not appended) from the source string to the end of the destination string. If a null character is not encountered, then a terminating null character is appended to the result. If the strings overlap, the behavior is undefined.
Example:
Output:
buf1 : We're here
(10 characters)
buf2 : Where is the time?
(18 characters)
buf3 : Why?
(4 characters)
buf1 after strncat of 6 characters of buf2:
We're hereWhere
(16 characters)
buf1 after strncat of 25 characters of buf2:
We're hereWhere Where is the time?
(34 characters)
buf1 after strncat of 4 characters of buf3:
We're hereWhere Where is the time?Why?
(38 characters)
int strncmp | ( | const char * | s1, |
const char * | s2, | ||
size_t | n | ||
) |
Description: Compare two strings, up to a specified number of characters.
Include: <string.h>
s1 | first string |
s2 | second string |
n | number of characters to compare |
Remarks:
strncmp returns a value based on the first character that differs between s1 and s2. Characters that follow a null character are not compared.
Example:
Output:
buf1 : Where is the time?
buf2 : Where did they go?
buf3 : Why?
6 characters of buf1 and buf2 are equal
buf2 comes before buf1
buf1 comes before buf3
char* strncpy | ( | char * | s1, |
const char * | s2, | ||
size_t | n | ||
) |
Description: Copy characters from the source string into the destination string, up to the specified number of characters.
Include: <string.h>
s1 | destination string to copy to |
s2 | source string to copy from |
n | number of characters to copy |
Remarks:
Copies n characters from the source string to the destination string. If the source string is less than n characters, the destination is filled with null characters to total n characters. If n characters were copied and no null character was found, then the destination string will not be null-terminated. If the strings overlap, the behavior is undefined.
Example:
Output:
buf1 : We're here
buf2 : Where is the time?
buf3 : Why?
buf4 : Where?
buf1 after strncpy of 6 characters of buf2:
Where here
( 10 characters)
buf1 after strncpy of 18 characters of buf2:
Where is the time?
( 18 characters)
buf1 after strncpy of 5 characters of buf3:
Why?
( 4 characters)
buf1 after strncpy of 9 characters of buf4:
Where?
( 6 characters)
Explanation:
Each buffer contains the string shown, followed by null characters for a length of 50. Using strlen will find the length of the string up to, but not including, the first null character.
In the first example, 6 characters of buf2 ("Where ") replace the first 6 characters of buf1 ("We're ") and the rest of buf1 remains the same ("here" plus null characters).
In the second example, 18 characters replace the first 18 characters of buf1 and the rest remain null characters.
In the third example, 5 characters of buf3 ("Why?" plus a null terminating character) replace the first 5 characters of buf1. buf1 now actually contains ("Why?", 1 null character, " is the time?", 32 null characters). strlen shows 4 characters because it stops when it reaches the first null character.
In the fourth example, since buf4 is only 7 characters strncpy uses 2 additional null characters to replace the first 9 characters of buf1. The result of buf1 is 6 characters ("Where?") followed by 3 null characters, followed by 9 characters ("the time?"), followed by 32 null characters.
char* strpbrk | ( | const char * | s1, |
const char * | s2 | ||
) |
Description: Search a string for the first occurrence of a character from a specified set of characters.
Include: <string.h>
s1 | pointer to the string to be searched |
s2 | pointer to characters to search for |
Remarks:
This function will search s1 for the first occurrence of a character contained in s2.
Example:
Output:
strpbrk("What time is it?", "xyz")
match not found
strpbrk("What time is it?", "eou?")
match found at position 9
char* strrchr | ( | const char * | s, |
int | c | ||
) |
Description: Search for the last occurrence of a specified character in a string.
Include: <string.h>
s | pointer to the string to be searched |
c | character to search for |
Remarks:
The function searches the string s, including the terminating null character, to find the last occurrence of character c.
Example:
Output:
buf1 : What time is it?
m found at position 8
y not found
size_t strspn | ( | const char * | s1, |
const char * | s2 | ||
) |
Description: Calculate the number of consecutive characters at the beginning of a string that are contained in a set of characters.
Include: <string.h>
s1 | pointer to the string to be searched |
s2 | pointer to characters to search for |
Remarks:
This function stops searching when a character from s1 is not in s2.
Example:
Output:
strspn("animal", "aeiounm") = 5
strspn("animal", "aimnl") = 6
strspn("animal", "xyz") = 0
Explanation:
In the first result, l is not in s2.
In the second result, the terminating null is not in s2.
In the third result, a is not in s2 , so the comparison stops.
char* strstr | ( | const char * | s1, |
const char * | s2 | ||
) |
Description: Search for the first occurrence of a string inside another string.
Include: <string.h>
s1 | pointer to the string to be searched |
s2 | pointer to substring to be searched for |
Remarks:
This function will find the first occurrence of the string, s2 (excluding the null terminator) within the string, s1. If s2 points to a zero length string, s1 is returned.
Example:
Output:
str1 : What time is it?
str2 : is
str3 : xyz
"is" found at position 11
"xyz" not found
char* strtok | ( | char * | s1, |
const char * | s2 | ||
) |
Description: Break a string into substrings, or tokens, by inserting null characters in place of specified delimiters.
Include: <string.h>
s1 | pointer to the null terminated string to be searched |
s2 | pointer to characters to be searched for (used as delimiters) |
Remarks:
A sequence of calls to this function can be used to split up a string into substrings (or tokens) by replacing specified characters with null characters.
The first time this function is invoked on a particular string, that string should be passed in s1. After the first time, this function can continue parsing the string from the last delimiter by invoking it with a null value passed in s1.
It skips all leading characters that appear in the string, s2 (delimiters), then skips all characters not appearing in s2 (this segment of characters is the token), and then overwrites the next character with a null character, terminating the current token. The function, strtok, then saves a pointer to the character that follows, from which the next search will start.
If strtok finds the end of the string before it finds a delimiter, the current token extends to the end of the string pointed to by s1.
If this is the first call to strtok, it does not modify the string (no null characters are written to s1). The set of characters that is passed in s2 need not be the same for each call to strtok.
If strtok is called with a non-null parameter for s1 after the initial call, the string becomes the new string to search. The old string previously searched will be lost.
Example:
Output:
str1 : Here, on top of the world!
word 1: Here
word 2: on
word 3: top
word 4: of
word 5: the
word 6: world!
Description: Transforms a string using the locale-dependent rules. (See Remarks.)
Include: <string.h>
s1 | destination string |
s2 | source string to be transformed |
n | number of characters to transform |
Remarks:
If the return value is greater than or equal to n, the content of s1 is indeterminate. Since the 16-bit compiler does not support alternate locales, the transformation is equivalent to strcpy, except that the length of the destination string is bounded by n-1.