Use of assert macro in C/C++ with Examples

The assert macro puts diagnostic tests into programs. It is a very good habit to implement a self-test in the program. It solves many critical issues.

When the assert is executed, if the expression is false (0), the assert macro writes information about the particular call that failed on the standard error stream in an implementation-defined format.  After that, it calls the abort function.

The assert is defined in the ‘assert.h’ header file, so you have to include the header file before using it.

 

Syntax assert in C:

//Syntax of assert in C

void assert(scalar expression);

 Parameters:

expression: expression of scalar type

Return:

The assert macro returns no value.

 

Let’s see an example code to understand the usage of assert in C.

#include <stdio.h>
#include <assert.h>

int main()
{
    int data = 27;

    /* Some big code in between and let's say data
    is accidentally changed to 6 */
    data = 6;

    // Programmer assumes data to be 27 in rest of the code
    assert(data== 27);

    printf("%d\n",data);

    return 0;
}

Output:

assert in c

 

 

Some common places where assert should use:

A number of philosophies can be employed when deciding where to use an assert() macro in our program. We need to remember that assert is used to catching the bugs. So let’s see some common scenarios where assert should.

 

Scenario 1:

Suppose you have a function to set the value between the 0 to 100. In this function, we can use the assert macro, if anybody will try to set the value beyond this range, then the assert() will print the message on the standard error stream and abort the program.

//set function to set the data
// in EEPROM

void set(int value)
{
    assert((value > 0) && (value < 100));

    //value setting in memory
}

 

Scenario 2:

We can use the assert() at the time of dynamic memory allocation.

//assert use at the time
//of memory allocation
char *buffer = (char*)malloc(10);
assert (NULL != buffer);

 

 

Scenario 3:

Some portion of the code executes only when the bug exists. A very common example is a switch case statement where we put the default case. The control comes to this default case only when the control expression is wrong. In simple words, if an illegal value is coming, the default case will execute.

switch (expression)
{
case 1:
    //Do some action
    break;

case 2:
    //Do some action
    break;

default:
    assert(0);
    break;
}

 

You can see in default case I am passing 0, or false, to the assert. As you know if we pass 0 in the assert, then it guarantees the assert will call its error handler. If you make a habit of coding all of your switch statements in this way, you will catch a surprising number of illegal values, and you will immediately know which value is at fault.

 

There are also many scenarios, I will add it in the future. But you should remember that before using the assert macro in C, you have to include ‘assert.h’ header file.

 

Recommended Articles for you:

Leave a Reply

Your email address will not be published. Required fields are marked *