Assertions In Cc++ - C++ Programming Tutorial
C++ Course / Miscellaneous / Assertions In Cc++

Assertions In Cc++

BLUF: Mastering Assertions In Cc++ is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: Assertions In Cc++

C++ is renowned for its efficiency. Learn how Assertions In Cc++ enables low-level control and high-performance computing in the tutorial below.

What is Assertion?

Assertions are a collection of code snippets where we include specific expressions or conditions to verify their truthfulness or existence.

  • When the condition or expression evaluates to true, the integer value 1 is returned, allowing the program to proceed to the next line smoothly.
  • Conversely, if the assertion's condition is false or non-existent, it will yield a false result, returning 0 as an integer, consequently leading to the termination of our program.

The 'assert' keyword is employed in assertions to specify the expression being evaluated. This expression typically reflects a programmer's assumptions about a variable or code statement.

For instance, if we need to verify if a variable's value has been altered to a different value at any point in the program, we can use assertions to make assumptions.

Syntax:

Example

assert(int expression)

C Example 1:

Example

#include <stdio.h>
#include<assert.h>
int main() {
    int var= 65;
    var=6+var;
    var=var-45;
    assert(var==45);
    printf("if the value of var in assert condition is 26 this line will execute otherwise, our program will be terminated");

    return 0;
}

Output:

Explanation:

In the preceding code snippet, there is a variable that has been assigned an initial value of 65. Subsequently, 6 has been incremented to this variable, resulting in a new value of 71. Following this, a subtraction of 45 has been performed on the variable, resulting in a final value of 26.

Now, within the assert statement, we specify the condition to check if the variable's value is 45. In this case, the condition evaluates to false, resulting in a return value of 0. Consequently, the program will terminate, and any subsequent code will not be executed.

C Example 2:

Example

#include <stdio.h>
#include<assert.h>
int main() {
    int var= 65;
    var=6+var;
    var=var-45;
    assert(var==26);
    printf("if the value of var in assert condition is  26 the, this line will execute. Otherwise, our program will be terminated");

    return 0;
}

Output:

Explanation

In the provided code snippet, a condition has been set in the assertion to check if the variable equals 26. When this condition evaluates to true, the assertion will return a value of 1. Consequently, the subsequent line of code will execute smoothly without encountering any issues.

Since assertion functions similarly to error handling, in that it allows us to catch errors and handle them if they occur, ensuring that the program can continue running smoothly if no errors are encountered.

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

  • 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.
  • Assertions can be ignored or disabled at run time, but this is not possible in error handling.
  • 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 also have the option to disable the assertion during compilation if it exists by importing the preprocessor directive NDEBUG.

C Example 3:

Example

#define NDEBUG
#include <stdio.h>
#include<assert.h>
int main() {
    int var= 65;
    var=6+var;
    var=var-45;
    assert(var==65);
    printf("if the value of var in assert condition is  26 the this line will execute otherwise our program will be terminated");

    return 0;
}

Output:

Explanation

In this scenario, the variable holds a value of 26, and despite the assertion condition being false, the program continues to run as intended. This is due to the inclusion of the preprocessor directive NDEBUG, which effectively bypasses the assertion check.

There is another idea known as compile-time assertion, which gets executed during compilation, unlike assert which operates solely during runtime. This feature is exclusive to C++, lacking in the C programming language.

C++ Example:

Example

#include <iostream>
#include<cassert>
using namespace std;
int main() {
    
    assert(10+6 == 19);
    static_assert(10+6 == 19,"this is static assertion");
    cout<<" this line can be executed if assertion return true"<<endl;
    return 0;
}

Output:

Explanation

The static assertion is verified during compilation, triggering a compile-time error if the specified expression is incorrect. Unlike normal assertions in the code, which are only validated at runtime.

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience