Just like normal functions syntax for declaration in C which is:
Assert function is also declared in a similar way as:
Similar to any other expression in the world of programming languages, it takes the Boolean expression for comparison (any kind of comparison logical, inequality, etc.). Checks if it is FALSE to display an error message and aborts the program execution, or the program runs smoothly without any abruptions instead.
Return Type= void
As we have stated before, the assert function is a macro, in disguise of a function, and the macro does not return any value. This is the reason why its return type is void.
In our first example, we will create a simple function to calculate the average with the help of arrays in the C language.
We will use the assert function for, limiting the user from entering invalid input in variable n. As an accidental input 0, variables present in an array under normal circumstances will be an issue for this program. Divide the sum of variables present in an array with the array’s size, also known as n. Any number divided by 0 results in a runtime error, which is why we will use the assert function to tackle this issue. Now, let’s commence with the coding.
Create a notepad file and name it anything. In our case, we will keep our file name “Example1” and add a .cpp extension at the end of my file name.
Now open your command prompt terminal and type the following command:
It is used for compiling c programs and results similar to the picture below unless you have any compiling errors in your program.
At last type:
For executing your C program on the command prompt.
Now let’s dig into the main course of example. First of all, we need to add an assert header at the top.
To ensure the problem does not occur, we will use the assert function and pass the expression to ensure that.
And that is all, let’s test our program. When a user enters a value of n equals 0.
As it’s clear from the screenshot posted above, the assert function abrupt the flow of execution when the expression became FALSE and told us the reason for the program to abort().
When a user enters a valid value, we will get an error-free smooth running program.
In this example, we will create a C program that takes a string of the user’s name and returns Hello [user’s name] as an output.
The program above has no error, but you might already know the problem if you have any prior experience with string inputs. The problem is that the user can enter an empty string and, this program will execute with a hitch and display Hello as a result.
This is the reason why we need the assert function to ensure an empty string isn’t entered. The drill for coding is the same as before; we just need to change the placement of the assert function declaration and its expression. The user normally presses the “Enter” key to enter an empty string, which is ‘\0’ in terms of char variable type. We will pass the expression to assert the function that checks the string is not empty.
Let’s check the result of the changes we just made to our code when a user enters an empty string.
And as we can see from the screenshot, the assert function responded to the expression correctly and abrupted the execution of the program to display an error message. A user name is entered now.
Nothing happened, as the function’s expression condition is met.
This article covered an assert function, its syntax, how it is declared, and a few examples related to it. You may change the examples as per your work responsibilities. I hope this tutorial was easy to understand and cleared all your queries related to assert function in C language.