Reputation: 1243
What is the purpose of using #define to define a constant with no value?
Such as:
#define TOKEN
Upvotes: 3
Views: 57
Reputation: 8866
As @Nutomic says, this sort of #define
is useful for enabling/disabling certain bits of code. I've seen this used to create compile-time options for a given library:
#ifdef STRICT_PARSING
//...
#endif
#ifdef APPROXIMATE_PARSING
//...
#endif
#ifdef UNICODE //this is especially useful when trying to control Unicode vs ANSI builds
//call Unicode functions here
#else
//call Ansi functions here
#endif
#ifdef USE_STL //another good one, used to activate or deactivate STL-based bits of an API; the function declarations were dependent on USE_STL being defined. A plain-C API was exposed either way, but the STL support was nice too.
std::string MyApi();
#endif
Upvotes: 0
Reputation:
You can use it to enable or disable certain code using #ifdef
or #ifndef
, eg this:
#ifdef TOKEN
printf("token is defined");
#endif
One use for this would be to toggle logging in debug builds. Another would be include guards.
Upvotes: 4