YouTip LogoYouTip

C Standard Library String H

# C Standard Library – `` ## Introduction The **string.h** header file defines a variable type, a macro, and various functions for manipulating character arrays. `` is a header file in the C standard library that provides a set of functions for handling strings and memory blocks. These functions cover string copying, concatenation, comparison, searching, and memory operations. ## Library Variables The following variable types are defined in the header file string.h: | No. | Variable & Description | | --- | --- | | 1 | **size_t** This is an unsigned integer type, which is the result of the **sizeof** keyword. | ## Library Macros The following macros are defined in the header file string.h: | No. | Macro & Description | | --- | --- | | 1 | **NULL** This macro is the value of a null pointer constant. | ## Library Functions The following functions are defined in the header file string.h: | No. | Function & Description | | --- | --- | | 1 | [void *memchr(const void *str, int c, size_t n)](#) Searches for the first occurrence of character `c` (an unsigned char) in the first `n` bytes of the string pointed to by `str`. | | 2 | [int memcmp(const void *str1, const void *str2, size_t n)](#) Compares the first `n` bytes of `str1` and `str2`. | | 3 | [void *memcpy(void *dest, const void *src, size_t n)](#) Copies `n` characters from `src` to `dest`. | | 4 | [void *memmove(void *dest, const void *src, size_t n)](#) Another function for copying `n` characters from `src` to `dest`. | | 5 | [void *memset(void *str, int c, size_t n)](#) Copies the value `c` (converted to an unsigned char) to the first `n` bytes of the memory area pointed to by `str`. | | 6 | [char *strcat(char *dest, const char *src)](#) Appends the string pointed to by `src` to the end of the string pointed to by `dest`. | | 7 | [char *strncat(char *dest, const char *src, size_t n)](#) Appends the string pointed to by `src` to the end of the string pointed to by `dest`, up to `n` characters long. | | 8 | [char *strchr(const char *str, int c)](#) Searches for the first occurrence of character `c` (an unsigned char) in the string pointed to by `str`. | | 9 | [int strcmp(const char *str1, const char *str2)](#) Compares the string pointed to by `str1` to the string pointed to by `str2`. | | 10 | [int strncmp(const char *str1, const char *str2, size_t n)](#) Compares `str1` and `str2`, comparing at most the first `n` bytes. | | 11 | [int strcoll(const char *str1, const char *str2)](#) Compares `str1` and `str2`, with the result depending on the `LC_COLLATE` locale setting. | | 12 | [char *strcpy(char *dest, const char *src)](#) Copies the string pointed to by `src` to `dest`. | | 13 | [char *strncpy(char *dest, const char *src, size_t n)](#) Copies up to `n` characters from the string pointed to by `src` to `dest`. | | 14 | [size_t strcspn(const char *str1, const char *str2)](#) Calculates the length of the initial segment of `str1` which consists entirely of characters not in `str2`. | | 15 | [char *strerror(int errnum)](#) Searches the internal array for the error number `errnum` and returns a pointer to the error message string. | | 16 | [size_t strlen(const char *str)](#) Calculates the length of the string `str` up to, but not including, the terminating null character. | | 17 | [char *strpbrk(const char *str1, const char *str2)](#) Finds the first character in string `str1` that matches any character in string `str2`, excluding the terminating null character. In other words, it examines each character in `str1` and stops when it finds a character that is also present in `str2`, then returns a pointer to that character in `str1`. | | 18 | [char *strrchr(const char *str, int c)](#) Searches for the last occurrence of character `c` (an unsigned char) in the string pointed to by `str`. | | 19 | [size_t strspn(const char *str1, const char *str2)](#) Calculates the length of the initial segment of `str1` which consists entirely of characters in `str2`. | | 20 | [char *strstr(const char *haystack, const char *needle)](#) Finds the first occurrence of the substring `needle` (excluding the terminating null character) in the string `haystack`. | | 21 | [char *strtok(char *str, const char *delim)](#) Breaks string `str` into a series of tokens separated by `delim`. | | 22 | [size_t strxfrm(char *dest, const char *src, size_t n)](#) Transforms the first `n` characters of the string `src` according to the current locale setting for `LC_COLLATE` and stores the result in `dest`. | ### Examples Here are some examples using functions from ``. Copying a string: ## Example ```c #include #include int main() { char src[] = "Hello, World!"; char dest; strcpy(dest, src); printf("Copied string: %sn", dest); return 0; } Concatenating strings: ## Example ```c #include #include int main() { char str1 = "Hello"; char str2[] = ", World!"; strcat(str1, str2); printf("Concatenated string: %sn", str1); return 0; } Comparing strings: ## Example ```c #include #include int main() { char str1[] = "Hello"; char str2[] = "World"; int result = strcmp(str1, str2); if (result == 0) { printf("Strings are equaln"); } else { printf("Strings are not equaln"); } return 0; } Finding the first occurrence of a character in a string: ## Example ```c #include #include int main() { char str[] = "Hello, World!"; char *ptr = strchr(str, 'o'); if (ptr != NULL) { printf("First occurrence of 'o' found at position: %ldn", ptr - str); } else { printf("Character not foundn"); } return 0; } Copying a memory block: ## Example ```c #include #include int main() { char src[] = "Hello, World!"; char dest; memcpy(dest, src, strlen(src) + 1); printf("Copied memory: %sn", dest); return 0; } ### Important Notes * When using string functions, ensure the destination buffer has enough space to store the result to avoid buffer overflow. * `strncpy` and `strncat` allow specifying the maximum number of characters to copy or concatenate, which helps prevent buffer overflow. * The `strtok` function is not thread-safe because it uses a static variable internally to store context. If used in a multithreaded environment, use the `strtok_r` function instead. * When using `memcpy`, the source and destination memory areas should not overlap. If overlap is possible, use `memmove`. By understanding and using the functions provided by ``, you can conveniently perform string and memory operations, writing more efficient and reliable C programs.
← C Function IsupperC Standard Library Assert H β†’