Understanding assert in C/C++ With Examples

In this article, we’ll take a look at using the concept of assertions, using the assert in C/C++.

This is not very hard to understand, so let’s get started!

We’ll first look at what we mean by an assertion, and then look at how we can use it in our C programs to debug effectively!


What is an assertion?

An assertion is a specification that verifies that a program satisfies certain conditions at particular points, during its execution (run-time condition checks).

For computer programs, there are primarily three types of assertions checks:

  • Precondition Assertion -> Condition satisfied before main body execution
  • Post-condition Assertion -> Condition satisfied after main body execution
  • Invariant Assertion -> Condition satisfied after every repetitive region of a function (like a loop)

Now that we know what an assertion is, let’s look at doing this in a C program.


Using assertions in C programs

In C, we use the assert macro to define an assertion statement. This is there in the <assert.h> header file.

To define an assertion, we can write something like this:

Here, condition must be boolean. For example, the below is an example of an assertion:

The above assertion holds true, since 0>=0. Therefore, during execution, our program continues normally.

If the assert condition holds false, it will produce an error, and our program will stop executing, with suitable error messages.

Now that we’ve covered the basics, let’s look at using assertions in a for loop.

This not only ensures that we want the program to do what we want, but also structures code logically, so that it is easy to read.


An example – Using assert in a loop

Consider the below code, that simply adds integers within a given range. We want to ensure that our final result is always positive, and does not overflow.

Output

If the value of our result is too high (beyond the integer size), then the result will overflow and become negative!

Our second set of input does this, and hence the loop invariant assertion will fail!

So our program will halt there itself. This ensures that we can detect any design flaw in our program, and handle such inputs accordingly.


Conclusion

In this article, we learned how we could check for assertions, using the assert macro in C/C++. For similar articles on C, do look at our tutorial section on C programming.

References


By admin

Leave a Reply

%d bloggers like this: