C String Functions

C string functions are library functions that work with null-terminated character arrays. They help you measure, copy, join, and compare strings without writing every loop yourself.

Most common string functions are declared in <string.h>, so include that header before using them.

Common Functions

Function What it does
strlen(s) Returns the number of characters before '\0'
strcpy(dest, src) Copies one string into another array
strcat(dest, src) Adds one string to the end of another string
strcmp(a, b) Compares two strings alphabetically by character codes

These functions expect valid C strings. That means each string must have a null character, and destination arrays must have enough room for the result.

Length, Copy, And Join

The strlen function measures the visible text length. The strcpy function copies a string into an array, and strcat appends more text to the end of an existing string.

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

int main(void)
{
    char message[40];

    strcpy(message, "Hello");
    strcat(message, ", ");
    strcat(message, "C learner");

    printf("Message: %s\n", message);
    printf("Length: %zu\n", strlen(message));

    return 0;
}

Output:

Message: Hello, C learner
Length: 16

The array message has room for 40 characters. The final string has 16 visible characters plus the ending '\0', so it fits safely.

Compare Strings

You cannot compare string contents with ==. For arrays, == does not check whether the text is the same. Use strcmp instead.

strcmp(a, b) returns 0 when the strings are equal. It returns a value less than 0 if a comes before b, and greater than 0 if a comes after b.

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

int main(void)
{
    char saved[] = "blue";
    char guess[] = "blue";
    char other[] = "green";

    if (strcmp(saved, guess) == 0) {
        printf("saved and guess match\n");
    }

    if (strcmp(saved, other) != 0) {
        printf("saved and other are different\n");
    }

    printf("Compare result: %d\n", strcmp(saved, other));

    return 0;
}

Output:

saved and guess match
saved and other are different
Compare result: -5

The exact positive or negative number from strcmp can vary by the characters being compared, but 0, less than 0, and greater than 0 are the important meanings.

Buffer Size Matters

C string functions do not automatically grow arrays. If you copy or append more characters than the destination can hold, your program has undefined behavior.

Before using strcpy or strcat, make sure the destination array is large enough for all visible characters and the final null character. For beginner examples, using a clearly larger array is a simple way to avoid overflow.

Quick Rules

  • Include <string.h> for standard string functions.
  • Use strlen for text length, not sizeof.
  • Use strcmp to compare string contents.
  • Leave space for '\0' when copying or joining strings.

The main takeaway is that C string functions are useful, but they still rely on correct character arrays and enough storage.