Ali Alzahrani
Ali Alzahrani

Reputation: 529

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?

Upvotes: 3

Views: 1113

Answers (2)

Jesse Rusak
Jesse Rusak

Reputation: 57188

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.

Upvotes: 2

Andrew Wilcockson
Andrew Wilcockson

Reputation: 133

In some versions of C, it is illegal to re-declare a macro unless the definition is exactly the same as the previous declaration.

Therefore this is simply defensive coding to prevent against this issue (although a quick test shows that this restriction does not appear in Objective-C).

Upvotes: 3

Related Questions