๐งต C <string.h>
โ String and Memory Manipulation Functions in C
๐งฒ Introduction โ What Is <string.h>
in C?
The <string.h>
header provides a set of functions and macros for working with C-style strings and memory blocks. Strings in C are arrays of characters terminated by a null character ('\0'
), and <string.h>
gives you the tools to manipulate themโcopying, comparing, concatenating, and more.
๐ฏ In this guide, youโll learn:
- Common string and memory handling functions
- Syntax and examples for each
- Best practices and safety tips
- Real-world use cases
๐ฆ Key Features of <string.h>
Category | Functions |
---|---|
String length | strlen() |
String copy/concat | strcpy() , strncpy() , strcat() , strncat() |
String comparison | strcmp() , strncmp() |
Memory operations | memcpy() , memset() , memcmp() |
Search and tokenize | strchr() , strstr() , strtok() |
๐ Commonly Used Functions
โ
strlen()
โ Get String Length
size_t len = strlen("hello"); // Returns 5
โ
strcpy()
/ strncpy()
โ Copy Strings
char dest[20];
strcpy(dest, "world"); // Copies the string
// or safer:
strncpy(dest, "world", 19); // Avoids buffer overflow
โ
strcat()
/ strncat()
โ Concatenate Strings
char str[20] = "Hello ";
strcat(str, "World!"); // Appends "World!" to str
โ
strcmp()
/ strncmp()
โ Compare Strings
if (strcmp("cat", "dog") < 0) {
printf("cat comes before dog\n");
}
๐ Returns 0 if strings are equal, a negative value if the first is less, positive if greater.
โ
strchr()
/ strstr()
โ Search in Strings
char *p = strchr("hello", 'e'); // Points to 'e'
char *s = strstr("abc123", "123"); // Points to "123"
โ
strtok()
โ Tokenize String
char str[] = "apple,banana,grape";
char *token = strtok(str, ",");
while (token != NULL) {
printf("%s\n", token);
token = strtok(NULL, ",");
}
๐ Useful for splitting input strings by a delimiter.
๐ง Memory Handling Functions
โ
memcpy()
โ Copy Raw Memory
memcpy(dest, src, size); // Fast block copy
โ
memset()
โ Set Memory
memset(arr, 0, sizeof(arr)); // Fill array with 0s
โ
memcmp()
โ Compare Memory Blocks
if (memcmp(buf1, buf2, size) == 0) {
printf("Buffers match");
}
๐ Real-World Use Cases
Scenario | Function(s) Used |
---|---|
Parsing CSV/text | strtok() , strchr() |
Copying strings | strcpy() , strncpy() |
Comparing passwords/input | strcmp() , strncmp() |
Allocating & clearing memory | memcpy() , memset() |
Search in config strings | strstr() , strchr() |
๐ก Best Practices & Tips
๐ Best Practice:
Use strncpy()
instead of strcpy()
to avoid buffer overflows.
๐ก Tip:
Always make sure destination arrays are large enough before using string functions.
โ ๏ธ Pitfall:strtok()
modifies the original stringโit is not thread-safe and should not be reused in nested loops.
๐ Summary โ Recap & Next Steps
The <string.h>
header is your go-to toolkit for low-level text processing and efficient memory operations. Mastering it is essential for building text-based applications, custom parsers, and binary data handlers.
๐ Key Takeaways:
strlen()
,strcpy()
,strcat()
,strcmp()
are fundamental string tools- Use
memcpy()
,memset()
, andmemcmp()
for raw memory operations strtok()
is powerful for breaking strings into tokens- Be careful with buffer sizes and use
strncpy()
/strncat()
for safety
โ๏ธ Real-World Relevance:
Commonly used in text processing tools, file parsers, config readers, and data serialization/deserialization.
โ Frequently Asked Questions (FAQ)
โ What does <string.h>
do?
โ It provides functions for manipulating C strings (char arrays) and memory blocks.
โ Is strcpy()
safe?
โ ๏ธ No. It doesnโt check for buffer overflows. Use strncpy()
or ensure buffer size manually.
โ Can I compare strings with ==
?
โ No. Use strcmp()
for string comparisons in C. ==
compares memory addresses, not content.
โ Whatโs the difference between memcpy()
and strcpy()
?
โ
memcpy()
copies raw memory, including null bytes.strcpy()
copies null-terminated strings only.
โ How do I split a string in C?
โ
Use strtok()
:
char *token = strtok(input, ",");
Share Now :