Last updated:2026-10-12
C++ assert¶
About the Author¶
WeChat official account:

1. A First assert Example¶
assert is a macro, not a function.
The assert macro's prototype is defined in
assert can be disabled by defining NDEBUG, but this must be done at the beginning of the source code, before include
Code example: assert.c
Output: As you can see, the output reveals the source file, the line number, and the location of the error!
2. Assertions vs. Normal Error Handling¶
- Assertions are mainly used to check for situations that are logically impossible.
For example, they can be used to check the state the code expects before it starts running, or to check the state after running completes. Unlike normal error handling, assertions are usually disabled at run time.
- To ignore assertions, add the following at the beginning of the code:
Code example: ignore_assert.c
Related Topics¶
- C++ Pure Virtual Functions and Abstract Classes | C++那些事
- C++ Bit Fields | C++那些事
- Implementing C++ Polymorphism in C | C++那些事
- C++ const Constants and Qualifiers | C++那些事
- The Complete C++ const Family | C++那些事
- C++ Dereference Operator * | C++那些事
- C++ Initialization and Assignment | C++那些事
- Ways to Return Multiple Values in C++ | C++那些事
- C++ mutable Keyword | C++那些事
- C++ restrict and Compiler Optimization | C++那些事