Latest web development tutorials

C library macro - assert ()

C standard library - <assert.h> C standard library - <assert.h>

description

C library macrovoid assert (int expression) allow diagnostic information is written to the standard error file.In other words, it can be used to add diagnostics in C programs.

statement

Here is the assert () macro statement.

void assert(int expression);

parameter

  • expression - it can be a variable or any C expression.If theexpression is TRUE, assert () does not perform any action.If theexpression is FALSE, assert () displays an error message on the standard error stderr, and aborts program execution.

return value

This macro does not return any value.

Examples

The following example demonstrates the assert () macro usage.

#include <assert.h>
#include <stdio.h>

int main()
{
   int a;
   char str[50];
	 
   printf("请输入一个整数值: ");
   scanf("%d\n", &a);
   assert(a >= 10);
   printf("输入的整数是: %d\n", a);
    
   printf("请输入字符串: ");
   scanf("%s\n", &str);
   assert(str != NULL);
   printf("输入的字符串是: %s\n", str);
	
   return(0);
}

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

请输入一个整数值: 11
输入的整数是: 11
请输入字符串: w3cschool 
输入的字符串是: w3cschool 

C standard library - <assert.h> C standard library - <assert.h>