Clever Geek Handbook
📜 ⬆️ ⬇️

assert.h

assert.h is the header file of the standard library of the C programming language , in which the macro of the C preprocessor assert() declared. This macro implements an exception that can be used to check the calculations made by the program.

Standard library
C programming language
  • assert.h
  • complex.h (C99)
  • ctype.h
  • errno.h
  • fenv.h (C99)
  • float.h
  • inttypes.h (C99)
  • iso646.h
  • limits.h
  • locale.h
  • math.h
  • setjmp.h
  • signal.h
  • stdalign.h (C11)
  • stdarg.h
  • stdatomic.h (C11)
  • stdbool.h (C99)
  • stddef.h
  • stdint.h (C99)
  • stdio.h
  • stdlib.h
  • stdnoreturn.h (C11)
  • string.h
  • tgmath.h
  • threads.h (C11)
  • time.h
  • uchar.h (C11)
  • wchar.h
  • wctype.h

Macros

The assert() macro adds a diagnostic routine to the program. After execution, if the expression is false (that is, the comparison result is 0), assert() writes the call information to the stderr stream and calls the abort() function. The information that is written in stderr includes:

  • source file name (predefined macro __FILE__ )
  • a line in the source file (predefined macro __LINE__ )
  • function in the source code (predefined macro __func__ ) (added in the C99 standard)
  • text of an expression whose value is zero 0


In order to disable the check, it is not necessary to exclude it from the code or comment on the macro declaration, you just need to declare another macro - NDEBUG in the program before #include <assert.h> :

  #define NDEBUG

then the macro declaration assert() will look like this:

  #define assert (ignore) ((void) 0)

and therefore will not affect the operation of the program.

The assert () macro is overridden each time assert.h connected, depending on the NDEBUG macro. [one]

The assert() macro is implemented as a macro, not a function. If the assert macro is used to call the assert function itself, then the correct functioning of the code is not guaranteed.

Usage Example

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

 int main ( void )
 {
     FILE * fd ;

     fd = fopen ( "/home/user/file.txt" , "r" );
     assert ( fd );
     fclose ( fd );

     return 0 ;
 }

In this example, the assert() macro will fire if the fopen() function fails.

See also

  • Approval (programming)

Links

  • assert.h - Basic Definitions, The Single UNIX® Specification , Issue 7 by The Open Group

Notes

  1. ↑ ISO / IEC 9899: 1999
Source - https://ru.wikipedia.org/w/index.php?title=Assert.h&oldid=91598226


More articles:

  • Devdariani, Gayoz Solomonovich
  • Douglas, Archibald, 5th Earl of Angus
  • List of Georgia Airports
  • List of Airports of Louisiana
  • Wait for it! (release 9)
  • Grishkovets, Evgeny Valeryevich
  • Geography of Yemen
  • Martin Beyerink
  • Chinese paper
  • Khovalyg, Vladislav Tovarishchtayovich

All articles

Clever Geek | 2019