I need to display some relevant information in #error preprocessor directive. For example:
#define myConstant 5
#if myConstant > 10
#error myConstant has to be > 10, now %d //should display "5"
#endif
Run Code Online (Sandbox Code Playgroud)
How can I do that?
JL2*_*210 10
You can't. #error
doesn't allow/support that.
Instead, use _Static_assert
, from C11:
#define myConstant 5
#define STRINGIFY(x) STRINGIFY_(x)
#define STRINGIFY_(x) #x
_Static_assert(myConstant > 10, "myConstant has to be > 10, now " STRINGIFY(myConstant));
Run Code Online (Sandbox Code Playgroud)
Output:
Run Code Online (Sandbox Code Playgroud)test.c:5:1: error: static assertion failed: "myConstant has to be > 10, now 5" _Static_assert(myConstant > 10, "myConstant has to be > 10, now " STRINGIFY(myConstant)); ^~~~~~~~~~~~~~
归档时间: |
|
查看次数: |
111 次 |
最近记录: |