×

Assert() Function in C

Assert (): In C, the statements are executed with the exit statement. In C language declare, and it tests the condition parameters. If the statement executed will be false, it shows the standard error in the program using the string parameter to display the failed condition.

Then the variable sets to the exit function as _assert_exit to the one and compiler the exit statement.

In c programming language we use the assert.h header file to invoke the assert () function. This library will be provided by a Marco function which will be used to exit the statement with the standard error.

Assert is a macro function that is used to check the specific conditions at the runtime and it is very useful while debugging the source code or program.

 The assert () function is used to tests the condition expression or statement. If the statement is false, it prints the standard error message, the string parameter is used to describe the false condition. Then it sets to the variable like  _assert_exit and prints the exit statement.

The assert.h is the header file of the C which we used in the assert function and the Standard Library provides a macro called assert which can be used to verify the assumptions made by the code and print a message if this assumption is not true.

Similarly, we will use other expressions in the programming languages. Rather than int we use the Boolean function and the Boolean expression to check the logical and relatively conditions.

If we check the condition, it returns false it will display the error message and exit from the program or else the program will exit without assertions.

Here, we use the return data type as: void function

As we know that the assert function is a macro function and that macro function doesn't return any value. Hence this is the reason for that the return data type is void.

Assertion violations:

If the assert condition is violated during the compiling and executing of the program. if there is any error or bug present in the code then that function called the precondition. After successful compilation the bug will be found and it will be fixed. This is the concept of precondition.

Whereas in the post condition, if the post condition of the program is violated during the execution or testing of the code. if there is a bug or error present in the code it contains the precondition. Then the bug must be found and it will be fixed while debugging process.

 Syntax for the assert () function:

Void assert (int expression)

Example 1:

#include<assert.h>
Let’s see some example programs for assert function in c
Example 1:
Include <stdio.h>
#include <assert.h>
 
Int main(int argc, const char * argv[])
{
    Int pos = 1
    Printf(“pos is %d\n”, exp);
    Assert(pos);
    Pos = 0;
    Printf(“Pos is %d\n”, pos);
    Assert(pos);
    Return 0;
}

Output:

Pos is 1
Pos is 0
Assert:  assert.c: main 24
Assertion failed

In the above program we used two header files like stdio.h and assert.h 

Then we declared an expression pos and declared that expression. Assert should not exit. since exp is not 0 

And changing the expression into 0.

In this case expression is 0 so assert value will display an error and exit 

Example 2:

#include <stdio.h>
#include <assert.h>
 
Int main () {
  Int x, y;
 
  Printf(“the two numbers are to be divided\n”);
  Scanf(“%d%d”, &x, &y);
 
  Assert (y!= 0);
 
  Printf(“%d/%d = %.2f\n”, x, y, x/(float)y);
 
  Return 0;
}

Output:

The two numbers going to be divided:
4,0
Assertion failed: (y!=0), function main, file assert.c, line 10
Abort trap: 4

In the above program the assert occurred because of 4 can’t divided by 0.

Thus, the assertion occurred at line 10.

Example 3:

#include <assert.h>
#include <stdio.h>
Int main () {
   Int x;
   Char str [10];
 
   Printf(“Enter the value: “);
   Scanf(“%d”, &x);
   Assert (x >= 10);
   Printf(“value entered is %d\n”, x);
    
   Printf(“Enter string: “);
   Scanf(“%s”, str);
   Assert (str!= NULL);
   Printf(“String entered is: %s\n”, str);	
   Return (0);
}

Output:

Enter the value: 1
Value entered is 1
Enter string: java point
String entered is: java point

 This is the basic assert program without assertions. For the beginners this program will be very useful for learning the concept of assert () function.


Related Topics

Type Casting in C

Type casting is a way to convert a variable from one data type to another data type.Syntax: (type) expression;     Example: #include <stdio.h> int main() { int a,b; float sum; printf("Example of Type Casting\n"); printf("Enter any integer nos:"); scanf("%d",&a,&b); sum=a+b; printf("Sum: %f",sum); return 0; } Output Example...

1 minute read.

Ftell() Function in C

Ftell(): In File Handling we have some special functions like Ftell(), Fseek(), rewind() etc.. while you are randomly accessing the file these functions play very important role and these functions...

3 minutes read.

Run Time Polymorphism in C

What is polymorphism? Polymorphism refers to the existence of various forms. Polymorphism can be simply defined as a message's capacity to be presented in multiple forms. An individual who can have...

4 minutes read.

GCD program of two numbers in C

GCD stands for Greatest Common Divisor, which is also known as Highest Common Factor, which can be abbreviated as HCF. Mathematically it can be defined as any two positive integers...

3 minutes read.

C Switch Statements

Switch-case statement comes under the Selection control statement; there are four types of Control statements Decision-making statements (if, if-else)Selection statements (Switch-case)Iteration statements (for, while, do-while)Jump statements (break, continue, goto) If we want...

4 minutes read.

How to get ASCII value in C

For text data on computers and the internet, ASCII (American Standard Code for Information Interchange). It is the most widely used character encoding standard. One hundred twenty-eight alphabetic, numeric, special...

3 minutes read.

Flexible Array Members in a Structure in C

There is flexibility to declare array from c99 generation onwards that declaration of the collections can be made possible without even mentioning its dimension, which means that the displays are...

4 minutes read.

2f in C language

Float data type in c: Double-precision floating-point numbers with up to 17 significant digits are stored in the FLOAT data type. FLOAT is equivalent to C's double data type and IEEE...

3 minutes read.

Bigint (BIG INTEGERS) in C with Example

The maximum number of digits that a long, long int can have in C/C++ is 20. The issue is how to store the 22-digit number, which is difficult to do...

9 minutes read.

Difference between If and Switch Statement in C

Key Contrast: In the event that assertion is utilizes a Boolean articulation to execute the capability and can frequently be utilized to really look at various circumstances all at once.  The change...

4 minutes read.

Pointer in C

Pointer is a variable that is used to contain the address of another variable. We can easily create a pointer in C language. Example: int *ptr Symbol Description & (ampersand sign) Address of an operator...

1 minute read.

Deadlock Detection Program in C

What is Deadlock? A deadlock is a circumstance where a group in the process is halted because they are each holding onto resources while waiting for other methods to obtain them. Think...

5 minutes read.

Return array from function in C

Return array from function in C C programming does not require the return to a function of a whole array as an argument. However, you can return a pointer to an array without...

2 minutes read.

Command line arguments in C

Command line arguments in C The arguments that are generally passed from the line of command are referred to as command line arguments. These command line arguments are always handled by...

3 minutes read.

Conditional Operator in C

In C programming language, the conditional operator (also known as the ternary operator) is a shorthand way of writing an if-else statement. The syntax of conditional operator in C is...

3 minutes read.

Program to Find Mode of an Array in C

Mode is the highest occurring value or number in a given set of data elements. In a group of data values, a value that occurs most frequently is considered to...

3 minutes read.

C Language Environment Setup

To compile C program, we must have GCC compiler installed on our machine. In this C tutorial, all the examples are compiled and tested using GCC compiler. Although we can...

3 minutes read.

Star Program in C Language

Star Program in C Star patterns are a sequence of * or any other character used to construct any pattern or any like-square geometric form, triangle, hollow square, pyramid, rhombus, etc.  Many programmers worldwide highly...

4 minutes read.

Actual and Formal Parameters

Any variable declared within the parenthesis is referred to as the parameters during the function declaration. Parameters tell the function about the argument datatype, their order, and the number of...

5 minutes read.

fopen() function in C

fopen() function, is one of the file handling functions. It is used to open the existing file and perform operations on the file. If the file does not exist, it...

3 minutes read.