devnotes.

Strings and the string.h Library

C has no dedicated string type — a string is simply a char array ending with a special null terminator byte, '\0', which marks where the string stops.

#include <string.h>

char greeting[20] = "Hello"; // stored as {'H','e','l','l','o','\0', ...}

printf("%zu\n", strlen(greeting));  // 5 — length up to (not including) '\0'

char dest[20];
strcpy(dest, greeting);              // copies greeting into dest, including '\0'
strcat(dest, ", world!");            // appends onto the end of dest

if (strcmp(dest, "Hello, world!") == 0) {
    printf("Strings match!\n");
}

strlen scans forward from the start of the array until it finds '\0', so it doesn't count the terminator itself — this also means an unterminated char array (missing '\0') causes strlen and every other string function to read past the array's real end, a serious and common bug.

strcmp returns 0 when strings are equal, not 1/true as you might expect from other languages — comparing strings with == instead compares the two pointers, not their contents, which almost never does what a beginner wants.

strcpy and strcat perform no bounds checking — if dest isn't large enough to hold the result, they'll overflow it and corrupt adjacent memory. Prefer the safer, length-limited alternatives strncpy and strncat (always explicitly null-terminate the result afterward, since these bounded functions don't guarantee it) whenever the source's length isn't already known and controlled.

C Strings and the string.h Library | DevNotes