learnprogramming123

LEARN PROGRAMMING 123
your resources to programming and free tutition



Navigation

 
C
 
C++ soon here!
a+ - soon here!

Search

 


Next Back

 

<assert.h>        -        Diagnostics

void assert(expression);

The assert() macro inserts diagnostics into programs. When it is executed, if expression is false (that is, compares equal to 0), assert() writes information about the particular call that failed (including the text of the argument, the name of the source file and the source file line number - the latter are respectively the values of the preprocessing macros __FILE__ and __LINE__) on stderr and calls abort().

Forcing a definition of the name NDEBUG, either from the compiler command line or with the preprocessor control statement #define NDEBUG ahead of the #include <assert.h> statement, will stop assertions from being compiled into the program.

/* program to demonstrate the assert function */

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

void test(char character)
{
    /* assertion character is lower case letter or digit */
    assert(islower(character) || isdigit(character));
    printf("character %c lower case or digit\n", charactr);
}

int main(void)
{
    test('3');
    test('a');
    test('l');

    return 0;
}

Restults:

character 3 lower case or digit
character a lower case or digit
Assertion failed: islower(character) || isdigit(character), file p1.c, line 12

go to <ctype.h>        back to top        back to main