Easy Tutorial
❮ C Exercise Example50 C Do While Loop ❯

C Library Macro - assert()

C Standard Library - <assert.h>

Description

The C library macro void assert(int expression) allows diagnostic information to be written to the standard error file. In other words, it can be used to add diagnostics to a C program.

Declaration

Here is the declaration for the assert() macro.

void assert(int expression);

Parameters

Return Value

This macro does not return any value.

Example

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

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

int main()
{
   int a;
   char str[50];

   printf("Please enter an integer value: ");
   scanf("%d", &a);
   assert(a >= 10);
   printf("The entered integer is: %d\n", a);

   printf("Please enter a string: ");
   scanf("%s", str);
   assert(str != NULL);
   printf("The entered string is: %s\n", str);

   return(0);
}

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

Please enter an integer value: 11
The entered integer is: 11
Please enter a string: tutorialpro
The entered string is: tutorialpro

C Standard Library - <assert.h>

❮ C Exercise Example50 C Do While Loop ❯