The sizeof operator helps you find out how much memory a variable or data type uses. This is important to plan and control memory use in small devices.
sizeof and memory budgeting in Embedded C
sizeof(expression_or_type)
The sizeof operator returns the size in bytes.
You can use it with a variable, a data type, or an expression.
int type in bytes.sizeof(int)myVariable. Parentheses are optional here.sizeof myVariable
sizeof(array)
MyStruct.sizeof(struct MyStruct)
This program shows how to use sizeof to find the size of different types and variables. It also calculates how many elements are in an array by dividing the total size by the size of one element.
#include <stdio.h> int main() { int a = 10; char b = 'x'; double c = 3.14; int arr[5]; printf("Size of int: %zu bytes\n", sizeof(int)); printf("Size of variable a: %zu bytes\n", sizeof a); printf("Size of char: %zu bytes\n", sizeof(char)); printf("Size of variable b: %zu bytes\n", sizeof b); printf("Size of double: %zu bytes\n", sizeof(double)); printf("Size of variable c: %zu bytes\n", sizeof c); printf("Size of array arr: %zu bytes\n", sizeof arr); printf("Number of elements in arr: %zu\n", sizeof arr / sizeof arr[0]); return 0; }
Sizes can vary depending on the hardware and compiler.
Use sizeof to avoid hardcoding sizes and make your code safer and portable.
Remember that sizeof returns size in bytes, which is the smallest addressable memory unit.
sizeof tells you how many bytes a type or variable uses.
It helps you plan memory use, especially in devices with limited memory.
Use it to find array sizes and avoid memory errors.
Practice
sizeof operator in embedded C tell you?Solution
Step 1: Understand the purpose of
Thesizeofsizeofoperator returns the size in bytes of a data type or variable in memory.Step 2: Differentiate from other concepts
It does not give the value stored, bits count, or address, only the byte size.Final Answer:
The number of bytes used by a data type or variable -> Option AQuick Check:
sizeof= bytes used [OK]
- Confusing bytes with bits
- Thinking sizeof returns variable value
- Mixing sizeof with pointer address
int data type in embedded C?Solution
Step 1: Recall correct sizeof syntax
Thesizeofoperator requires parentheses around the variable or type name.Step 2: Check each option
sizeof(int); usessizeof(int);which is correct syntax. Options B and D are invalid syntax, and C is not a valid operator.Final Answer:
sizeof(int); -> Option AQuick Check:
Use parentheses with sizeof [OK]
- Omitting parentheses
- Using square brackets instead of parentheses
- Typing size_of instead of sizeof
char arr[10]; int total_size = sizeof(arr);
What is the value of
total_size assuming char is 1 byte?Solution
Step 1: Calculate size of array
The arrayarrhas 10 elements of typechar, each 1 byte.Step 2: Multiply element size by count
Total size = 10 elements * 1 byte = 10 bytes.Final Answer:
10 -> Option DQuick Check:
Array size = element count x sizeof(element) = 10 [OK]
- Confusing element size with total size
- Assuming int size instead of char
- Using sizeof pointer instead of array
int *ptr; int size = sizeof(ptr) / sizeof(int); ptr = malloc(size * sizeof(int));
Solution
Step 1: Understand sizeof on pointer
sizeof(ptr)returns the size of the pointer itself (usually 4 or 8 bytes), not the size of the array it points to.Step 2: Identify the error in size calculation
Dividing pointer size bysizeof(int)does not give the number of elements to allocate.Final Answer:
sizeof(ptr) returns pointer size, not array size -> Option CQuick Check:
Pointer size ≠ array size [OK]
- Using sizeof(pointer) to find array length
- Forgetting malloc returns void* needing cast (in C++)
- Assuming pointer is initialized before sizeof
short) and a struct:struct Data {
char flag;
int value;
};If
short is 2 bytes, char 1 byte, and int 4 bytes, what is the maximum number of short elements you can store if you also need space for 5 Data structs?Solution
Step 1: Calculate size of one Data struct
Size = size ofchar+ size ofint= 1 + 4 = 5 bytes.
Assuming no padding for simplicity.Step 2: Calculate total size for 5 Data structs
5 structs x 5 bytes = 25 bytes.Step 3: Calculate remaining memory for shorts
Total budget 73 bytes - 25 bytes = 48 bytes left.Step 4: Calculate max shorts fitting in remaining memory
Eachshortis 2 bytes, so max elements = 48 / 2 = 24 elements.Step 5: Consider struct padding
Usually,struct Datawill be padded to alignintat 4 bytes:
Layout:char flag (1 byte) + 3 bytes padding + int value (4 bytes) = 8 bytesper struct.
Total for 5 structs = 5 x 8 = 40 bytes.Step 6: Calculate remaining memory with padding
73 - 40 = 33 bytes left for shorts.Step 7: Calculate max shorts with padding
33 bytes / 2 bytes per short = 16.5 -> 16 shorts.Step 8: Re-examine options
Options are larger than 16, so question expects total shorts + structs size ≤ 73.
Try 24: 24 shorts x 2 bytes = 48 bytes + 5 structs x 8 bytes = 40 bytes -> 88 bytes (too big).Step 9: Reconsider question intent
Question asks max shorts if 5 structs also stored.
So total memory = shorts size + structs size ≤ 73 bytes.
Assuming no padding (5 bytes per struct), total structs = 25 bytes.
73 - 25 = 48 bytes for shorts -> 48/2 = 24 shorts max.Final Answer:
24 -> Option BQuick Check:
Memory budget - structs size = shorts size [OK]
- Ignoring struct padding/alignment
- Using pointer size instead of actual struct size
- Not subtracting struct memory before shorts calculation
Ready to prove it?
Pass the test and this pattern moves unknown
