Provides the assert() macro for debugging. Define NDEBUG before the include to disable all assertions.
Include: #include <assert.h> | Compile: gcc -Wall -o prog prog.c
| Name | Prototype / Value | Description |
|---|---|---|
| assert | void assert(scalar expr) | Abort if expr is false |
| static_assert | static_assert(expr, msg) | Compile-time assertion (C11) |
| NDEBUG | #define NDEBUG | Disables all assert() when defined |
Copy, save as demo.c, and compile with the command shown at the bottom.
#include <stdio.h>
#include <assert.h>
int divide(int a, int b) {
assert(b != 0);
return a / b;
}
int main(void) {
printf("10 / 2 = %d\n", divide(10, 2));
static_assert(sizeof(int) >= 4, "int must be at least 32 bits");
printf("All assertions passed.\n");
return 0;
}
/* Compile: gcc -Wall -std=c11 -o assert_demo assert_demo.c
Release: gcc -DNDEBUG -O2 -o assert_demo assert_demo.c */