Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C

strings and text — concept C23

A C string is a `char` array terminated by the first `\0` byte; length excludes the terminator, but the buffer must reserve space for it. String functions do not know capacity, so a missing terminator or undersized destination causes access outside an object.

Version: C23

Syntax

strings and text

A C string is a char array terminated by the first \0 byte; length excludes the terminator, but the buffer must reserve space for it. String functions do not know capacity, so a missing terminator or undersized destination causes access outside an object.

Store capacity with the buffer and, before concatenation, compute used + added + 1 with overflow checks. For data of known length, use a byte operation and append the terminator explicitly; strncpy does not guarantee termination when the source is too long.

Test an empty string, capacity-one buffer, embedded \0 byte, invalid multibyte encoding, and source/destination overlap. strlen searches for a terminator without knowing a bound, so call it only on an already established C string.

Examples

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

int main(void) { const char text[] = "C\0X"; printf("%zu %zu\n", strlen(text), sizeof text); return 0; }

Result

1 4

A C string is a `char` array terminated by the first `\0` byte; length excludes the terminator, but the buffer must reserve space for it. String functions do not know capacity, so a missing terminator or undersized destination causes access outside an object.

Sources