#include <stdio.h> typedef struct { char a; int b; short c; } MyStruct; void print_offsets() { MyStruct obj; //%z → modifier meaning “this matches a size_t” //u → unsigned decimal integer printf("Offset of a: %zu\n", (char*)&(obj.a)- (char*)&obj); printf("Offset of b: %zu\n", (char*)&(obj.b) - (char*)&obj); printf("Offset of c: %zu\n", (char*)&(obj.c) - (char*)&obj); printf("Size: %zu\n", sizeof(MyStruct)); } int main() { print_offsets(); return 0; }
Test Cases
Test Results
Input
Expected Output
Offset of a: 0 Offset of b: 4 Offset of c: 8 Size: 12