C Library - <string.h>

Learn via video course
FREE
View all courses
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
Topics Covered

Overview

String is an array of characters. The C language provides us with the string.h library to manipulate and work with the strings. Also, there are various data types and macros present to work with strings. To perform operations like comparison, concatenation, and copying, this string.h library is widely used.

What is String in C?

A string is an array of characters with a specialty that terminates with a special character '\0' (i.e., NULL).

There are two ways to declare the string in C as follows:

Declaration Method - 1 :

In this type of declaration, we need to declare the '\0' explicitly.

Declaration Method - 2 :

In this type of declaration, the '\0' gets declared automatically.

What is a string.h in C?

Having the familiarity of string.h in C is very important to have as a C programmer because it is widely used to manipulate and work with strings.

The file string.h in C contains the macro definitions, constants, and declarations of functions which we will see in the next sections of this article.

Syntax

We can include the string.h in C language with the following syntax :

Introduction to string.h Functions in C

There are so many functions defined in the string.h file with which we can perform operations like comparing two strings, concatenating strings, copying one string to another, and performing various string manipulation operations.

The string.h functions in C are not only used for string handling or manipulation but also used for various memory handling operations.

Example

Let's see some of the examples to understand the usage of functions from string.h in the C.

Output :

In the above code, we worked with the 3 different functions strlen, strcpy, and strchr. Their work is as mentioned in the above lists. You can try to work with other functions as given above.

The Variable Type and Macro Defined in the Header string.h

size_t Variable Type in string.h

In the string.h header, we have one variable type named size_t. size_t is an unsigned integral data type and it represents the size of objects in bytes. It is also used as the return type for the sizeof operator.

The size_t datatype is similar to the unsigned long long in the 64-bit compiler and it is never negative.

Due to this property of the size_t variable, it is used in so many C library functions like strlen as an argument or return type as shown in the following example.

NULL Macro in string.h

In string.h the NULL macro is the value of a null-pointer constant.

NULL is a special value given to indicate that the pointer is neither pointing nor referring to any other object.

A null-pointer constant is also known as an integral constant expression that evaluates to zero(0), to make it a null pointer constant we cast the void* on a 0, and it is represented as ((void*)0).

The usage of the null-pointer constant provides certain advantages over using literals like 0, it produces warnings by the compilers on improper use.

C string.h Library Functions

There are various functions in the string.h header used for various operations on the string.

Following is the list of functions and their description:

Copying Strings

Sr.FunctionDescription
1void* memcpy(void *dest, const void *src, size_t n)It takes 3 parameters destination string pointer, source string pointer, and number of characters. It copies n characters from string src to the string dest and does not return any value to its caller function.
2void *memmove(void *dest, const void *src, size_t n)It takes 3 parameters destination string, source string, and number of characters. It moves n characters from string src to the string dest and does not return any value to its caller function.
3char *strcpy(char *dest, const char *src)It takes 2 parameters destination string pointer and source string pointer. It copies the string from string src to the string dest and returns the dest string to its caller function.
4char *strncpy(char *dest, const char *src, size_t n)It takes 3 parameters destination string pointer, source string pointer, and number of characters. It copies upto n characters from src to the dest and returns the dest string to its caller function.

Concatenating Strings

Sr.FunctionDescription
1char *strcat(char *str1, const char *str2)It takes 2 parameters and both are string pointers. It appends the string str2 at the end of string str1 and returns the concatenated string i.e. str1.
2char *strncat(char *str1, const char *str2, size_t n)It takes 3 parameters 2 of them are constant string pointers and 3rd is the number of characters. It appends not more than n characters of string str2 at the end of string str1 and returns the concatenated string i.e. str1.

Comparing Strings

Sr.FunctionDescription
1int memcmp(const void *str1, const void *str2, size_t n)It takes the 3 parameters out of which 2 are constant string pointers and 3rd is the number of characters. It compares the first n bytes of memory between str1 and str2. This function returns an integer value and if the returned value is equal to 0, then str1 is identical to str2, if the value is positive, then str1 is greater than str2 and if the value is negative then str1 is lesser than str2.
2int strcmp(const char *str1, const char *str2)It takes the 2 parameters of type constant string pointer to compare the string str1 and string str2. It returns the integer value and if the returned value is equal to 0, then str1 is identical to str2, if the value is positive then str1 is greater than str2 and if the value is negative then str1 is lesser than str2.
3int strncmp(const char *str1, const char *str2, size_t n)It takes the 3 parameters out of which 2 are constant string pointers and 3rd is the number of characters. It compares at most the first n bytes of string str1 with the string str2. It returns the integer value and if the returned value is equal to 0, then str1 is identical to str2, if the value is positive then str1 is greater than str2 and if the value is negative then str1 is lesser than str2.
4int strcoll(const char *str1, const char *str2)It takes the 2 parameters of the type string pointer and compares the string str1 with string str2 based on locale. It returns the integer value and if the returned value is equal to 0, then str1 is identical to str2, if the value is positive then str1 is greater than str2 and if the value is negative then str1 is lesser than str2.
5size_t strxfrm(char *dest, const char *src, size_t n)It takes the 3 parameters : string pointer, constant string pointer and the size of characters. It transforms first n characters from string src to the string dest based on locale. It returns the length of the transformed string.

Searching Strings

Sr.FunctionDescription
1void *memchr(const void *str, int c, size_t n)It takes 3 parameters of type const string pointer, integer, and the size_t. It searches for the first occurrence of character c from n bytes in str and returns nothing to the caller function.
2char *strchr(const char *str, int c)It takes 2 parameters of type const string pointer and integer. It searches for the first occurrence of character c in str and returns a pointer to the first occurrence of c in str.
3size_t strcspn(const char *str1, const char *str2)It takes the 2 parameters of type constant string pointer and gives the length of string str1 till there are no characters from string str2. It returns the value of type size_t which is the calculated length of the initial portion found.
4char *strpbrk(const char *str1, const char *str2)It takes the 2 parameters of type constant string pointer and finds the first occurrence of any character from string str2 in string str1. If the common character is found, it is returned to the caller function, else it returns the NULL value.
5char *strrchr(const char *str, int c)It takes 2 parameters of type const string pointer and integer. It searches for the last occurrence of character c in string str. If the common character is found, a pointer to it is returned to the caller function, else it returns the NULL value.
6size_t strspn(const char *str1, const char *str2)It takes the 2 parameters of type constant string pointer and returns the number of characters in the initial segment of str1 which consists only of characters from str2.
7char *strstr(const char *str1, const char *str2)It takes the 2 parameters of type constant string pointer. It gives the substring from string str1 which matches with string str2. If the substring is found, it returns the pointer which points to the initial occurrence of str2 in str1, else it returns NULL.
8char *strtok(char *str, const char *delim)It takes the 2 parameters of type string pointer and constant string pointer. It splits string str into tokens around delimiter delim and returns the char* which is the next token.

Other Operations on Strings

Sr.FunctionDescription
1void *memset(void *str, int c, size_t n)It takes 3 parameters of type void pointer, integer and size_t. It replaces first n characters from string str with character c. It does not return anything to its caller function.
2char *strerror(int errnum)It takes the parameter of type integer. It takes a pointer to the error message string based on the error number(errno) and returns the char* of the error message.
3size_t strlen(const char *str)It takes the parameter of type constant string pointer and calculates the length of the string without including the terminating character('\0'). It returns the length of the string of type size_t.

Conclusion

  • Array of characters is known as a string and there are two ways to declare the string.
  • string.h header file in C contains variable type, macro, and useful functions to work with the strings.
  • size_t is a variable type that is equivalent to unsigned long long and never is negative
  • NULL is an important macro and provides certain advantages.
  • There are various functions in string.h which help us to copy, concatenate, compare, search, and perform other operations on strings.