Strings as char Arrays in C
C has no string type — a string is a char array terminated by a '\0' null character. "hi" occupies 3 bytes (h, i, \0), and that terminator is how every string routine knows where text ends. Forgetting room for it is the original buffer overflow: char name[5] = "hello"; needs 6.
String literals live in read-only memory — writing through char *p = "hi"; is undefined behavior. Copy into a writable array (char buf[16]; strcpy(...)) before modifying, and size every buffer for content plus terminator.
Null terminator rule
Every string ends with '\0'; a 5-letter word needs char[6]. strlen counts characters excluding the terminator.
Literals are read-only
char *p = "hi"; points at immutable storage — modify a copy in a writable array instead.
Size for content + 1
Buffer declarations must add the terminator: 15 visible characters need char[16] at minimum.
Arrays decay to char*
Passing a string hands functions a pointer — length information is lost, so conventions (terminator, explicit size) carry it.
#include <stdio.h>#include <string.h> int main(void) { char word[16] = "hello"; printf("%zu\n", strlen(word)); printf("%c\n", word[0]); return 0;}Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Practice what you learned
Re-implement each visualized algorithm in C yourself — building beats watching.