Skip to content

Instantly share code, notes, and snippets.

Last active September 5, 2022 23:18
What would you like to do?
Useful assertion macros in C which include debug information like file, line number and error message.
// These require compiler support for statement expressions (
#define assert_ok(e) ({int x = (e); if (x < 0) { printf("%s:%d: ", __FILE__, __LINE__); fflush(stdout); perror(#e); abort(); } x;})
#define assert_ptr(e) ({void* p = (e); if (p == NULL) { printf("%s:%d: %s: NULL pointer returned\n", __FILE__, __LINE__, #e); abort(); } p;})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment