Strings in C: char Arrays, the Null Terminator and the Bugs to Avoid
Strings in C with tested examples: char arrays and the \0 terminator, strlen vs sizeof, strcmp, strcpy vs snprintf, strtok and reading a string with spaces.
By Avinash KoriPublished Updated 9 min read
On this page
C has no string type. What C calls a string is a convention: a sequence of char values in memory that ends with a zero byte, '\0'. Every string function in the standard library, from printf("%s") to strlen, depends on that zero being there, and almost every string bug in C comes from it being missing, misplaced or overwritten.
This guide explains how strings are stored, how to read, copy and compare them safely, and what goes wrong when you don't. Each program was run with Clang 21 in the online C Compiler and with GCC 11; the outputs are real, and you can paste any example into the compiler to experiment.
A string is a char array ending in \0
#include <stdio.h>
#include <string.h>
int main(void) {
char word[] = "hello";
char buffer[20] = "hi";
printf("sizeof word = %zu, strlen(word) = %zu\n", sizeof word, strlen(word));
printf("sizeof buffer = %zu, strlen(buffer) = %zu\n", sizeof buffer, strlen(buffer));
printf("last byte of word is %d\n", word[5]);
return 0;
}sizeof word = 6, strlen(word) = 5
sizeof buffer = 20, strlen(buffer) = 2
last byte of word is 0The literal "hello" has five letters but occupies six bytes: the compiler adds the terminator. Two numbers describe every string, and mixing them up causes most of the trouble:
sizeofis the size of the array, fixed when it is declared. It includes the terminator and any unused space.strlenis the length of the text: it counts characters until it finds'\0', which is not counted.
A buffer of N bytes can hold at most N - 1 characters of text. Forgetting the extra byte is the classic off-by-one error.
Not every char array is a string. char letters[3] = {'a', 'b', 'c'}; has no terminator, so passing it to printf("%s") or strlen reads past the end until a zero byte happens to turn up. If you must print such an array, give the length explicitly: printf("%.3s", letters).
String literals are read-only
These two declarations look alike but behave differently:
#include <stdio.h>
int main(void) {
char editable[] = "cat";
const char *literal = "cat";
editable[0] = 'b';
printf("%s %s\n", editable, literal);
return 0;
}bat catchar editable[] = "cat" creates a new four-byte array and copies the text into it, so you can change it. const char *literal = "cat" points at the literal itself, which lives in read-only memory. C allows writing char *literal = "cat" without const, but modifying it through that pointer is undefined behaviour: on a PC it usually crashes with a segmentation fault. Declaring such pointers const lets the compiler catch the mistake. If its message isn't clear, the guide to C compiler errors explains the common ones line by line.
Reading strings: scanf versus fgets
scanf("%s") reads one word: it stops at the first space, tab or newline. fgets reads a whole line, including spaces, and keeps the newline at the end:
#include <stdio.h>
#include <string.h>
int main(void) {
char first[32], full[64];
scanf("%31s", first);
getchar(); /* skip the rest of the line: the '\n' */
fgets(full, sizeof full, stdin);
printf("scanf read: [%s]\n", first);
printf("fgets read: [%s]\n", full);
full[strcspn(full, "\n")] = '\0';
printf("trimmed: [%s]\n", full);
return 0;
}With Ada on the first line of input and Ada Lovelace on the second, it prints:
scanf read: [Ada]
fgets read: [Ada Lovelace
]
trimmed: [Ada Lovelace]Three details matter here. The width in %31s stops scanf from writing more than 31 characters into a 32-byte array; a bare %s has no limit and can overflow the buffer. The getchar() consumes the newline scanf leaves behind; without it, fgets would read that empty rest of the line. And full[strcspn(full, "\n")] = '\0' removes the newline fgets keeps, and does nothing if there is none. Our guide to reading input in C and C++ goes deeper into mixing scanf and fgets.
Never use gets(). It cannot know the size of the buffer, so it was removed from the language in C11. Modern compilers reject it: Clang 21 reports "call to undeclared function 'gets'".
Comparing strings: == compares addresses
#include <stdio.h>
#include <string.h>
int main(void) {
char typed[16] = "admin";
const char *expected = "admin";
printf("typed == expected: %s\n", typed == expected ? "true" : "false");
printf("strcmp(typed, expected): %d\n", strcmp(typed, expected));
printf("strcmp(\"apple\", \"banana\") < 0: %s\n", strcmp("apple", "banana") < 0 ? "true" : "false");
return 0;
}typed == expected: false
strcmp(typed, expected): 0
strcmp("apple", "banana") < 0: true== on two strings compares two addresses, and typed and expected are stored in different places, so the answer is false even though the text is identical. strcmp compares the characters and returns 0 when the strings are equal, a negative number when the first sorts before the second, and a positive number otherwise. Only the sign is meaningful: don't test for -1 or 1. A common bug is if (strcmp(a, b)), which is true when the strings are different.
Copying safely: snprintf instead of strcpy
strcpy and strcat copy until the terminator, however small the destination is:
#include <stdio.h>
#include <string.h>
int main(void) {
char name[4];
strcpy(name, "Alexander");
printf("%s\n", name);
return 0;
}Clang warns at compile time that "'strcpy' will always overflow; destination buffer has size 4, but the source string has length 10". What happens at run time depends on the platform. GCC on Ubuntu builds with fortified string functions, so the program stops with *** buffer overflow detected ***: terminated. The WebAssembly build of the online compiler has no such check and prints Alexander, having silently overwritten the memory after name. The second outcome is the dangerous one: in real programs, buffer overflows like this are a classic source of security holes.
Use snprintf instead. It takes the buffer size, never writes past it, always adds the terminator, and returns how many characters it wanted to write, so you can detect truncation:
#include <stdio.h>
#include <string.h>
int main(void) {
char small[8];
int needed = snprintf(small, sizeof small, "%s-%d", "order", 12345);
printf("result: [%s]\n", small);
printf("needed %d chars, buffer holds %zu\n", needed, sizeof small - 1);
char dest[6];
strncpy(dest, "abcdefgh", sizeof dest - 1);
dest[sizeof dest - 1] = '\0';
printf("strncpy + terminator: [%s]\n", dest);
return 0;
}result: [order-1]
needed 11 chars, buffer holds 7
strncpy + terminator: [abcde]strncpy looks like the safe version of strcpy, but it does not add a terminator when the source is too long, which is why the example sets the last byte by hand. In most code, snprintf(dest, sizeof dest, "%s", src) is the simpler safe copy.
Working with characters
A string is an array, so you can walk it with an index or a pointer until you reach '\0'. The functions in <ctype.h> classify and convert single characters:
#include <ctype.h>
#include <stdio.h>
int main(void) {
char text[] = "Hello, C World 2026!";
int letters = 0, digits = 0, spaces = 0;
for (char *p = text; *p != '\0'; p++) {
if (isalpha((unsigned char)*p)) letters++;
else if (isdigit((unsigned char)*p)) digits++;
else if (isspace((unsigned char)*p)) spaces++;
*p = (char)toupper((unsigned char)*p);
}
printf("%s\nletters=%d digits=%d spaces=%d\n", text, letters, digits, spaces);
return 0;
}HELLO, C WORLD 2026!
letters=11 digits=4 spaces=3The (unsigned char) casts are not decoration: passing a negative char (any byte above 127 on platforms where char is signed) to the ctype functions is undefined behaviour. They only understand single bytes, so for UTF-8 text with accents or emoji you need a Unicode library. The Word & Character Counter shows how character and byte counts differ for such text.
Converting strings to numbers (and back)
atoi is the function most tutorials show, but it cannot tell "0" from "abc": both give 0. Use strtol, which tells you where it stopped, and check the end pointer.
#include <stdio.h>
#include <stdlib.h>
int main(void) {
const char *inputs[] = {"42", "3.5", "12abc", "abc"};
for (int i = 0; i < 4; i++) {
char *end;
long n = strtol(inputs[i], &end, 10);
if (end == inputs[i]) {
printf("%-6s -> not a number\n", inputs[i]);
} else if (*end != '\0') {
printf("%-6s -> %ld, but \"%s\" is left over\n", inputs[i], n, end);
} else {
printf("%-6s -> %ld\n", inputs[i], n);
}
}
char text[32];
snprintf(text, sizeof text, "%d items, %.2f total", 7, 19.5);
puts(text);
return 0;
}42 -> 42
3.5 -> 3, but ".5" is left over
12abc -> 12, but "abc" is left over
abc -> not a number
7 items, 19.50 totalThe other direction, number to string, is snprintf with a buffer size, as in the last lines above. Use strtod for floating-point numbers; it works the same way as strtol.
Splitting a string with strtok
strtok cuts a string at any of the delimiter characters you give it. It modifies the string by writing \0 over each delimiter, so it must be a char array, not a string literal. Empty fields are skipped, which is why green,,blue yields no empty token.
#include <stdio.h>
#include <string.h>
int main(void) {
char line[] = "red,green,,blue";
for (char *tok = strtok(line, ","); tok != NULL; tok = strtok(NULL, ",")) {
printf("[%s]\n", tok);
}
return 0;
}[red]
[green]
[blue]strtok keeps hidden state between calls, so it is not safe in threads or when two splits are interleaved. On POSIX systems use strtok_r; on Windows, strtok_s.
Arrays of strings
There are two ways to store several strings, and they behave differently. An array of pointers stores addresses of read-only literals and takes only the space of the pointers. A two-dimensional char array reserves a fixed-size row for each string, and every row can be modified.
#include <stdio.h>
#include <string.h>
int main(void) {
const char *days[] = {"Mon", "Tue", "Wed"};
size_t count = sizeof days / sizeof days[0];
for (size_t i = 0; i < count; i++) {
printf("%zu: %s (%zu chars)\n", i, days[i], strlen(days[i]));
}
char names[2][8] = {"Ada", "Linus"};
strcpy(names[0], "Grace");
printf("%s %s\n", names[0], names[1]);
printf("sizeof names = %zu\n", sizeof names);
return 0;
}0: Mon (3 chars)
1: Tue (3 chars)
2: Wed (3 chars)
Grace Linus
sizeof names = 16names takes 16 bytes (2 rows of 8) however short the names are, and "Grace" fits because it is shorter than 8 bytes. Copying a longer name would overflow the row, so check the length first.
The string functions you will use most
| Function | What it does | Watch out for |
|---|---|---|
strlen(s) | Length of the text, without \0 | Walks the whole string each call |
strcmp(a, b) | Compares; 0 means equal | Only the sign of the result matters |
snprintf(d, n, fmt, ...) | Formats into a buffer of size n | Returns the length it wanted |
strncpy(d, s, n) | Copies at most n bytes | May leave d unterminated |
strcat(d, s) | Appends s to d | No size check; prefer snprintf |
strchr(s, c) / strstr(s, t) | Finds a character / substring | Returns NULL if not found |
strcspn(s, set) | Length before any char in set | Handy for trimming newlines |
strtol(s, &end, 10) | Converts text to a number | Check end to detect bad input |
Common questions about strings in C
Is there a string type in C?
No. C has no built-in string type: a string is an array of char that ends with '\0'. Everything the standard library offers (strlen, strcmp, strcpy) works on that convention.
What is the difference between char *s and char s[]?
char s[] = "hi"; makes a modifiable copy of the text in an array. char *s = "hi"; points to a read-only literal, so writing through it is undefined behaviour; declare it as const char *s.
Why does strlen return a different number than sizeof?
sizeof is the size of the whole array, including unused space and the terminator. strlen counts the characters before the first '\0'. For a pointer, sizeof is only the size of the pointer.
How do I take a string with spaces as input?
Use fgets(buf, sizeof buf, stdin), which reads a whole line, and remove the newline afterwards. scanf("%s") stops at the first space. The reading input in C and C++ guide covers both.
How do I compare strings ignoring case?
Standard C has no function for it. POSIX provides strcasecmp (_stricmp on Windows), or you can lower-case both strings with tolower first.
Summary
- A C string is a
chararray terminated by'\0'. Leave room for it: a buffer ofNbytes holdsN - 1characters. sizeofgives the array size,strlenthe text length.- String literals are read-only; point to them with
const char *. - Read words with
scanf("%31s")(always with a width) and lines withfgets, then trim the newline. - Compare with
strcmp, never==. - Copy and build strings with
snprintf; avoidstrcpy,strcatandgets.
To go further, see how strings relate to pointers in C, and run the examples above in the C Compiler with your own input. If you would rather let the language manage memory, std::string in the C++ Compiler grows automatically and compares with ==.

Written by
Avinash Kori
Founder of Devtoolry
Avinash is a full-stack web developer from Surat, India, with eight years of experience in PHP, Laravel and Angular. He built Devtoolry so developers can solve everyday problems quickly with free tools that run entirely in the browser, and writes practical guides on the formats, encodings and techniques behind them.
Connect on LinkedInTry it yourself
The free tools used in this article. They run in your browser, so nothing you paste is uploaded.
C Compiler
Compile and run C code online with Clang 21, a GCC-compatible compiler: pick C89 (ANSI C), C99, C11, C17 or C23 and add stdin input. Your code stays private.
Runs in your browserC++ Compiler
Run C++ code online for free: compile C++20 with Clang 21 and libc++, type stdin input and see the output. It runs in your browser, so your code stays private.
Runs in your browserWord & Character Counter
Count words, characters, sentences and paragraphs online with reading and speaking time, keyword density, SMS parts and limits for X, Instagram and meta tags.
Runs in your browser