C Strings in C

From the C cheat sheet · Strings · verified Jul 2026

C Strings

char arrays and the string library.

c
// Quick Reference
char s[] = "hello";       // char array + '\0'
#include <string.h>
strlen(s);  strcmp(a, b);
strcpy(dst, src);  snprintf(buf, n, "%d", x);
💡 C strings are char arrays terminated by '\0'; strlen counts up to (not including) it.
⚡ Compare strings with strcmp (== compares pointers, not contents).
📌 Prefer snprintf/strncpy over sprintf/strcpy - the bounded versions prevent overflows.
🟢 A string literal ("x") is read-only - modifying it via a char* is undefined behavior.
stringsstring-h
Back to the full C cheat sheet