dasblinkenlight pretty much covers it, but I would just clarify some of the things they say, macros really don't define constants, you do this with the const modifier, and you should use this most of the time. A macro defines substitution text, the only thing it knows about the language you write in is using spaces. So for macros you can do something like this
#define CODE_SNIPPET 10; i++ ) printf( "Hello %d\n" for( int i = 0; i < CODE_SNIPPET , i );
which will be converted by the preprocessor to
for( int i = 0; i < 10; i++ ) printf( "Hello %d\n" , i );
It is very powerful, but clearly open to abuse. I very rarely use macros for constants, instead I will define the values ββas const in the .c file, and then declare it extern in the header, as
const int kAnswerToEveryThing = 42;
I have one project that I use macros quite a bit, this is not c in Objective-C, but the macro system is identical, and I defined macros that extend into class methods that add metadata to the class that should be used by my library so that it performed the same functions as Java annotations.
Nathan day
source share