Open In App

Assertions in C/C++

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

Assertions are statements used to test assumptions made by programmers. For example, we may use assertion to check if the pointer returned by malloc() is NULL or not. 
Following is the syntax for assertion.
 

void assert( int expression ); 

If the expression evaluates to 0 (false), then the expression, sourcecode filename, and line number are sent to the standard error, and then abort() function is called. 
For example, consider the following program. 
 

C




#include <stdio.h>
#include <assert.h>
 
int main()
{
    int x = 7;
 
    /*  Some big code in between and let's say x
       is accidentally changed to 9  */
    x = 9;
 
    // Programmer assumes x to be 7 in rest of the code
    assert(x==7);
 
    /* Rest of the code */
 
    return 0;
}


Output 

Assertion failed: x==7, file test.cpp, line 13 
This application has requested the Runtime to terminate it in an unusual 
way. Please contact the application's support team for more information.

Assertion Vs Normal Error Handling 
Assertions are mainly used to check logically impossible situations. For example, they can be used to check the state of a code which is expected before it starts running, or the state after it finishes running. Unlike normal error handling, assertions are generally disabled at run-time. Therefore, it is not a good idea to write statements in assert() that can cause side effects. For example, writing something like assert(x = 5) is not a good idea as x is changed and this change won’t happen when assertions are disabled. See this for more details.
Ignoring Assertions 
In C/C++, we can completely remove assertions at compile time using the preprocessor NDEBUG. 
 

C




// The below program runs fine because NDEBUG is defined
# define NDEBUG
# include <assert.h>
 
int main()
{
    int x = 7;
    assert (x==5);
    return 0;
}


The above program compiles and runs fine. 
In Java, assertions are not enabled by default and we must pass an option to the run-time engine to enable them.
Reference: 
http://en.wikipedia.org/wiki/Assertion_%28software_development%29

 



Last Updated : 28 Feb, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments