Learn C
#include "arraysstrings/strings.h"

Strings as char Arrays in C

#concept

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.

#how it works
1

Null terminator rule

Every string ends with '\0'; a 5-letter word needs char[6]. strlen counts characters excluding the terminator.

2

Literals are read-only

char *p = "hi"; points at immutable storage — modify a copy in a writable array instead.

3

Size for content + 1

Buffer declarations must add the terminator: 15 visible characters need char[16] at minimum.

4

Arrays decay to char*

Passing a string hands functions a pointer — length information is lost, so conventions (terminator, explicit size) carry it.

#example
#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;
}
Output
5
h
1word holds h,e,l,l,o,\0 in a 16-byte writable buffer — strlen stops at the terminator and reports 5, printed portably with %zu.
2word[0] is the character h, printed with %c — indexing works exactly like any array.
3Both lines confirm array storage plus terminator discipline in one glance.
#use cases
Sizing every buffer for content plus the terminator
Copying literals into writable arrays before any modification
Reading legacy code that manipulates raw char arrays
Treating the terminator as part of every length calculation

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in C yourself — building beats watching.