Search code examples
objective-cpreprocessor

What is the benefit of putting #undef before #define?


I found these lines:

#undef TAG
#define kTAG @"TestRegistration///: "
#define TAG kTAG

If TAG never been used, why do they #undef TAG before they #define it?


Solution

  • There's no reason to do it if the macro wasn't previously defined. If you expect the macro could be previously defined, though, it's a good idea to undef it first because it makes it clear to the reader of the code that you intend to override the previously-set value of the macro (if any).

    If you don't expect the macro could already be defined, you shouldn't use the undef first. That way, if it is (surprisingly!) already-defined, you'll get a warning.