C Assertions

Advanced
6 min read
Updated: Nov 2026
1 example

What Is Assertions?

assert(condition) from <assert.h> aborts the program with a message showing the file, line, and condition if the condition is false. Use it for conditions that must always be true (bugs), not for user input errors. Defining NDEBUG removes all asserts, so never put required side effects inside them.

Remember
#include <assert.h>
assert(ptr != NULL);
assert(index >= 0 && index < size);
/* gcc -DNDEBUG disables every assert */

Cheat Sheet

Keep these facts handy while you work with Assertions in C.

TopicDetail
FailsPrints location and calls abort()
Disable-DNDEBUG
Use forInternal invariants
Not forInput validation

C Examples

Compile each example with gcc file.c -std=c11 -Wall -o out. Next: static_assert.

Example 1 — Guard a precondition

The assertion documents that n must be positive.

C
#include <assert.h>
#include <stdio.h>

static double average(const int *a, int n) {
    assert(a != NULL);
    assert(n > 0);
    int sum = 0;
    for (int i = 0; i < n; i++) sum += a[i];
    return (double)sum / n;
}

int main(void) {
    int v[] = {1, 2, 3, 4};
    printf("average = %.1f\n", average(v, 4));
    return 0;
}

How It Works

  1. Both assertions pass for valid input.
  2. Calling average(v, 0) would print Assertion failed: n > 0 with the file and line, then abort.
  3. The cast to double keeps the fractional average, 10 / 4 = 2.5.

Edge Cases & Pitfalls

Check these before relying on Assertions in real programs.

side

Side effects inside assert

assert(i++ < 10) loses the increment when NDEBUG is set.

input

Validating user input with assert

Release builds remove asserts, so real input errors must use normal error handling.

Key Takeaways

  • Invariants: assert them.
  • Input: handle errors properly.
  • NDEBUG: removes asserts.

One line: assert(condition) from <assert.h> aborts the program with a message showing the file, line, and condition if the condition is false.

Frequently Asked Questions

The program prints a diagnostic with the expression, file, and line number to stderr and calls abort().
Many projects disable them with NDEBUG for speed; others keep them for safety. Never rely on them for input validation.
Continue with "C static_assert" next. Check conditions at compile time with static_assert (C11). The C Advanced Concepts hub lists every topic in order.

Next: static_assert

Check conditions at compile time with static_assert (C11).

Continue →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful