String header files in C language parsing __c language

Source: Internet
Author: User
Tags function prototype strcmp strtok

The standard library provides functions for handling strings whose prototypes are in the header file string.h.

the string.h header file defines a variable :
size_t: This is the unsigned integer type, which is the result of the sizeof keyword.

• Defines a macro :
Null: This macro is the value of a null pointer constant.

22 library functions are defined in the String.h header file. among the commonly used are: memcpy function

Function prototypes: void *memcpy (void *dest, const void *SRC, size_t n);
Function Description: Copy n characters from Src to dest.
function returns: This function returns a pointer to the Target store dest.
Characteristics:
The actual type of 1:SRC and Dest is independent of the function, and the actual result is based on the binary copy (binary copy).
2: The function copies only n characters, without checking src when null occurs. The general copy length is sizeof (SRC).
3: To prevent overflow (overflow), the length of dest and SRC should be at least equal to N, and they cannot overlap (overlap). memset function

Function prototypes: void *memset (void *str, int c, size_t N);
Function Description: Copy the characters c to the first n characters that Str points to.
function return: Returns a pointer to the STR store.
Features: typically used to initialize or zero a storage space (such as struct, etc.). strcat function

Function prototypes: Char *strcat (char *dest, const char *SRC);
Function Description: Appends the string src points to the end of the string that dest points to.
function return value: Returns a pointer to the final destination string dest.
Characteristics:
The null at the end of the 1:dest is overwritten by the SRC first character, and the newly generated string is added with null.
2: Need to ensure that the dest pointer to the space is large enough.
3:dest and SRC cannot overlap. strncat function

Function prototypes: Char *strncat (char *dest, const char *SRC, size_t N);
Function Description: Appends the string src points to the end of the string pointed to by dest until the N-character length.
function return value: Returns a pointer to the final destination string dest.
Characteristics:
1: When the SRC pointer points to space less than N, only the dest appended to the SRC line of N.
2: The rest is the same as strcat. strchr function

Function prototype: char *STRCHR (const char *STR, int c);
Function Description: Searches for the first occurrence of the character C (unsigned char) in the string pointed to by parameter Str.
function return value: Returns the first occurrence of the character C in string str, or NULL if the character is not found.
Characteristics:
1: Note that return is char *, not int strcmp function

function prototypes: int strcmp (const char *STR1, const char *STR2);
Function Description: Compares the string that the str1 points to and the string that the str2 points to.
function returns:
If the return value < 0 indicates that STR1 is less than str2.
If the return value > 0 indicates that str2 is less than str1.
If the return value = 0, the str1 equals str2. strncmp function

function prototypes: int strncmp (const char *STR1, const char *STR2, size_t N);
Function Description: Compares the string that the str1 points to and the first n characters of the string that the str2 points to.
function return: Reference strcmp strcpy function

Function prototypes: Char *strcpy (char *dest, const char *SRC);
Function Description: Copy the string src points to dest.
function return: Returns a pointer to the final destination string dest.
Characteristics:
1:dest pointed to a large enough space
2:dest and src memory do not overlap strncpy Function

Function prototypes: Char *strncpy (char *dest, const char *SRC, size_t N);
Function Description: Copy the string src points to dest and copy up to n characters.
function return: Returns a pointer to the final target string dest
Characteristics:
1: When the length of SRC is less than N, the remainder of the dest will be populated with empty bytes.
2: When the length of SRC is greater than n, no null characters are added to the end of the dest. To avoid errors that occur without null at the end of a character, you need to manually place the last character null.
3: Generally for the sake of insurance, often put N to sizeof (dest)-1, that is, using the original dest end of the null character.
4:SRC and dest memory space cannot overlap. strerror function

Function prototype: char *strerror (int errnum);
Function Description: Searches for the error number errnum from the internal array and returns a pointer to the error message string.
function return: Returns a pointer to the error string that describes the error errnum.
Characteristics:
The error string generated by 1:strerror depends on the development platform and compiler. strlen Function

Function prototype: size_t strlen (const char *STR);
Function Description: Computes the length of the string str until the null ending character, but not the null ending character.
function return: Returns the length of a string
Characteristics:
1: Terminates when you encounter null, which is the number of characters that actually return before the first null.
2: Notice the difference with sizeof, sizeof will be null to calculate, and strlen not. strstr function

Function prototypes: Char *strstr (const char *haystack, const char *needle);
Function Description: Finds the first occurrence of a string needle in a string haystack.
function return: Returns the position of the first occurrence of the needle string in haystack, or null if not found.
Characteristics:
1: The matching procedure does not include null characters, but stops matching with null characters. strtok function

Function prototypes: Char *strtok (char *str, const char *delim);
Function Description: Explode string str is a set of strings and Delim is a separator.
function return: Returns a pointer to the segmented fragment, or returns a null pointer if there is no string to retrieve.
Characteristics:
1: When Strtok () finds the split character contained in the parameter Delim in the string of parameter S, the character is changed to '.
2: On the first call, the Strtok () must give the parameter s string; then the call sets the parameter s to null.
Explanation: The first time a character in Delim is found, and the character is changed to NULL, the subsequent partition begins with a null character, so the subsequent call sets S to null. some of the less common are: memchr function

Function prototypes: void *MEMCHR (const void *STR, int c, size_t N);
Function Description: Searches for the first occurrence of the character C (unsigned char) in the top n bytes of the string pointed to by parameter Str.
function return: Returns a pointer to a matching byte that returns NULL if no characters appear in the given memory area. memcmp function

function prototypes: int memcmp (const void *STR1, const void *STR2, size_t n));
Function Description: Compares the storage area str1 with the first n bytes of the store str2.
function returns:
If the return value < 0 indicates that STR1 is less than str2.
If the return value > 0 indicates that str2 is less than str1.
If the return value = 0, the str1 equals str2 memmove function

Function prototypes: void *memmove (void *dest, const void *SRC, size_t n);
Function Description: Copy n characters from Src to dest.
function return: Returns a pointer to the Target store dest
Characteristics:
1: In the area of overlapping memory blocks, memmove () is a more secure method than memcpy (). If the target area and the source area overlap, memmove () can ensure that the source string copies the byte of overlapping area to the target area before overwriting, and the contents of the source area are changed after the copy is overwritten.
2: the memcpy () function is the same if the destination area does not overlap with the source area. strrchr function

Function prototype: char *STRRCHR (const char *STR, int c);
Function Description: Searches for the last occurrence of the character C (unsigned char) in the string pointed to by parameter Str.
function return: Returns the position of the last occurrence of character C in Str. If the value is not found, the function returns a null pointer.
Characteristics:
1:null is considered to be part of STR, so this function can be used to get a pointer to the end of the string. strspn function

Function prototype size_t strspn (const char *STR1, const char *STR2);
Function Description: Retrieves the first word Poute in the string str1 that is not present in the string str2.
function return: Returns the first word Poute in str1 that is not present in the string str2.
Characteristics:
1: If STRSPN () returns the value of N, then the beginning of the string s begins with n characters that belong to the string str2.
2: The search does not contain null characters, but NULL to stop the retrieval. strcspn function

Function prototypes: size_t strcspn (const char *STR1, const char *STR2);
Function Description: Retrieves a string str1 the first consecutive number of characters do not contain characters in the string str2.
function return: Returns the number of characters in the string str2 that are not str1 at the beginning of a sequence.
Characteristics:
1: The retrieve contains a null character, so the function will return the length of the str1 if any character in str1 does not belong to STR2.

Another strcoll function, strpbrk function, strxfrm function, the use of less likely to use when the data can be found.

Reference:
1:http://www.runoob.com/cprogramming/c-standard-library-string-h.html

2:http://www.cplusplus.com/reference/cstring/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.