I'm writing C89 on MSFT Visual Studio 2010 Beta. How can I make an assertion, similar to Java's assert
keyword? I think I need to define a macro, but I'm not sure how. (It seems like this is something that's been done before, so I'd rather use 开发者_StackOverflow社区that than try to roll my own.)
Here's a guess:
int assert(int truth_value) {
// crash the program with an appropriate error message
}
C89 has <assert.h>
, which contains the macro you're looking for.
#include <assert.h>
assert(expression);
From the documentation:
The assert() macro tests the given expression and if it is false, the calling process is terminated. A diagnostic message is written to stderr and the abort(3) function is called, effectively terminating the program.
If expression is true, the assert() macro does nothing.
精彩评论