Example 1 — Require C99 and pack a struct
pack(1) removes padding in GCC, Clang, and MSVC.
#include <stdio.h>
#if !defined(__STDC_VERSION__) || __STDC_VERSION__ < 199901L
#error "C99 or newer is required"
#endif
#pragma pack(push, 1)
struct Packed { char c; int i; };
#pragma pack(pop)
int main(void) {
printf("built as C99 or newer\n");
printf("sizeof Packed = %zu\n", sizeof(struct Packed));
return 0;
} How It Works
- If the compiler is older than C99,
#errorstops the build with a clear message. pack(push, 1)aligns members on 1 byte, sochar+intis 5 bytes.pack(pop)restores the previous alignment.
