Parts of <string.h> in C
Variables
The string.h header file defines one variable called size_t. Size_t is an unsigned integral type which is the result of sizeof keyword. It gurantees to be big enough that it contain the size of the biggest oject that the host system can handle.
While using the size_t objects we must ensure that in all the contexts it is used including arithmetic we use only non-negative values.
Using size_t with loops
Example
#include <stdio.h>
#include <string.h>
int main()
{
printf("Using size_t with possible values\n");
for(size_t i=0;i<2;i++){
printf("%lu\n",i);
}
}

You can also try this code with Online C Compiler
Run Code
Output
Using size_t with possible values
0
1
Example
#include <stdio.h>
#include <string.h>
int main()
{
printf("Using size_t with negative values\n");
for(size_t i=2;i>=0;i--){
printf("%lu\n",i);
}
}

You can also try this code with Online C Compiler
Run Code
Output
Infinite loop
You can also read about the dynamic arrays in c, and Tribonacci Series
Macros
String,h header file defines a NULL macro. The value of a NULL macro is a null pointer constant.The NULL macro can have the following declarations depending on the compiler
Syntax
#define NULL ((char *)0)
or
#define NULL 0L
or
#define NULL 0
Example
#include <string.h>
#include <stdio.h>
int main () {
FILE *f;
f = fopen("file.txt", "r");
if( f != NULL ) {
printf("file.txt successfully opened \n");
fclose(f);
}
f = fopen("nofile.txt", "r");
if( f == NULL ) {
printf("Could not open file nofile.txt\n");
}
return(0);
}

You can also try this code with Online C Compiler
Run Code
Output
Could not open file nofile.txt
For the above code we have assumed that neither file.txt nor nofile.txt file exists.
Functions of C string.h Library
void *memchr(const void *str, int x,size_t n)
The function accepts three parameteres: str- pointer to the block of memory where the search in to be done, x- this int value is converted to unsigned char on which the function performs a byte per byte seach, n- number of bytes to be analyzed. The function returns a pointer to matching byte or NULL if character does not occur in the given memory.
Example
#include <stdio.h>
#include <string.h>
int main () {
const char str[] = "Coding Ninjas Studio";
const char ch = 'e';
char *ret;
ret = memchr(str, ch, strlen(str));
printf("String after '%c' is '%s'\n", ch, ret+1);
return(0);
}

You can also try this code with Online C Compiler
Run Code
Output
String after 'e' is 'Studio'
int memcmp(const void *string1,const void *string2,size_t n)
The function accepts three parameters: *string1 - a pointer to the block of memory occupied by string1, *string2 - a pointer to the block of memory occupied by the string2, n - the number of bytes to be compared. The function returns an integer value, the value can be : <0 indicating string1 is less than string2, >0 indicating string2 is less than string1, =0 indicating string1 is equal to string2
Example
#include <stdio.h>
#include <string.h>
int main () {
char string1[]="Coding Ninjas Studio";
char string2[]="CODESTUDIO";
int res = memcmp(string1, string2, 5);
if(res == 0) {
printf("string1 is equal to string2");
} else if(res < 0) {
printf("string1 is less than string2");
} else {
printf("string2 is less than string1");
}
}

You can also try this code with Online C Compiler
Run Code
Output
string2 is less than string1
void *memcpy(void *destination,const void *source,size_t n)
The function accepts three parameters: destination - a pointer to the destination array where the content to be copied, source - a pointer to the source of content, n - number of bytes to be copied. The functions returns a pointer to the destination where the content is copied too.
Example
#include <stdio.h>
#include <string.h>
int main () {
const char source[50] = "Hello Coders!!!";
char dest[50]="Coding Ninjas Studio";
printf("Destination string before = %s\n", dest);
memcpy(dest, source, strlen(source)+1);
printf("Destination string after = %s\n", dest);
}

You can also try this code with Online C Compiler
Run Code
Output
Destination string before = Coding Ninjas Studio
Destination string after = Hello Coders!!!
void *memset(void *string,int x,size_t n)
The function accepts three parameters: *string - a pointer to the block of memory which is to be filled, x - the value to filled in the block of memory is passed as int and is converted into unsigned char, n - number of bytes to be set. The function returns a pointer to the memory occupied by string.
Example
#include <stdio.h>
#include <string.h>
int main () {
char str[]="CODESTUDIO";
printf("String Before = %s",str);
memset(str,'*',4);
printf("\nString After = %s",str);
}

You can also try this code with Online C Compiler
Run Code
Output
String Before = CODESTUDIO
String After = ****STUDIO
char *strcat(char *destination,const char *source)
The function accepts two parameters: *destination- a pointer to the destination char array, *source - a pointer to the string to be appended. The function returns a pointer to the resulting string destination in which we have appended the source string.
Check out also strcat() function in C and Short int in C Programming
Example
#include <stdio.h>
#include <string.h>
int main () {
char src[50]=" World!!", dest[50]="Hello";
strcat(dest, src);
printf("String2 after concatenation : %s", dest);
}

You can also try this code with Online C Compiler
Run Code
Output
String2 after concatenation : Hello World!!
You can implement code by yourself on online compiler.
Also check out - Substr C++
Some Other functions of <string.h>
-
memmove() : This function is used to copy the first n characters from string2 to string1.
-
strncat() : This function is used to append only the first n characters of string1 to string2.
-
strchr() : This function is used to find the first occurrence of a character in the string.
-
strcmp() : This function is used to compare two strings and returns an integer value based on the comparison.
-
strncmp() : This function is similar to strcmp() but it compares only the first n characters of both the strings.
-
strlen() : This function is used to calculate the length of a string. Terminating character is not included in the length calculated.
-
strstr() : This function is used to find the first occurrence of string1 in string2.
- strncpy() : This function is used to copy only the first n characters of the source string to the destination string.
Frequently Asked Questions
What is the difference between strcmp() and strncmp()?
Both the functions strcmp() and strncmp() are used to compare two strings with each other. The strcmp() function compares the two strings for their entire length whereas the strncmp() function only compares the two strings for first n characters.
What is the difference between memset() and strncpy()?
The memset() function is used to set the first n characters of the string with a particular character whereas the strncpy() function is used to copy the first n characters of the source string to the destination string.
Is Cstring and string H the same?
No, "CString" usually refers to a string type in languages like C++, not a library. The C library <string.h> provides functions for manipulating C-style null-terminated strings.
What types of operations can I perform using functions in <string.h>?
Using <string.h>, you can perform operations like copying, concatenating, comparing, and searching within strings, as well as determining string lengths and manipulating memory blocks.
Conclusion
In this article, we have learned about the `<string.h>` header file in C, which provides a collection of functions for efficient string manipulation and memory operations. These functions simplify tasks like copying, concatenating, comparing, and searching strings, as well as manipulating blocks of memory. With the help of this `<string.h>` library, C programmers can write cleaner and more concise code when working with strings and memory.
We hope that this blog has helped you enhance your knowledge regarding <string.h> header file in the C programming language and if you would like to learn more.
Recommended Readings: