0
0
Cprogramming~20 mins

Storage size overview in C - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Storage Size Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this code snippet?

Consider the following C code that prints the size of different data types using sizeof. What will be the output?

C
#include <stdio.h>

int main() {
    printf("%zu %zu %zu\n", sizeof(char), sizeof(int), sizeof(double));
    return 0;
}
A1 2 4
B1 4 8
C2 4 8
D1 8 8
Attempts:
2 left
💡 Hint

Remember that char is usually 1 byte, int is commonly 4 bytes on modern systems, and double is often 8 bytes.

Predict Output
intermediate
2:00remaining
What is the size of a pointer on a 64-bit system?

Given the following code, what will be the output on a typical 64-bit system?

C
#include <stdio.h>

int main() {
    int *p;
    printf("%zu\n", sizeof(p));
    return 0;
}
A8
B16
C2
D4
Attempts:
2 left
💡 Hint

Think about the size of pointers on 64-bit architectures.

Predict Output
advanced
2:00remaining
What is the output of this struct size code?

Consider this C code that prints the size of a struct with different data types. What is the output?

C
#include <stdio.h>

struct Data {
    char c;
    int i;
    double d;
};

int main() {
    printf("%zu\n", sizeof(struct Data));
    return 0;
}
A16
B20
C13
D24
Attempts:
2 left
💡 Hint

Remember that structs may have padding to align data properly in memory.

Predict Output
advanced
2:00remaining
What is the output of this union size code?

What will this program print when it runs?

C
#include <stdio.h>

union U {
    char c;
    int i;
    double d;
};

int main() {
    printf("%zu\n", sizeof(union U));
    return 0;
}
A16
B4
C8
D1
Attempts:
2 left
💡 Hint

Unions allocate memory equal to the size of their largest member, possibly with padding.

🧠 Conceptual
expert
3:00remaining
How many bytes does this bit-field struct occupy?

Given the following struct with bit-fields, how many bytes does it occupy on a typical 64-bit system?

C
struct BitField {
    unsigned int a : 3;
    unsigned int b : 5;
    unsigned int c : 6;
    unsigned int d : 18;
};
A4
B8
C2
D6
Attempts:
2 left
💡 Hint

Bit-fields are packed into the smallest number of bytes that can hold all bits, aligned to the base type size.