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.
char buffer[] = "hello"; /* writable copy, 6 bytes */ const char *name = "hello"; /* points at the literal; read-only */
Try it
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.
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
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 ischar[N], but treating it asconstlets the compiler catch accidental writes (GCC's-Wwrite-strings). ==compares addresses, not contents. Usestrcmp,strncmp, ormemcmp.- Assignment does not copy.
char a[10]; a = "x";is an error. Usestrcpy,snprintf, ormemcpy. - Prefer
snprintffor building strings: it always terminates (when the size is above zero) and reports the length it wanted. strncpyis not a safestrcpy. It pads with zeros and may leave the buffer unterminated; seestrcpy.- Encoding.
charstrings are bytes. UTF-8 text works as long as you do not index by "character";strlencounts bytes. C23 addschar8_tandu8""literals. charsignedness matters when passing characters to<ctype.h>: cast tounsigned charfirst.
Related
strlen,strcpy,strcmp,printfformat specifiers, Literals- Lessons: Strings, String functions, String safety