×

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

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.

Find reverse of an array in C using functions

Introduction: Here, we describe how to find the reverse array in C using functions. Suppose you have an array with n elements. I need to display the elements present in...

3 minutes read.

Prototype in C

A prototype is nothing but a model, a model of initial creation of an intended product. Similarly, in the C programming language, all functions have a prototype. In general, all...

4 minutes read.

Memory leak in C

What is memory leak in C? Memory leak occurs when we keep allocating memory in the heap without freeing it, i.e., the allocated memory in heap is not released back to...

3 minutes read.

Doubly Linked list in C

To know the Doubly Linked List in C, first we should know about how the Linked List works. Linked List The Linked list is the linear data structure. In the Linked...

5 minutes read.

Pascal Triangle in C

The pascal triangle in c is an array of binomial coefficients in triangular form. Here the nth row contains the binomial coefficient of ncr.In a pascal triangle, every number is...

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.

Multi-dimensional Arrays in C

Multi-dimensional Arrays in C The C programming allows the concept of multi-dimensional arrays. The data within the multidimensional arrays are stored in the tabular form, that is, in a row significant...

3 minutes read.

Formatted Input and output function in C

Formatted I/O functions display multiple outputs to the user by taking various inputs. All data types like int, float, char and double are supported by the formatted I/O function. In...

4 minutes read.

Infinite loop in C

Definition of infinite loop The infinite loop is a loop that does not get dismissed because of its looping construct. A continuous output or no output are the two possible outcomes...

3 minutes read.

Ferror() in c

Ferror():  In C, the ferror() function checks assuming there is a blunder in the given stream. The ferror() function is utilized to check for the document blunder on given stream. A return...

4 minutes read.

Difference between If Else and Nested If Else in C

What is the If Else statement in C? In C programs, if else conditions are used to perform some operations based on specific conditions. The operation is only executed if the...

6 minutes read.

Integer Promotions in C

As we know that some of the data types such as char, short int, Enum takes a smaller number of bytes than compared to int. When an operation is applied...

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.

Loop Questions in C

Question 1: What is For loop syntax? Ans: The syntax that has been used for the ‘for’ loop in C programming language is: for (initialization statement /*for providing a value to variables*/; test...

19 minutes read.

C pre-processor

The C processor is a macro processor which is used to compile the source code of the program (step by step) . It is not a part of the compiler....

4 minutes read.

Structure Pointer in C

Structure Pointer in C In the C programming language, a structure pointer is defined as a pointer that points to the memory block address that stores a structure. Like C standard...

4 minutes read.

Loop Statement in C

Loop statement is used to execute one or more statement repeatedly multiple times. There are three types of loops in C language. Why use loop? We can use loop because it executes a...

2 minutes read.

How to lock top row in Excel

How to lock top row in excel While working with an Excel spreadsheet, the user utilizes the rows and columns to enter the details under various headings. Sometimes while scrolling the...

4 minutes read.

Segmentation Fault in C

Segmentation Fault in C In the C programming language, segmentation fault or segmentation violation or core dump is a condition that the hardware component raises to protect the memory by indicating...

4 minutes read.