The literal becomes a char array
"CIAO" has four visible characters, but five memory cells are required.
C programming · Lesson
Observe a C string as a sequence of characters in memory and discover why one extra cell is needed for the terminator.
In C, a string is a char array terminated by byte 0. The \0 terminator lets functions such as printf with %s know where to stop.
"CIAO" → ['C','I','A','O','\0'] → %s reads until \0
The flowchart shows the logical string value; the String Memory Explorer below shows the individual char cells and the \0 terminator actually required in C.
Each cell contains one char. The \0 terminator occupies a real cell and tells C functions where the string ends.
"CIAO" has four visible characters, but five memory cells are required.
parola[0] contains C. Just like numeric arrays, the first index is 0.
Cell parola[4] contains byte 0. It is not a printable character: it marks the end of the C string.
%s reads C, I, A, O and stops when it reaches the terminator. The console prints CIAO.
Run C executes only the example shown on the left, using stdin on the right. The coding challenge below has a separate editor and test button.
1#include <stdio.h>
2
3int main(void)
4{
5 char parola[] = "CIAO";
6 printf("%s\n", parola);
7 return 0;
8}
Read a word (max 50 chars); print bytes needed including null terminator.
This is a different program from lab 1. Complete the starter code: Check all test cases compiles this editor and tests every input, including boundary cases.
How many cells does char parola[] = "CIAO" occupy?
Challenge 1 / 5 · Understanding
Progress: 0 / 5 · correct: 0
ConceptStep separates what is required from what is recommended or useful as reinforcement. Only required prerequisites block your progress.
Relationships: solid line = required · dashed line = recommended · dotted line = reinforcement.