C Library - <string.h>

Last Updated : 11 Jul, 2026

The <string.h> header file is a standard C library that provides functions for manipulating C-style strings and memory blocks. It simplifies common string handling tasks in C programs.

  • Supports operations such as concatenation, comparison, searching, and tokenization.
  • Includes functions for copying, moving, comparing, and initializing memory.

Example: Using <string.h>

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

int main()
{
    char str1[20] = "Geeksfor";
    char str2[20] = "Geeks";

    printf("Before Concatenation\n");
    printf("String: %s\n", str1);
    printf("Length: %lu\n", strlen(str1));

    strcat(str1, str2);

    printf("\nAfter Concatenation\n");
    printf("String: %s\n", str1);
    printf("Length: %lu\n", strlen(str1));

    return 0;
}

Output
Before Concatenation
String: Geeksfor
Length: 8

After Concatenation
String: GeeksforGeeks
Length: 13

Explanation

  • strlen() returns the length of the string.
  • strcat() appends the second string to the first string.

Syntax

#include <string.h>

Common <string.h> Functions

The <string.h> header provides several functions for string and memory manipulation.

Function Name

Function Description

strlen()

Returns the length of the string.

strcpy()

Copy one string to another.

strncpy()

Copy first n characters of one string to another.

strcat()

Concatenates two strings.

strncat()

Concatenates first n characters of one string to another.

strcmp()

Compares two strings.

strncmp()

Compares first n characters of two strings.

strchr()

Find the first occurrence of the given character in the string.

strstr()

Find the given substring in the string.

strcspn()

Returns the span of the source string not containing any character of the given string.

strspn()

Returns the span of the source string containing only the characters of the given string.

strtok()

Split the given string into tokens based on some character as a delimiter.

memset()

Initialize a block of memory with the given character.

memcpy()

Copy two blocks of memory.

memmove()

Moves two blocks of memory.

Note: The <string.h> header provides many additional string and memory manipulation functions beyond those listed above.

Using Common <string.h> Functions

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

// defining the common size of the string arrays
#define size 50

int main()
{
    char destString[size] = "Geeksfor";
    char sourceString[size] = "Geeks";
    char tempDestString[size];

    printf("Length of Destination String: %d\n",
           strlen(destString));

    // copying sourceString to tempDestString using strcpy()
    strcpy(tempDestString, sourceString);
    printf("tempDestString after strcpy(): %s\n",
           tempDestString);

    // concatenating source to destination using strcat()
    strcat(destString, sourceString);
    printf("destString after Concatenation: %s\n",
           destString);

    // comparison using strcmp()
    printf("Comparing destString with sourceString: %d\n",
           strcmp(destString, sourceString));

    printf("Comparing first 5 characters: %d\n",
           strncmp(destString, sourceString, 5));

    // searching substring using strstr()
    printf("Searching sourceString in destString: %s\n",
           strstr(destString, sourceString));

    return 0;
}

Output
Length of Destination String: 8
tempDestString after strcpy(): Geeks
destString after Concatenation: GeeksforGeeks
Comparing destString with sourceString: 102
Comparing first 5 characters: 0
Searching sourceString in destString: GeeksforGeeks

Applications of <string.h>

The <string.h> library is commonly used for:

  • Processing user input.
  • Comparing and searching strings.
  • Copying and concatenating strings.
  • Parsing delimited text.
  • Manipulating memory blocks efficiently.
Comment