The purpose
More reliable program. For debugging
Definition
#include <assert.h>
void assert (scalar expression);
Description
If expression which shall have a scalar type is false ( = 0), The eassert() macro will wirte information about the particular call that failed on stderr and call abort().
That information include:
The text of argument
The name of the source file
The source file line number
The name of the enclosing function: the latter are, respectively, the values of the preprocessing macros __FILE__ and __LINE__ and of the identifier __func__.
No return value
No errors are defined
Expression should be true
Example
assert_test.c
/*
* =====================================================================================
*
* Filename: assert_test.c
*
* Description: Test macro assert()
*
* Version: 1.0
* Created: 01/22/2013 09:58:30 AM
* Revision: none
* Compiler: gcc
*
* Author: quyendv
* Company: dovanquyen.vn@gmail.com
*
* =====================================================================================
*/
#include <stdio.h>
#include <assert.h>
int main()
{
int x = 0;
printf("Enter a positive integer number x = ");
scanf("%d", &x);
assert(x >= 0);
printf("You have just entered x = %d", x);
return 0;
}
Result: