문제

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 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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top