Assert in C
Until recently I was unware of assert in C. It is a very powerful tool used in robust programming. For example to check for certain preconditions on entry to the function, most naiive C programmers use if statement.
for example:
if (c == NULL) {
printf("the pointer c is NULL");
exit(0);
}
But C has inbuilt library function which can do the same stuff in a very nice manner. For example,
#include
assert(c==NULL);
suppose if c==NULL, the program prints out at what line the assert failed and the value. And if you look at the
assert.h it has NDEBUG flag. If UNIX systems the gcc, cc should be compiled with -DNDEBUG option.
for example:
if (c == NULL) {
printf("the pointer c is NULL");
exit(0);
}
But C has inbuilt library function which can do the same stuff in a very nice manner. For example,
#include
assert(c==NULL);
suppose if c==NULL, the program prints out at what line the assert failed and the value. And if you look at the
assert.h it has NDEBUG flag. If UNIX systems the gcc, cc should be compiled with -DNDEBUG option.
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home