Reputation: 2121
I'm making a wrapper for the string parser, which converts string to primitive types. I'm writing unit tests for it, so I need the string representation of the extreme values of these primitive types, so it is needed to stringify the standard macros such as INT_MIN and INT_MAX.
The normal stringify macros look like this:
#define STRINGIFY(content) #content
#define EXPAND_AND_STRINGIFY(input) STRINGIFY(input)
It works well for EXPAND_AND_STRINGIFY(SHRT_MAX)
, which will be expanded to "32767".
However, when it going to work with EXPAND_AND_STRINGIFY(SHRT_MIN)
, it will be expanded to "(-32767 -1)", because #define SHRT_MIN (-SHRT_MAX - 1)
. This is not what I want. Is there any possible workarounds for it?
Upvotes: 3
Views: 524
Reputation: 241701
No, there is no way to get a preprocessor macro to evaluate an arithmetic expression.
You can get the preprocessor to do arithmetic evaluate, but only in the context of #if
, which allows you to evaluate a boolean expression. You can use that feature to rather laboriously output a number, but only by using preprocessor input in #include
d files, since you cannot put #if
inside a macro.
You don't mention why you want to stringify INT_MIN
, so I'm not in a position to offer an alternative, if indeed one exists.
However, it's most likely that your best bet is to simply produce the string at run-time, using snprintf
.
Upvotes: 4