0
0
Cprogramming~10 mins

Storage size overview in C - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Storage size overview
Declare variable
Allocate memory based on type
Check size with sizeof()
Use size for operations or info
End
This flow shows how a variable's storage size is determined by its type and checked using sizeof() in C.
Execution Sample
C
int a;
char b;
float c;
printf("Size of a: %zu\n", sizeof(a));
printf("Size of b: %zu\n", sizeof(b));
printf("Size of c: %zu\n", sizeof(c));
This code declares variables of different types and prints their storage sizes in bytes.
Execution Table
StepActionVariable/Typesizeof() Result (bytes)Output
1Declare variableint a--
2Declare variablechar b--
3Declare variablefloat c--
4Evaluate sizeof(a)int a4Size of a: 4
5Evaluate sizeof(b)char b1Size of b: 1
6Evaluate sizeof(c)float c4Size of c: 4
7End---
💡 All variables declared and their sizes printed; program ends.
Variable Tracker
VariableStartAfter sizeof()
a (int)declaredsize = 4 bytes
b (char)declaredsize = 1 byte
c (float)declaredsize = 4 bytes
Key Moments - 2 Insights
Why does sizeof(char) return 1 even though char can store only a small letter?
sizeof(char) returns 1 because it measures size in bytes, and by definition, a char occupies exactly 1 byte (the smallest addressable memory unit). See execution_table rows 2 and 5.
Why do int and float both have size 4 bytes here?
On this system, both int and float types use 4 bytes of memory. This can vary by system but is common. See execution_table rows 1, 3, 4, and 6.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the size of variable 'b' at step 5?
A1 byte
B4 bytes
C2 bytes
D8 bytes
💡 Hint
Check the sizeof() Result column at step 5 in execution_table.
At which step does the program print the size of the float variable?
AStep 4
BStep 6
CStep 5
DStep 7
💡 Hint
Look for the output related to 'Size of c' in execution_table.
If the int type was 8 bytes instead of 4, how would the output at step 4 change?
ASize of a: 4
BSize of a: 1
CSize of a: 8
DSize of a: 2
💡 Hint
Consider how sizeof() reflects the actual memory size of the type.
Concept Snapshot
Storage size overview in C:
- Use sizeof(type_or_variable) to get size in bytes.
- char is always 1 byte.
- int and float sizes depend on system (commonly 4 bytes).
- Sizes help understand memory use and data limits.
- sizeof returns size_t, use %zu to print.
- Sizes can vary by platform.
Full Transcript
This visual execution trace shows how C determines storage sizes of variables using the sizeof operator. Variables of types int, char, and float are declared. Then sizeof is used to find their sizes in bytes. The trace shows int and float both have size 4 bytes, and char has size 1 byte on this system. The variable tracker records these sizes after sizeof is called. Key moments clarify why char is 1 byte and why int and float sizes can vary by system. The quiz tests understanding of sizes at specific steps and how changes affect output. This helps beginners see how storage size relates to variable types and memory in C.