Created
January 17, 2022 13:09
-
-
Save aprell/7f3fa315bd6754fc3e6667ab678df5c5 to your computer and use it in GitHub Desktop.
One of many ways to do compile-time assertions in C
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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