Strings
A C string is an array of char ending in a '\0' byte. The terminator is part of the value. There is no stored length, so every read walks until it finds that byte.
Detailed Explanation
"hello" is a string literal: a static array of six chars, h e l l o \0. The type is char[6]. In most expressions it decays to char *. The standard says you must not modify a string literal. On many systems the bytes live in a read-only page and a write faults. On others the write appears to work and then another "hello" in the program changes with it, because the linker merged the literals. Treat literals as const char *.
A string you own is a char array you filled and terminated. char buf[8] = "hello"; copies the six bytes including '\0' into buf. char buf[5] = "hello"; is a special case that copies five characters and no terminator — that object is not a string. char buf[] = "hello"; sizes the array to six. char *p = "hello"; does not copy; p points at the literal.
Because the length is not stored, strlen is O(n). Any API that writes a string needs the destination capacity, not just a pointer. strcpy(dest, src) does not know dest's size and will write past the end if src is longer. snprintf(dest, dest_sz, "%s", src) does know, and it always writes a terminator when dest_sz > 0. strncpy is the wrong substitute: it does not terminate when src is as long as n.
Empty string and null pointer are different. "" is a valid string of length zero, one byte of '\0'. NULL is not a string. Passing NULL to strlen is undefined behavior. An API that wants an optional string should take const char * and document that NULL means "absent", then check before every library call.
Wide strings (wchar_t, L"...") and UTF-8 in char arrays are separate layers. C's string library is byte-oriented. A UTF-8 code point may be several bytes. strlen counts bytes, not characters. For text you care about as text, pick a library. For paths, protocol tokens, and C identifiers, bytes are the right model.
Code Examples
#include <stdio.h>
#include <string.h>
int main(void)
{
char buf[8];
const char *src = "hello";
if (strlen(src) + 1 > sizeof buf) {
return 1;
}
memcpy(buf, src, strlen(src) + 1); /* include the terminator */
printf("%s %zu\n", buf, strlen(buf)); /* prints: hello 5 */
return 0;
}#include <stdio.h>
int main(void)
{
char named[] = "hi"; /* char[3]: 'h','i','\0' */
const char *lit = "hi"; /* points at a literal; do not write */
printf("%zu %zu\n", sizeof named, sizeof lit);
/* typical 64-bit: 3 8 */
char dest[4];
int n = snprintf(dest, sizeof dest, "%s", "hello");
printf("%s %d\n", dest, n); /* prints: hel 5
dest holds "hel"; n is the length that
would have been written, not the truncated one */
return 0;
}Common Gotchas
A `char` array without a `'\0'` is not a string
`strlen`, `strcpy`, and `printf("%s")` will read off the end. Size the array for the terminator.
`strcpy` and `sprintf` do not know the destination size
use `snprintf` or copy with an explicit bound and write the terminator yourself.
`strncpy` does not always terminate
when `src` is longer than `n`, `dest` is a raw character buffer. Prefer `snprintf`.
Writing through a pointer to a string literal is undefined behavior
declare `const char *s = "text";` so the compiler rejects the write.