Storage size overview in C - Time & Space Complexity
When we talk about storage size in C, we want to understand how much memory different data types use.
This helps us know how the program's memory needs grow as we use more or bigger data.
Analyze the memory size used by different variables in this code snippet.
#include <stdio.h>
int main() {
int a;
double b;
char c;
int arr[10];
return 0;
}
This code declares variables of different types and an array, each using some memory space.
Here, we look at how memory size adds up for each variable and array element.
- Primary operation: Counting memory size of each variable and array elements.
- How many times: Once per variable, and once per array element (10 times for the array).
Memory size grows as we add more elements or bigger types.
| Input Size (array length) | Approx. Memory Used |
|---|---|
| 10 | Size of 10 ints + other variables |
| 100 | Size of 100 ints + other variables |
| 1000 | Size of 1000 ints + other variables |
Pattern observation: Memory grows linearly with the number of array elements.
Space Complexity: O(n)
This means memory size grows in direct proportion to the number of elements we store.
[X] Wrong: "All variables use the same amount of memory regardless of type or size."
[OK] Correct: Different types like int, double, and char use different amounts of memory, and arrays multiply that by their length.
Understanding how memory size grows helps you write programs that use memory wisely and avoid surprises in real projects.
"What if we changed the array to hold doubles instead of ints? How would the memory size change?"
Practice
Solution
Step 1: Recall typical sizes of basic data types
In C,charusually takes 1 byte,int4 bytes,float4 bytes, anddouble8 bytes.Step 2: Compare sizes to find the smallest
Since 1 byte is less than 4 or 8 bytes,charuses the least memory.Final Answer:
char -> Option BQuick Check:
char = 1 byte [OK]
- Confusing char with int size
- Assuming float is smaller than char
- Thinking double is smallest
double in C?Solution
Step 1: Recall C variable declaration syntax
In C, the type comes first, then the variable name, ending with a semicolon.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.Final Answer:
double num; -> Option AQuick Check:
Type before name, semicolon ends declaration [OK]
- Using colon instead of semicolon
- Reversing type and variable name
- Using invalid characters like dash
printf("%zu", sizeof(long));What is the expected output on a typical 64-bit system?
Solution
Step 1: Understand sizeof operator on 64-bit systems
On most 64-bit systems,longis 8 bytes (64 bits).Step 2: Match the output of
Thesizeof(long)sizeofoperator returns the size in bytes, so it returns 8.Final Answer:
8 -> Option AQuick Check:
long = 8 bytes on 64-bit [OK]
- Assuming long is 4 bytes on 64-bit
- Confusing bits with bytes
- Using wrong format specifier in printf
short variable but causes a compilation error:short s;
printf("%d", sizeof(s));
What is the main reason for the error?
Solution
Step 1: Identify the type returned by sizeof
Thesizeofoperator returns a value of typesize_t, which is an unsigned integer type.Step 2: Check printf format specifier compatibility
The format specifier%dexpects anint, butsizeofreturnssize_t. The correct specifier is%zu.Final Answer:
Using wrong format specifier for sizeof result -> Option DQuick Check:
Use %zu for sizeof output [OK]
- Using %d instead of %zu for sizeof
- Thinking sizeof needs variable initialization
- Missing semicolon (actually present)
int is 4 bytes, how much memory in bytes will the array consume?Solution
Step 1: Identify size of one integer
On a 32-bit system,inttypically uses 4 bytes.Step 2: Calculate total memory for 1000 integers
Total memory = number of elements x size of each element = 1000 x 4 = 4000 bytes.Final Answer:
4000 bytes -> Option CQuick Check:
1000 x 4 = 4000 bytes [OK]
- Using size 2 bytes for int
- Multiplying by 8 bytes (double size)
- Confusing bits with bytes
Ready to prove it?
Pass the test and this pattern moves unknown
