C assert.h
In this page:
#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?
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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)
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
- Putting code with side effects inside
assert, which disappears whenNDEBUGis defined. - Using
assertto check user input, when it is meant for programmer errors and can be compiled out. - Forgetting
#include <assert.h>before using theassertmacro.
Chapter Quiz — Complete all 20 topics to unlock
0/20 topics done
Complete these topics first:
- C typedef
- C Type Casting
- C Bit Fields
- C Variable Length Arrays
- C Command Line Arguments
- C Function Pointers
- C Callback Functions
- C Multidimensional Pointer
- C string.h Functions
- C stdlib.h Functions
- C math.h Functions
- C time.h Functions
- C ctype.h Functions
- C errno.h
- C assert.h
- C Error Handling
- C Debugging Techniques
- C Code Style & Best Practices
- C Common Mistakes
- C Interview Questions