×

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

Example of Iteration in C

The iterations in the C language are the statements which are executed number of times until a certain condition is reached. These iterations are regarded as “Loops” in C language....

3 minutes read.

Simple hash() function in C

Introduction In this context, we briefly discuss HASH FUNCTION, HASHING or HASH TABLE in C. It is a function used to map data and mapped arbitrary sizes to the fixed-size values. The...

7 minutes read.

Different ways to Declare the Variable as Constant in C

There are a wide range of ways of making the variable as steady Using const keyword: The const catchphrase permits a software engineer to inform the compiler that a specific variable should...

5 minutes read.

Passing Array to Function in C

Need to pass Arrays? The need to pass arrays to a function arises when we need to pass a list of values to a given function. During the course of our programming...

4 minutes read.

Types of Function in C

A function is a piece of code that accomplish a certain operation for a specific task. There are two types of functions in C: System-defined function (Library Function)User-defined function 1. System defined Function...

8 minutes read.

First Fit Program in C

This is one of the easiest ways to allocate memory. The main goal is to divide the memory into several fixed sizes. In C, there is only one process for...

3 minutes read.

Difference between Scope and Lifetime in C

In this article, we will discuss about the key differences between scope and lifetime in C, including their definitions, applications, and how they relate to one another. By the end...

8 minutes read.

C Token

C Token and Keyword The C tokens are the basic buildings blocks in C language which are constructed together to write a C program. Each and every smallest individual unit in the C program is...

2 minutes read.

memcmp() in C

Introduction: In this article we are discuss about memcmp() function in C. This function permits the person to evaluate the bytes of the two characters, as mentioned above. Depending on...

4 minutes read.

Purpose of a Function Prototype in C

A function prototype in C is a declaration of a function that specifies the function's name, return type and parameters. It has the following syntax: return_type function_name(parameter_list); For example, the prototype for...

4 minutes read.

Pyramid Pattern in C

Pyramid Pattern in C A pyramid is a polyhedron created by connecting a base that will be a polygon, and all the lateral faces are triangles. All the bases are connected...

5 minutes read.

Null character in C

Introduction The Null character in the C programming language is used to terminate the character strings. In other words, the Null character is used to represent the end of the string...

3 minutes read.

Displaying Array in C

Reference a collection of variables of similar data type in an array using a single element. The parts are stored next to each other. When declaring an array, you must specify...

4 minutes read.

C Math Library

C Math Library The <math.h> header defines various mathematical functions and one macro. Many functions are available in this library to take double as an argument and then return double as...

4 minutes read.

Difference between C and Java

C programming and Java programming are two of the earliest programming languages. C programming follows a procedural approach whereas Java programming follows an object-oriented approach. Java programming is a part...

3 minutes read.

Pseudo Code in C

Pseudo code in C can be referred to as a simple way or method to write programming code or program algorithm in English. A pseudocode is an informal representation of...

4 minutes read.

#include in C

In the C programming language, #include is another way of inferring a standard, or a user defines file into the application or program. The preprocessor of the programming standard generally...

4 minutes read.

Sizeof in C

A pointer in C is defined as a variable that stores the information of the address of another variable. A pointer can be incremented or decremented, which means we can...

4 minutes read.

C Array

An array is a collection of elements that are used to hold the fixed number of values of the same type. We cannot change the size and type of array...

2 minutes read.

Array to function in C

Array to function in C We can create functions that receive an array as an argument. To pass an array into a function, we need to write the name of the...

4 minutes read.