#include <stddef.h>
//static_assert checks a condition while compiling:
//if it is false, the program is not built at all.
//C23: a keyword, and the message may be left out
static_assert(sizeof(int) >= 4, "int must hold 32 bits");
static_assert(CHAR_BIT == 8);
//the layout of a structure that is written to a file as is
typedef struct {
uint32_t id;
uint16_t year;
uint8_t month;
uint8_t day;
} Record;
static_assert(sizeof(Record) == 8, "Record must take 8 bytes");
static_assert(offsetof(Record, year) == 4);
//a table must have a value for every item of the enumeration
typedef enum {
RED,
GREEN,
BLUE,
COLOR_COUNT,
} Color;
const char *colorNames[] = {"red", "green", "blue"};
static_assert(sizeof colorNames / sizeof colorNames[0] == COLOR_COUNT,
"a name for every color");
//a false condition stops the build:
//static_assert(sizeof(long) == 2); // <- Error: static assertion failed
//unlike assert, the check costs nothing while the program runs
size_t recordSize = sizeof(Record);
//recordSize is 8
const char *blueName = colorNames[BLUE];
//blueName is "blue"
printf("recordSize is %zu\n", recordSize);
printf("blueName is %s\n", blueName);