Skip to content

Instantly share code, notes, and snippets.

@LarryRuane
Last active August 27, 2022 15:29
Show Gist options
  • Save LarryRuane/ce53d47588bd19f4c76e801b955ea729 to your computer and use it in GitHub Desktop.
Save LarryRuane/ce53d47588bd19f4c76e801b955ea729 to your computer and use it in GitHub Desktop.
example static assert implemention
#define static_assert(c) do { int _x = 1/((int)c); } while (0)
int main() {
static_assert(sizeof(int) == 4);
static_assert(sizeof(int) < 4);
return 0;
}
/* output:
$ gcc t.c
t.c: In function ‘main’:
t.c:1:41: warning: division by zero [-Wdiv-by-zero]
1 | #define static_assert(c) do { int _x = 1/((int)c); } while (0)
| ^
t.c:5:5: note: in expansion of macro ‘static_assert’
5 | static_assert(sizeof(int) < 4);
| ^~~~~~~~~~~~~
$
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment