C has no built-in string type. Text is a null-terminated array of char ending with '\0'. Use <string.h> for common operations.
Literals and buffers
const char *msg = "hello"; /* read-only literal */
char buf[32] = "copy me"; /* mutable buffer */
Never write through a string literal pointer—storage is read-only.
Common functions
strlen(s)— length excluding null terminatorstrcpy(dst, src)— copy (ensure dst is large enough)strcmp(a, b)— compare; returns 0 if equal
Prefer strncpy, snprintf, or bounded copies in production code.
Important interview questions and answers
- Q: Why crash on strcpy without space?
A: Buffer overflow—writes past the destination array into adjacent memory. - Q: char* vs char[]?
A: Pointer may aim at a literal (often read-only); array is mutable storage on stack or static memory.
Self-check
- What character terminates a C string?
- Which function measures string length?
Pitfall: strcpy without bounds checking causes buffer overflows—prefer snprintf or bounded copies in production code.
Interview prep
- How are C strings stored?
Contiguous
chararrays ending with a null terminator\0.- strcpy danger?
Writes until source null byte with no destination size check—classic buffer overflow source.