C string.h Header

Beginner
6 min read
Updated: Oct 2026
3 examples
Live preview

What Is string.h?

<string.h> is the C standard header for null-terminated strings and raw memory helpers. A C string is a sequence of characters ending with '\0'. Functions like strlen, strcpy, strcmp, and strstr assume that layout.

Remember
#include <string.h>

"hi" in memory → 'h' 'i' '\0'
strlen("hi") → 2   (does not count '\0')

strcpy / strcat → need enough room in dest
strcmp(a, b) == 0 → equal (not a == b)
strchr / strstr → pointer or NULL

also: memcpy, memset, memmove (byte APIs)
UsePrefer
Length, copy, compare, searchstring.h functions
Safer formatted buildssnprintf
Character tests while parsingctype.h

How to Use string.h in C

Size the destination first, keep the null terminator, and compare with strcmp—not ==.

  1. Include <string.h> (and <stdio.h> if you print).
  2. Give destination arrays enough room for characters plus '\0'.
  3. Copy or append with strcpy/strcat (or bounded variants).
  4. Compare with strcmp; search with strchr/strstr and check for NULL.
PieceRequired?Role
#include <string.h>YesDeclares string and memory functions
Null terminatorYesMarks the end of every C string
Buffer sizeYes for writesMust fit text + '\0'

Pseudocode

Pseudocode
ensure dest has capacity for src (+ existing text if strcat)

strcpy(dest, src)          // copy including '\0'
len = strlen(dest)         // count until '\0'

if strcmp(a, b) == 0:
    strings are equal
else if strcmp(a, b) < 0:
    a sorts before b

p = strstr(haystack, needle)
if p != NULL:
    found at p
else:
    not found

Cheat Sheet

Core string APIs you will use constantly.

FunctionAction / return
strlen(s)Length excluding '\0'
strcpy(d, s)Copy s into d
strncpy(d, s, n)Copy at most n chars (may omit '\0')
strcat(d, s)Append s onto d
strcmp(a, b)<0 / 0 / >0
strchr(s, c)First char c, or NULL
strstr(h, n)First substring, or NULL
memset / memcpyFill / copy raw bytes

Live Preview

Pick an operation and see the result for sample strings "Hello, " and "World!".

Browser simulation of common string.h calls (same idea as the C examples below).

Result
s = "Hello, World!"
strlen(s) → 13
(does not count the final '\0')

Worked Walkthrough — Build "Hello, World!"

Trace copy, append, length, and compare on two short strings.

Calculation
str1 = "Hello, "     (7 chars + '\0')
str2 = "World!"      (6 chars + '\0')

strcpy(str3, str2)
  str3 → "World!"

strcat(str1, str2)
  str1 → "Hello, World!"
  strlen(str1) → 13

strcmp(str2, str3) → 0   (equal)

Need room in str1 for 7 + 6 + 1 = 14 bytes ✓

C Examples

Compile with gcc file.c -std=c11 -o out. Demo buffers are sized so strcpy/strcat stay safe.

Example 1 — strcpy, strcat, strlen

Copy a string, append another, then measure the result.

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

int main(void) {
    char str1[32] = "Hello, ";
    char str2[] = "World!";
    char str3[32];

    strcpy(str3, str2);
    printf("str3: %s\n", str3);

    strcat(str1, str2);
    printf("str1: %s\n", str1);
    printf("Length of str1: %zu\n", strlen(str1));

    return 0;
}

How It Works

  1. Copy. strcpy(str3, str2) writes "World!" plus '\0' into str3.
  2. Append. strcat finds the end of "Hello, " and adds "World!".
  3. Measure. strlen counts 13 characters before the terminator.
  4. Capacity. str1[32] is large enough for 13 chars + '\0'—always check this before writing.

Example 2 — Compare with strcmp

Pointer equality (a == b) is not string equality. Use strcmp.

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

int main(void) {
    char a[] = "Hello";
    char b[] = "World";
    char c[] = "Hello";

    printf("strcmp(a, b): %d\n", strcmp(a, b));
    printf("strcmp(a, c): %d\n", strcmp(a, c));

    if (strcmp(a, b) < 0) {
        printf("\"Hello\" comes before \"World\"\n");
    }

    return 0;
}

How It Works

  1. Compare bytes. strcmp walks both strings until a difference or '\0'.
  2. Unequal. 'H' vs 'W' → negative result (exact number is implementation-defined).
  3. Equal. a and c match → return 0.
  4. Test the sign. Use < 0, == 0, or > 0—not equality with -1 or 1.

Edge Cases & Pitfalls

Most string bugs are buffer and terminator mistakes.

overflow

Destination must be large enough

strcpy/strcat do not check size. Overflow is undefined behavior.

strncpy

strncpy may omit '\0'

If n cuts mid-string, add the terminator yourself, or use snprintf.

compare

Do not use == for text

a == b compares pointers. Use strcmp(a, b) == 0 for contents.

null

Never pass NULL to strlen/strcpy

These functions require valid strings. Passing NULL is undefined behavior.

Performance Notes

ApproachCostBest for
strlen / strcmpO(n) over charactersNormal short strings
strcpy / strcatO(n) copyWhen dest size is known
Repeated strlen in a loopCan add upCache the length once

For tiny strings, clarity wins. For large buffers, prefer bounded APIs and avoid rescanning the same string with strlen on every iteration.

Key Takeaways

  • Shape: C strings end with '\0'.
  • Safety: size destinations for text + terminator.
  • Compare: strcmp for contents, not ==.
  • Search: strchr/strstr return NULL on miss.

One line: string.h works on null-terminated text—know the buffer size before you write.

Frequently Asked Questions

string.h is the C standard header for null-terminated character arrays and raw memory blocks. It provides strlen, strcpy, strcat, strcmp, strncpy, strchr, strstr, memcpy, memset, and related functions used in almost every C program that handles text.
strcpy copies until the source null terminator—if the destination is too small, you get a buffer overflow. strncpy copies at most n bytes but may not add a null terminator if the source is longer than n. After strncpy, set dest[n-1] = '\0' or prefer snprintf for safer copies.
strcmp compares two strings lexicographically. It returns 0 if equal, negative if the first sorts before the second, and positive if after. Do not assume the values are exactly -1 or 1—check < 0, == 0, or > 0.
No. strlen returns the number of characters before the terminating '\0'. The string "hi" has length 2, so the array needs at least 3 bytes including the null.
strcat appends without checking remaining space. The destination must hold the existing text, the new text, and the final '\0'. Prefer strncat with a limit or snprintf to build strings safely.
strchr finds the first occurrence of a single character. strstr finds the first occurrence of a whole substring. Both return a pointer to the match or NULL if not found.

Next: time.h

Work with clocks, calendar time, and formatted dates.

time.h tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful