Monday, 19 May 2014

C Library -

Post By: Hanan Mannan
Contact Number: Pak (+92)-321-59-95-634
-------------------------------------------------------

C Library - <assert.h>

Introduction

The assert.h header file of the C Standard Library provides a macro called assert which can be used to verify assumptions made by the program and print a diagnostic message if this assumption is false.
The defined macro assert refers to another macro NDEBUG which is not part of <assert.h>. If NDEBUG is defined as a macro name at the point in the source file where <assert.h> is included , the assertmacro is defined as follows:
#define assert(ignore) ((void)0)

Library Macros

Following is the only function defined in the header assert.h:
S.N.Function & Description
1void assert(int expression)
This is actually a macro and not a function, which can be used to add diagnostics in your C program.

Description

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

Declaration

Following is the declaration for assert() Macro.
void assert(int expression);

Parameters

  • expression -- This can be a variable or any C expression. If expression evaluates to TRUE, assert() does nothing. If expression evaluates to FALSE, assert() displays an error message on stderr and aborts program execution.

Return Value

This macro does not return any value.

Example

The following example shows the usage of assert() macro
#include <assert.h>
#include <stdio.h>

int main()
{
   int a;
   char str[50];
	 
   printf("Enter an integer value: ");
   scanf("%d\n", &a);
   assert(a >= 10);
   printf("Integer entered is %d\n", a);
    
   printf("Enter string: ");
   scanf("%s\n", &str);
   assert(str != NULL);
   printf("String entered is: %s\n", str);
	
   return(0);
}
Let us compile and run the above program in interactive mode as shown below:
Enter an integer value: 11
Integer entered is 11
Enter string: tutorialspoint 
String entered is: tutorialspoint 

0 comments: