CStrings as Char Arrays

Strings as Char Arrays

C has no dedicated string type
Unlike most modern languages, C does not have a built-in String or string type. A string in C is simply an array of char values, with one special rule: the array must end with a null byte, '\0', marking where the text stops. Everything C's string functions do is built on top of this one convention — there is no hidden length field anywhere.
The Null Terminator
The null terminator, written '\0', is a byte with the value zero. It is not printable and is not the character '0' (which has a completely different, nonzero value) — it is a sentinel value that says "the string ends here." Every C string function that operates on char * reads forward, byte by byte, until it hits this terminator.

C
char word[6] = {'H', 'e', 'l', 'l', 'o', '\0'};
// 5 visible characters + 1 null terminator = 6 bytes total
String Literals Include the Terminator Automatically
When you write a string literal like "hello", the compiler automatically appends the null terminator for you — you never have to add it yourself when using this shorthand.

C
char greeting[] = "hello"; // compiler makes this 6 bytes: 'h','e','l','l','o','\0'

// Exactly equivalent, spelled out the long way:
char greeting2[] = {'h', 'e', 'l', 'l', 'o', '\0'};
Sizing a String Buffer Correctly
Always leave room for the terminator
A common off-by-one mistake is forgetting that the null terminator needs its own byte of space. A buffer declared as char buf[5] can only safely hold a string of 4 visible characters plus the terminator — not 5 characters. Writing a 5-character word into it leaves no room for the '\0', and whatever function reads that buffer afterward will keep reading past its end looking for a terminator that never appears — undefined behavior, and a classic buffer overflow.

C
char buf[5];             // can hold at most a 4-character string + '\0'

strcpy(buf, "abcd");     // OK: 4 chars + terminator = 5 bytes, fits exactly
/* strcpy(buf, "abcde"); */ // BUG: 5 chars + terminator = 6 bytes, overflows buf!

char safer[6];           // needed if you actually want to store "abcde"
strcpy(safer, "abcde");  // OK: 5 chars + terminator = 6 bytes, fits exactly
strlen() Excludes the Terminator
The strlen() function (from <string.h>) counts only the visible characters in a string — it does not include the null terminator in its count. This means the number of bytes a string actually occupies in memory is always strlen(str) + 1.

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

int main(void) {
    char word[] = "hello";

    printf("strlen: %zu\n", strlen(word));  // 5 -- does NOT count '\0'
    printf("sizeof: %zu\n", sizeof(word));  // 6 -- DOES count '\0' (whole array)
    return 0;
}
  • C has no dedicated string type -- a "string" is just a char array ending in '\0'

  • String literals like "hello" automatically get the null terminator appended by the compiler

  • A buffer of size N can hold at most N-1 visible characters plus the terminator

  • strlen() counts visible characters only; the actual memory used is strlen(str) + 1

  • Forgetting to leave room for the terminator is one of the most common C bugs -- always size buffers with the terminator in mind