C string.h Functions
In this page:
Copying Strings with strcpy() and strncpy()
string.h's copy functions duplicate character data from a source string into a destination buffer. strcpy() copies until it hits the null terminator with no length check at all, while strncpy() takes an explicit maximum character count, making it the safer choice whenever the destination buffer might be smaller than the source.
Example: Copying Strings with strcpy() and strncpy()
#include <stdio.h>
#include <string.h>
int main() {
char dest[20];
strncpy(dest, "Hello", 20);
printf("%s", dest);
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Concatenating Strings with strcat() and strncat()
strcat() appends the characters of a source string onto the end of a destination string, overwriting the destination's existing null terminator and adding a new one after the appended text. strncat() takes a maximum number of characters to append, which is essential for preventing a source string from overflowing the destination buffer.
Example: Concatenating Strings with strcat() and strncat()
#include <stdio.h>
#include <string.h>
int main() {
char str[20] = "Hello";
strncat(str, " World", 6);
printf("%s", str);
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Comparing Strings with strcmp() and strncmp()
strcmp() compares two strings character by character in lexicographic (dictionary) order, returning 0 only when they are exactly identical, a negative value when the first string sorts earlier, and a positive value when it sorts later. strncmp() performs the same comparison but stops after a specified number of characters, useful for comparing only a prefix.
Example: Comparing Strings with strcmp() and strncmp()
#include <stdio.h>
#include <string.h>
int main() {
printf("%d", strcmp("apple", "apple"));
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Finding Lengths with strlen()
strlen() walks a string until it finds the terminating null byte and returns the count of characters before it, which does not include that null terminator itself in the returned length. This is why a buffer that holds an n-character string must actually be at least n+1 bytes to leave room for the terminator strlen() doesn't count.
Example: Finding Lengths with strlen()
#include <stdio.h>
#include <string.h>
int main() {
printf("%zu", strlen("Hello"));
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Searching Strings with strchr() and strstr()
strchr() scans a string for the first occurrence of a single character and returns a pointer to it (or NULL if not found), while strstr() performs the same kind of search but looks for an entire substring rather than just one character. Both are the standard tools for locating text inside a larger C string without writing a manual search loop.
Example: Searching Strings with strchr() and strstr()
#include <stdio.h>
#include <string.h>
int main() {
char *result = strchr("Hello", 'e');
printf("%s", result);
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Chapter Quiz — Complete all 20 topics to unlock
0/20 topics done
Complete these topics first:
- C typedef
- C Type Casting
- C Bit Fields
- C Variable Length Arrays
- C Command Line Arguments
- C Function Pointers
- C Callback Functions
- C Multidimensional Pointer
- C string.h Functions
- C stdlib.h Functions
- C math.h Functions
- C time.h Functions
- C ctype.h Functions
- C errno.h
- C assert.h
- C Error Handling
- C Debugging Techniques
- C Code Style & Best Practices
- C Common Mistakes
- C Interview Questions