assert() Function Example program in C

By: Jagan Viewed: 153739 times    

The macro assert() can diagnose program bugs. It is defined in ASSERT.H, and its prototype is

void assert(int expression);

The argument expression can be anything you want to test--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.

How do you use assert()? It is most frequently used to track down program bugs (which are distinct from compilation errors). A bug doesn't prevent a program from compiling, but it causes it to give incorrect results or to run improperly (locking up, for example). For instance, a financial-analysis program you're writing might occasionally give incorrect answers. You suspect that the problem is caused by the variable interest_rate taking on a negative value, which should never happen. To check this, place the statement

assert(interest_rate >= 0);

at locations in the program where interest_rate is used. If the variable ever does become negative, the assert() macro alerts you. You can then examine the relevant code to locate the cause of the problem.

To see how assert() works, run the sample program below. If you enter a nonzero value, the program displays the value and terminates normally. If you enter zero, the assert() macro forces abnormal program termination. The exact error message you see will depend on your compiler, but here's a typical example:

Assertion failed: x, file list19_3.c, line 13

Note that, in order for assert() to work, your program must be compiled in debug mode. Refer to your compiler documentation for information on enabling debug mode (as explained in a moment). When you later compile the final version in release mode, the assert() macros are disabled.

Using the assert() macro.

1: /* The assert() macro. */
2:
3: #include <stdio.h>
4: #include <assert.h>
5:
6: main()
7: {
8:     int x;
9:
10:     printf("\nEnter an integer value: ");
11:     scanf("%d", &x);
12:
13:     assert(x >= 0);
14:
15:     printf("You entered %d.\n", x);
16:     return(0);
17: }
Enter an integer value: 10
You entered 10.
Enter an integer value: -1
Assertion failed: x, file list19_3.c, line 13
Abnormal program termination

Your error message might differ, depending on your system and compiler, but the general idea is the same.

ANALYSIS: Run this program to see that the error message displayed by assert() on line 13 includes the expression whose test failed, the name of the file, and the line number where the assert() is located.

The action of assert() depends on another macro named NDEBUG (which stands for "no debugging"). If the macro NDEBUG isn't defined (the default), assert() is active. If NDEBUG is defined, assert() is turned off and has no effect. If you placed assert() in various program locations to help with debugging and then solved the problem, you can define NDEBUG to turn assert() off. This is much easier than going through the program and removing the assert() statements (only to discover later that you want to use them again). To define the macro NDEBUG, use the #define directive. You can demonstrate this by adding the line

#define NDEBUG

to Listing above, on line 2. Now the program prints the value entered and then terminates normally, even if you enter -1.

Note that NDEBUG doesn't need to be defined as anything in particular, as long as it's included in a #define directive.

Most Viewed Articles (in C )

Latest Articles (in C)

Comment on this tutorial

Subscribe to Tutorials

Related Tutorials

Archived Comments

1. Nice article..
View Tutorial          By: senthil at 2009-09-09 00:22:40

2. awesome. thanks for the info
View Tutorial          By: Darren at 2010-05-09 19:55:22

3. "If you enter zero, the assert() macro forces
View Tutorial          By: czx at 2010-09-17 07:23:20

4. I am really grateful for presenting such an easy t
View Tutorial          By: Rizwan Fazal at 2010-09-22 09:23:47

5. very usefull article............ thanks to the aut
View Tutorial          By: runa at 2010-10-16 08:43:38

6. Very nice article.... explained the concept very n
View Tutorial          By: prashant yeole at 2010-12-16 03:15:07

7. Excellent explanation , thank you.
View Tutorial          By: Tesfay K.mariam at 2011-03-31 05:31:46

8. Thanks for this article.
View Tutorial          By: Arindam Dey at 2011-06-16 05:23:17

9. Awesome Explanation...:-)
View Tutorial          By: satz at 2011-06-28 01:45:56

10. Hi Its very useful ad nice article Thanks
View Tutorial          By: Hanish at 2011-07-22 13:12:23

11. thanks nice article , but there is bug as already
View Tutorial          By: YZ at 2011-08-05 13:10:49

12. nice article ..........
View Tutorial          By: Indra at 2011-08-27 12:17:25

13. Thanks for a nice article !!!
View Tutorial          By: Piyush Sharma at 2011-12-15 12:24:06

14. simple and clear..
View Tutorial          By: sudhesh at 2011-12-22 04:05:14

15. Hi Jgan,
Its very useful ad nice article .

View Tutorial          By: pramod at 2012-01-16 13:21:35

16. Very consise and to the point. Thanks.
View Tutorial          By: Borota Igor at 2012-02-20 09:45:59

17. Thanks ... nice one with clear picture.
View Tutorial          By: Nithin at 2012-04-01 06:58:41

18. Informative Article. Concept is explained Very Nic
View Tutorial          By: GrpTechi at 2012-04-20 11:15:21

19. Thanks Jagan!!!!!!
View Tutorial          By: Maahe at 2012-07-06 05:22:52

20. Thanks Jagan!!!!!!
View Tutorial          By: Maahe at 2012-07-06 05:23:51

21. Good one.........!!!
View Tutorial          By: Shailesh Nakade at 2012-07-11 09:48:33

22. thnx...nice article...useful..
View Tutorial          By: anish at 2012-11-30 05:41:47

23. Really awesome keep posting ........
View Tutorial          By: viswanath at 2013-01-30 07:38:13

24. Thanks...Really nice...
View Tutorial          By: Raj at 2013-02-07 08:58:28

25. I can't compile the source with Dev-C++ 4.9.9.2, h
View Tutorial          By: ToKha at 2013-11-19 01:32:34

26. This example is very useful....Thanks for your sha
View Tutorial          By: raji at 2015-05-22 07:19:07

27. Its very usefull to clear understanding for us .Th
View Tutorial          By: vijay at 2015-07-16 09:29:40