๐Ÿ“šC Standard Library Headers
Estimated reading: 3 minutes 279 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 :
Share

๐Ÿงต C <string.h>

Or Copy Link

CONTENTS
Scroll to Top