Skip to content

Instantly share code, notes, and snippets.

@aprell
Created January 17, 2022 13:09
Show Gist options
  • Save aprell/7f3fa315bd6754fc3e6667ab678df5c5 to your computer and use it in GitHub Desktop.
Save aprell/7f3fa315bd6754fc3e6667ab678df5c5 to your computer and use it in GitHub Desktop.
One of many ways to do compile-time assertions in C
#define STATIC_ASSERT(cond) ((void)sizeof(char[1 - 2 * !(cond)]))
struct S {
int x, y;
short z;
};
int main(void) {
STATIC_ASSERT(1 + 2 == 3);
STATIC_ASSERT(sizeof(float) <= sizeof(double));
STATIC_ASSERT(sizeof(struct S) == 3 * sizeof(int));
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment