Bird
Raised Fist0
Cprogramming~10 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. Which of the following data types typically uses the least amount of memory in C?
easy
A. int
B. char
C. float
D. double

Solution

  1. Step 1: Recall typical sizes of basic data types

    In C, char usually takes 1 byte, int 4 bytes, float 4 bytes, and double 8 bytes.
  2. Step 2: Compare sizes to find the smallest

    Since 1 byte is less than 4 or 8 bytes, char uses the least memory.
  3. Final Answer:

    char -> Option B
  4. Quick Check:

    char = 1 byte [OK]
Hint: Remember char is 1 byte, smallest basic type in C [OK]
Common Mistakes:
  • Confusing char with int size
  • Assuming float is smaller than char
  • Thinking double is smallest
2. Which of the following is the correct way to declare a variable of type double in C?
easy
A. double num;
B. double: num;
C. num double;
D. double-num;

Solution

  1. Step 1: Recall C variable declaration syntax

    In C, the type comes first, then the variable name, ending with a semicolon.
  2. Step 2: Check each option for correct syntax

    double num; follows this rule: double num;. Others have invalid syntax like colon, reversed order, or invalid characters.
  3. Final Answer:

    double num; -> Option A
  4. Quick Check:

    Type before name, semicolon ends declaration [OK]
Hint: Type first, then variable name, end with semicolon [OK]
Common Mistakes:
  • Using colon instead of semicolon
  • Reversing type and variable name
  • Using invalid characters like dash
3. Consider the following C code snippet:
printf("%zu", sizeof(long));

What is the expected output on a typical 64-bit system?
medium
A. 8
B. 4
C. 2
D. 16

Solution

  1. Step 1: Understand sizeof operator on 64-bit systems

    On most 64-bit systems, long is 8 bytes (64 bits).
  2. Step 2: Match the output of sizeof(long)

    The sizeof operator returns the size in bytes, so it returns 8.
  3. Final Answer:

    8 -> Option A
  4. Quick Check:

    long = 8 bytes on 64-bit [OK]
Hint: On 64-bit, long is usually 8 bytes [OK]
Common Mistakes:
  • Assuming long is 4 bytes on 64-bit
  • Confusing bits with bytes
  • Using wrong format specifier in printf
4. The following code tries to print the size of a short variable but causes a compilation error:
short s;
printf("%d", sizeof(s));

What is the main reason for the error?
medium
A. Variable s is not initialized
B. Missing semicolon after short s
C. Cannot use sizeof on variables
D. Using wrong format specifier for sizeof result

Solution

  1. Step 1: Identify the type returned by sizeof

    The sizeof operator returns a value of type size_t, which is an unsigned integer type.
  2. Step 2: Check printf format specifier compatibility

    The format specifier %d expects an int, but sizeof returns size_t. The correct specifier is %zu.
  3. Final Answer:

    Using wrong format specifier for sizeof result -> Option D
  4. Quick Check:

    Use %zu for sizeof output [OK]
Hint: Use %zu to print sizeof results safely [OK]
Common Mistakes:
  • Using %d instead of %zu for sizeof
  • Thinking sizeof needs variable initialization
  • Missing semicolon (actually present)
5. You want to store 1000 integers in an array on a 32-bit system. If int is 4 bytes, how much memory in bytes will the array consume?
hard
A. 2000 bytes
B. 1000 bytes
C. 4000 bytes
D. 8000 bytes

Solution

  1. Step 1: Identify size of one integer

    On a 32-bit system, int typically uses 4 bytes.
  2. Step 2: Calculate total memory for 1000 integers

    Total memory = number of elements x size of each element = 1000 x 4 = 4000 bytes.
  3. Final Answer:

    4000 bytes -> Option C
  4. Quick Check:

    1000 x 4 = 4000 bytes [OK]
Hint: Multiply count by size of int (4 bytes) [OK]
Common Mistakes:
  • Using size 2 bytes for int
  • Multiplying by 8 bytes (double size)
  • Confusing bits with bytes
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard