C Standard Library -<string.h>

Introduction

string .hThe header file defines a variable type, a macro, and various functions for operating on character arrays.

<string.h>Is a header file in the C standard library, providing a set of functions for handling strings and memory blocks. These functions cover string copying, concatenation, comparison, searching, and memory operations, etc.

Library variables

Below is the variable type defined in the header file string.h:

Serial NumberVariables & Description
1size_t
This is an unsigned integer type, which issizeofResult of the keyword.

Library macros

Below are the macros defined in the header file string.h:

Serial NumberMacros & Description
1NULL
This macro is the value of a null pointer constant.

Library functions

Below are the functions defined in the header file string.h:

Serial NumberFunctions & Description
1void *memchr(const void *str, int c, size_t n)
In the parameterstrSearches for the first occurrence of character c (an unsigned character) in the first n bytes of the string pointed to.
2int memcmp(const void *str1, const void *str2, size_t n)
holdstr1andstr2Compares the first n bytes.
3void *memcpy(void *dest, const void *src, size_t n)
Copies n characters from src todest。
4void *memmove(void *dest, const void *src, size_t n)
another used to copy fromsrccopy n characters todestof the function.
5void *memset(void *str, int c, size_t n)
Copies the specified value c to the first n bytes of the memory area pointed to by str.
6char *strcat(char *dest, const char *src)
holdsrcAppends the string pointed to bydestTo the end of the string pointed to by
7char *strncat(char *dest, const char *src, size_t n)
holdsrcAppends the string pointed to bydestthe end of the string it points to, up to a length of n characters.
8char *strchr(const char *str, int c)
In the parameterstrSearches for the first occurrence of character c (an unsigned character) in the string pointed to.
9int strcmp(const char *str1, const char *str2)
holdstr1The string pointed to bystr2Compares with the string pointed to by
10int strncmp(const char *str1, const char *str2, size_t n)
holdstr1andstr2for comparison, comparing at most the first n bytes.
11int strcoll(const char *str1, const char *str2)
holdstr1andstr2Compares, and the result depends on the LC_COLLATE locale setting.
12char *strcpy(char *dest, const char *src)
holdsrcthe string pointed to by is copied todest。
13char *strncpy(char *dest, const char *src, size_t n)
holdsrcthe string pointed to by is copied todest, copying at most n characters.
14size_t strcspn(const char *str1, const char *str2)
Counts how many consecutive characters at the beginning of string str1 contain none of the characters in string str2.
15char *strerror(int errnum)
Searches the internal array for the error number errnum and returns a pointer to the error message string.
16size_t strlen(const char *str)
Calculates the length of string str, up to but not including the null terminating character.
17char *strpbrk(const char *str1, const char *str2)
Search stringstr1The first matching string instr2a character from the characters, not including the null terminating character. That is, it examines the characters in string str1 one by one; when a tested character is also found in string str2, it stops and returns the position of that character.
18char *strrchr(const char *str, int c)
In the parameterstrSearches for the last occurrence of character c (an unsigned character) in the string pointed to.
19size_t strspn(const char *str1, const char *str2)
Search stringstr1The first character in ... that is not in the stringstr2The index of the character appearing in
20char *strstr(const char *haystack, const char *needle)
In the stringhaystackFinds the first occurrence of the stringneedle(not including the null terminating character).
21char *strtok(char *str, const char *delim)
Split stringstras a set of strings,delimas the delimiter.
22size_t strxfrm(char *dest, const char *src, size_t n)
Converts the string according to the LC_COLLATE category of the program's current locale.srcthe frontnCharacters, and places them in the stringdestIn.

Example

The following is using<string.h>Examples of some functions in

Copying strings:

Example

#include <stdio.h>
#include <string.h>

int main() {
    char src[] = "Hello, World!";
    char dest[50];

    strcpy(dest, src);
    printf("Copied string: %s\n", dest);

    return 0;
}

Concatenating strings:

Example

#include <stdio.h>
#include <string.h>

int main() {
    char str1[50] = "Hello";
    char str2[] = ", World!";

    strcat(str1, str2);
    printf("Concatenated string: %s\n", str1);

    return 0;
}

Comparing strings:

Example

#include <stdio.h>
#include <string.h>

int main() {
    char str1[] = "Hello";
    char str2[] = "World";

    int result = strcmp(str1, str2);
    if (result == 0) {
        printf("Strings are equal\n");
    } else {
        printf("Strings are not equal\n");
    }

    return 0;
}

Finds the first occurrence of a character in a string:

Example

#include <stdio.h>
#include <string.h>

int main() {
    char str[] = "Hello, World!";
    char *ptr = strchr(str, 'o');

    if (ptr != NULL) {
        printf("First occurrence of 'o' found at position: %ld\n", ptr - str);
    } else {
        printf("Character not found\n");
    }

    return 0;
}

Copying memory blocks:

Example

#include <stdio.h>
#include <string.h>

int main() {
    char src[] = "Hello, World!";
    char dest[50];

    memcpy(dest, src, strlen(src) + 1);
    printf("Copied memory: %s\n", dest);

    return 0;
}

Notes

  • When using string functions, ensure that the destination buffer has enough space to store the result to avoid buffer overflow.
  • strncpyandstrncatCan specify the maximum number of characters to copy or concatenate, which helps prevent buffer overflow.
  • strtokThe function is not thread-safe because it internally uses static variables to save context. If used in a multithreaded environment, you need to usestrtok_rfunction.
  • Usagememcpywhen ... the source and destination memory areas should not overlap. If overlap may occur, you should usememmove。

By understanding and using<string.h>The provided functions allow convenient string and memory operations, enabling more efficient and reliable C programs.

other extensions