A structured lesson workspace with readable content, hands-on examples, and a clean path to completion.
sizeof is a compile-time operator, not a function. It returns the size in bytes of a type or expression. The result is a size_t, an unsigned integer type. sizeof(int) is typically 4. sizeof(double) is 8. sizeof a pointer is 8 on 64-bit systems. The operand is not evaluated (except for variable-length arrays in C99). You can write sizeof(x++) and x will not increment. This makes sizeof safe to use on expressions with side effects, though you should not rely on that quirk.
#include <stdio.h>typedef struct {char flag;int value;double weight;} Item;int main(void) {printf("sizeof(char): %zu\n", sizeof(char));printf("sizeof(int): %zu\n", sizeof(int));printf("sizeof(double): %zu\n", sizeof(double));printf("sizeof(int*): %zu\n", sizeof(int *));printf("sizeof(Item): %zu\n", sizeof(Item));int arr[10];printf("sizeof(arr): %zu\n", sizeof(arr));printf("elements: %zu\n", sizeof(arr) / sizeof(arr[0]));int x = 5;printf("sizeof(x++): %zu\n", sizeof(x++));printf("x after: %d\n", x);return 0;}
Item is 16 bytes, not 13 (1 + 4 + 8). The compiler inserts 3 bytes of padding after the char to align the int on a 4-byte boundary, and the struct's total size is padded to a multiple of its largest alignment requirement. We cover structure padding in depth in the structs module. The sizeof(arr) / sizeof(arr[0]) idiom gives the element count, but only in the scope where arr was declared. Pass arr to a function and sizeof gives the pointer size.
x remained 5 after sizeof(x++). The operator computed the size of the expression's type (int, 4 bytes) without executing the increment. This is specified behavior for non-VLA types. It means sizeof is safe to use in macros and generic code where the operand might have side effects. But it also means sizeof is not a tool for runtime measurement. It is a compile-time constant.
sizeof is the operator you use to allocate the right amount of memory, to compute array lengths, and to write type-generic macros. It is evaluated at compile time. It costs nothing at runtime. Use it everywhere instead of hardcoded sizes. Next: abs and fabs, for absolute values of integers and floating-point numbers.
Finish the lesson once you have worked through the material. This awards ★ 20 XP.