Javatpoint Logo
Javatpoint Logo

Assertions in C/C++

What is Assertion?

Assertions are the set of codes where we put some expression or condition to check whether the condition is true or not or to check whether the expression exists or not.

  • If the condition is true or an expression exists then we get the true or value 1 in the integer, and the following line of the program is executed easily.
  • But if the condition in the assertion is false or does not exist then it will return false, and as an integer, it will return 0, and our program will be terminated.

The 'assert()' keyword is used in assertion where we put the expression for the evaluation. Assertion's expression can be related to the assumption of a programmer for any variable or any statement of the code.

For example, if we want to check whether the value of any variable is changed to some other value during the program, then we can make assumptions with the help of assertion.

Syntax:

C Example 1:

Output:

Assertions in C/C++

Explanation:

In the above code, we have a variable which is initialized with the value 65. After that, we have added 6 to this variable, so now its value is 71. Now we have subtracted 45 from it, so now the value is 26.

Now in the assert condition, we put the expression that if the value of the variable is equal to 45 or not. So it will return 0 since it is a false condition, and our program will be aborted, and further lines of code will not run.

C Example 2:

Output:

Assertions in C/C++

Explanation

In the above code, we have put a condition in the assertion that if the variable is equal to 26, which is true and it will return 1. So the following line of the code will run without any problem.

Since assertion is quite similar to error handling, where if an error exists then we can do error handling and terminate our program; otherwise, the rest of the code will run without any problem.

But there are a lot of differences between assertion and normal error handling:

  1. Assertions are generally used for conditions which are logically not correct, whereas, in error handling, we handle the different types of errors in the code.
  2. Assertions can be ignored or disabled at run time, but this is not possible in error handling.
  3. Error handling is always considered a better way to write the code, whereas writing assertion is not a good idea for normal conditions as it affects the program badly.

We can also ignore the assertion at the compile time in the code if it is present. We have to import the preprocessor directive NDEBUG.

C Example 3:

Output:

Assertions in C/C++

Explanation

In the above, the value of the variable is 26, and the condition in the assertion is false. Still, the program will not be terminated because we have imported the preprocessor directive NDEBUG and assertion is ignored successfully.

There is also the concept of static assertion, which is run at compile time, whereas assert() is used at run time only. The static assertion is only possible in C++. It is not present in C.

C++ Example:

Output:

Assertions in C/C++

Explanation

The static assertion is checked at the compile time, so it will give the compile time error representing that the expression in the static assertion is not correct. We have normal assertion present also in this code, but it will be checked at the runtime only.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA