Skip to content
Tutorials Center
My learning

Topics

C Reference ยท Types and Literals

Strings in C

Reference for strings in C: null-terminated char arrays, string literals vs arrays, strlen vs sizeof, copying safely, comparing, and why literals are read-only.

C has no string type. A string is a sequence of char values ending with a zero byte, the null terminator '\0'. Functions such as strlen, printf("%s") and strcpy read until they find that terminator, so a missing one makes them run off the end of the array.

You meet strings in two forms. A string literal like "hello" is an unnamed array of char (six bytes here) that you must not modify. A char array initialized from a literal is your own writable copy.

C
char buffer[] = "hello";       /* writable copy, 6 bytes */
const char *name = "hello";    /* points at the literal; read-only */

Try it

C Length, size and the terminator

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

C Copy, compare, concatenate safely

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Syntax

C
char name[N];                      /* room for N-1 characters + '\0' */
char name[] = "text";              /* array sized by the literal (5 bytes) */
char name[N] = "text";             /* remaining bytes are zero */
const char *name = "text";         /* pointer to a string literal */
#include <string.h>                /* strlen, strcpy, strcmp, ... */
Notation Description
"abc" An array of char of length 4: 'a', 'b', 'c', '\0'. Adjacent literals are joined: "ab" "cd" is "abcd".
'\0' The null character; value 0. Not the same as '0', which is 48 in ASCII.
strlen(s) Number of characters before the terminator.
sizeof arr Size of the whole array in bytes, terminator and spare space included.

Errors / undefined behavior: modifying a string literal is undefined behavior (often a crash). Writing past the end of the array, calling a string function on an array without a terminator, or copying a string into a buffer that is too small is undefined behavior.

Usage notes

  • Size for the terminator. A string of 10 characters needs 11 bytes. Off-by-one errors here are the classic buffer overflow.
  • Use const char * for literals. In C the literal's type is char[N], but treating it as const lets the compiler catch accidental writes (GCC's -Wwrite-strings).
  • == compares addresses, not contents. Use strcmp, strncmp, or memcmp.
  • Assignment does not copy. char a[10]; a = "x"; is an error. Use strcpy, snprintf, or memcpy.
  • Prefer snprintf for building strings: it always terminates (when the size is above zero) and reports the length it wanted.
  • strncpy is not a safe strcpy. It pads with zeros and may leave the buffer unterminated; see strcpy.
  • Encoding. char strings are bytes. UTF-8 text works as long as you do not index by "character"; strlen counts bytes. C23 adds char8_t and u8"" literals.
  • char signedness matters when passing characters to <ctype.h>: cast to unsigned char first.