← GCC/C Index

🛑 <assert.h> — Diagnostic Assertions

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

📋 Functions & Macros

NamePrototype / ValueDescription
assertvoid assert(scalar expr)Abort if expr is false
static_assertstatic_assert(expr, msg)Compile-time assertion (C11)
NDEBUG#define NDEBUGDisables all assert() when defined

💡 Example Program

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 */

← <limits.h>  |  🏠 Index  |  <signal.h> →