Skip to content

Instantly share code, notes, and snippets.

@kylesluder
Created August 30, 2012 00:14
Show Gist options
  • Save kylesluder/3520840 to your computer and use it in GitHub Desktop.
Save kylesluder/3520840 to your computer and use it in GitHub Desktop.
Optional assertion description argument
#include <stdio.h>
static inline void _AssertionFailed(char *expr, char *reason)
{
if (!reason || *reason == '\0')
printf("Assertion failed: %s\n", expr);
else
printf("Assertion failed: %s (Reason: %s)\n", expr, reason);
}
#define ASSERTN(expr, ...) _AssertionFailed(#expr, "" __VA_ARGS__)
#define ASSERT(...) ASSERTN(__VA_ARGS__)
int main(int argc, char **argv) {
// Without a reason:
ASSERT(0);
// With a reason:
ASSERT(0, "Should not get here");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment