C string.h

<string.h> is the standard C header for many string and memory operations.

It includes tools for measuring, comparing, copying, joining, searching, and moving character or byte data.

Include string.h

#include <string.h>

The header declares functions for two closely related areas:

What string.h provides in C The string.h header branches into string operations such as strlen, strcmp, strcpy, strcat and searching, and memory operations such as memcpy, memmove and memset. string.h → STRINGS + MEMORY STANDARD HEADER <string.h> STRING OPERATIONS strlen() strcmp() strcpy() strcat() strchr() strstr() MEMORY OPERATIONS memcpy() memmove() memset() IMPORTANT String functions need valid C strings and enough destination space. Memory functions use explicit byte counts and do not depend on null terminators.

strlen() — String Length

size_t length = strlen("Hello");

strlen() counts characters before the terminating '\0'.

For "Hello", the result is 5.

strcmp() — Compare Strings

int result = strcmp("cat", "cat");

strcmp() returns 0 when the two strings compare equal.

A negative or positive result tells which string compares before the other according to the compared character values.

strcpy() — Copy a String

char destination[20];

strcpy(destination, "Hello");

strcpy() copies the source string, including its terminating '\0'.

The destination array must be large enough for the complete result.

strncpy() Needs Care

char destination[5];

strncpy(destination, "Hello", sizeof destination);

strncpy() does not guarantee a terminating null character when the source length is at least the count.

It should not be treated as an automatically safe replacement for strcpy().

strcat() — Join Strings

char text[20] = "Hello";

strcat(text, " C");

The result becomes "Hello C".

The destination must already contain a valid string and have enough unused space.

Memory Functions

<string.h> also contains functions that work with byte ranges rather than C strings.

memcpy(destination, source, count);
memmove(destination, source, count);
memset(destination, value, count);

memcpy() vs memmove()

memcpy() memmove()
Copies a specified number of bytes. Copies a specified number of bytes.
Source and destination must not overlap. Works correctly even when the regions overlap.
Useful for non-overlapping blocks. Use when overlap is possible.

C string.h Example

#include <stdio.h>
#include <string.h>

int main(void) {
    char name[20] = "Ali";
    size_t length = strlen(name);

    printf("Name: %s\n", name);
    printf("Length: %zu\n", length);

    if (strcmp(name, "Ali") == 0) {
        puts("Names match.");
    }

    return 0;
}

Output

Name: Ali
Length: 3
Names match.

The example uses strlen() to measure the string and strcmp() to compare its contents.

String Functions Need Null-Terminated Strings

char text[] = {'H', 'i', '\0'};

Functions such as strlen() and strcmp() depend on a terminating null character.

If an array is not properly terminated and a string function keeps reading beyond it, the behavior is undefined.

Destination Size Matters

char small[4];

/* Too small for "Hello" and '\0' */

Copying or concatenating more characters than the destination can hold can write outside the array.

Always know the available destination size before using copy or concatenation functions.

Common string.h Mistakes

Mistake Correct Idea
Comparing strings with ==. Use strcmp() to compare string contents.
Using strlen() on data without a null terminator. Pass a valid C string.
Copying into an undersized destination. Ensure enough room for all characters and '\0'.
Using memcpy() on overlapping regions. Use memmove() when overlap is possible.
  1. Which function returns the length of a C string?
  2. Which function compares two C strings?
  3. Which memory-copy function should be used when regions may overlap?

Practice Answers

1. Which function finds string length?

Use strlen().

2. Which function compares strings?

Use strcmp().

3. Which function supports overlap?

Use memmove().

FAQs

What is string.h in C?

string.h is a standard C header that declares functions for working with null-terminated strings and blocks of memory.

Which function finds the length of a C string?

strlen() returns the number of characters before the terminating null character.

Should C strings be compared with ==?

No. Use strcmp() to compare the contents of null-terminated strings.

What is the difference between memcpy() and memmove()?

Both copy bytes, but memmove() is designed to work correctly when the source and destination regions overlap, while overlapping regions make memcpy() unsuitable.

Summary

<string.h> provides standard functions for C strings and raw memory blocks. Use strlen() for length, strcmp() for comparison, copy and concatenation functions only when destination space is sufficient, and memmove() instead of memcpy() when memory regions may overlap.