Skip to content

Instantly share code, notes, and snippets.

@shilangyu
Created February 19, 2023 20:47
Show Gist options
  • Save shilangyu/7096faeeaa7cb84d980c05ef13a9eea9 to your computer and use it in GitHub Desktop.
Save shilangyu/7096faeeaa7cb84d980c05ef13a9eea9 to your computer and use it in GitHub Desktop.
Static assert in C

Enum variant values have to be constant expression integers. Division by 0 does not yield an integer so the compilation fails.

#define static_assert(cond, msg) enum { a = 1 / (cond) }
static_assert((SOME_CONST & (SOME_CONST - 1)) == 0, "SOME_CONST has to be a power of 2");
int main() {
int arr[SOME_CONST];
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment