开发者

Assertions in C89?

开发者 https://www.devze.com 2022-12-19 07:40 出处:网络
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 someth

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号