English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

C Language Basic Tutorial

C Language Flow Control

C Language Functions

C Language Arrays

C Language Pointers

C Language Strings

C Language Structure

C Language File

C Others

C Language Reference Manual

C Library Macro assert()

C Standard Library - <assert.h>

C Library Macro void assert(int expression) It allows diagnostic information to be written to the standard error file. In other words, it can be used to add diagnostics to C programs.

Declaration

Below is the declaration of the assert() macro.

void assert(int expression);

Parameter

  • expression --  this can be a variable or any C expression. If expression If TRUE, assert() does not perform any action. If expression If FALSE, assert() will display an error message on standard error stderr and terminate the program execution.

Return Value

This macro does not return any value.

Online Example

The following example demonstrates the usage of the assert() macro.

#include <assert.h>
#include <stdio.h>
 
int main()
{
   int a;
   char str[50];
     
   printf("Please input an integer value: \t");
   scanf("%d", &a);
   assert(a >= 10);
   printf("The input integer is: \t%d\n", a);
    
   printf("Please input a string: \t");
   scanf("%s", str);
   assert(str != NULL);
   printf("The input string is: \t%s\n", str);
    
   return(0);
}

Let's compile and run the above program in interactive mode as follows:

Please enter an integer value: 23
The input integer is: 23
Please enter a string: w3codebox 
The input string is: w3codebox

C Standard Library - <assert.h>