๐Ÿ“šC Standard Library Headers
Estimated reading: 3 minutes 7 views

๐Ÿงต 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>

CategoryFunctions
String lengthstrlen()
String copy/concatstrcpy(), strncpy(), strcat(), strncat()
String comparisonstrcmp(), strncmp()
Memory operationsmemcpy(), memset(), memcmp()
Search and tokenizestrchr(), 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

ScenarioFunction(s) Used
Parsing CSV/textstrtok(), strchr()
Copying stringsstrcpy(), strncpy()
Comparing passwords/inputstrcmp(), strncmp()
Allocating & clearing memorymemcpy(), memset()
Search in config stringsstrstr(), 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(), and memcmp() 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 :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

๐Ÿงต C <string.h>

Or Copy Link

CONTENTS
Scroll to Top