← Back to C Course | Chapter 12: Advanced Topics | Lesson 15 of 20

C assert.h

assert is a check you put in code that stops the program with a message if something you believed was true turns out false, like a smoke alarm. It helps find bugs early.
Syntax
c
#include <assert.h>

assert(condition);  // aborts if condition is false

What is Assertion?

An assertion documents an assumption your code is relying on and checks it automatically at runtime, immediately halting the program if that assumption turns out to be false — catching a bug at the exact point it happens rather than letting it silently corrupt data further downstream.

Example: What is Assertion?

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
#include <assert.h>
// Program execution starts in main()
int main() {
	// Declare x and set it to 5
	int x = 5;
	assert(x > 0);
	// Print formatted text to the screen
	printf("Assumption held: %d", x);
	// Return 0 to signal that the program finished successfully
	return 0;
}

The assert() Macro

The assert() macro, defined in assert.h, evaluates the expression you give it and, if that expression is false (zero), prints the failing expression along with the file name, function, and line number to stderr and then calls abort() to stop the program immediately.

Example: The assert() Macro

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
#include <assert.h>
// Program execution starts in main()
int main() {
	// Declare age and set it to 25
	int age = 25;
	assert(age >= 0);
	// Print formatted text to the screen
	printf("%d", age);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Disabling Assertions (NDEBUG)

Defining the NDEBUG macro before including assert.h turns every assert() call in that file into a no-op, which is the standard way to strip out assertion checks (and their runtime cost) from a release build while keeping them active during development and testing.

Example: Disabling Assertions (NDEBUG)

c
// Define the macro NDEBUG (replaced by the preprocessor)
#define NDEBUG
// Include standard input/output (printf, scanf)
#include <stdio.h>
#include <assert.h>
// Program execution starts in main()
int main() {
	assert(1 == 2);
	// Print formatted text to the screen
	printf("Assertion skipped");
	// Return 0 to signal that the program finished successfully
	return 0;
}

Asserting Pointer Validity

Placing assert(ptr != NULL); right before dereferencing a pointer is a fast, self-documenting way to catch a NULL-pointer bug the moment it happens during development, rather than tracking down a mysterious crash that occurs several function calls later.

Example: Asserting Pointer Validity

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
#include <assert.h>
// Program execution starts in main()
int main() {
	// Declare x and set it to 5
	int x = 5;
	int *ptr = &x;
	assert(ptr != NULL);
	// Print formatted text to the screen
	printf("%d", *ptr);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Asserting Value Ranges

Wrapping input parameters in something like assert(index >= 0 && index < size); catches out-of-range values immediately at the function boundary, making the function's real assumptions explicit in the code instead of leaving them as an unwritten expectation.

Example: Asserting Value Ranges

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
#include <assert.h>
// Program execution starts in main()
int main() {
	int arr[5] = {1,2,3,4,5};
	// Declare index and set it to 2
	int index = 2;
	assert(index >= 0 && index < 5);
	// Print formatted text to the screen
	printf("%d", arr[index]);
	// Return 0 to signal that the program finished successfully
	return 0;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Putting code with side effects inside assert, which disappears when NDEBUG is defined.
  2. Using assert to check user input, when it is meant for programmer errors and can be compiled out.
  3. Forgetting #include <assert.h> before using the assert macro.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.