C String Operations

Beginner
6 min read
Updated: Nov 2026
1 example

What Is String Operations?

The string.h header provides the everyday operations: strlen, strcpy/strncpy, strcat, and strcmp. Always ensure the destination buffer is large enough — these functions do not grow arrays.

Remember
strlen(s)
strcpy(dst, src)   /* needs room for src + \0 */
strcmp(a, b)       /* 0 if equal */
strcat(dst, src)   /* appends; needs spare room */

Cheat Sheet

TopicDetail
Lengthstrlen(s)
Copystrcpy / safer bounded copies
Comparestrcmp — 0 means equal
Searchstrchr, strstr

C Examples

Compile with gcc file.c -std=c11 -Wall -o out. Next: Array of Strings.

Example 1

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

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

    strcpy(b, a);
    strcat(b, ", C");
    printf("b: %s (len %zu)\n", b, strlen(b));
    printf("cmp: %d\n", strcmp(a, "Hello"));
    return 0;
}

How It Works

  1. strcpy copies "Hello" into b.
  2. strcat appends ", C" after the existing terminator.
  3. strcmp returns 0 because a still equals "Hello".

Edge Cases & Pitfalls

overflow

Destination too small

strcpy/strcat do not check size — overflow is undefined behavior.

strncmp

Partial compare

Use strncmp when you only care about a prefix.

Key Takeaways

  • Include: <string.h> for these helpers.
  • Capacity: you manage buffer sizes.
  • Catalog: see the Function Index for every API.

One line: string.h gives length, copy, concat, and compare — you supply safe buffers.

Frequently Asked Questions

Only if dst is large enough. Prefer bounded APIs or check lengths yourself before copying.
Open /c/string/function for the string.h function index on CodeToFun.
Array of Strings shows tables of text with char arrays of arrays or pointers.

Next: Array of Strings

Continue the C strings track.

Continue →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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.

12 people found this page helpful