annaranking.blogg.se

Macro definition in c
Macro definition in c








In such big projects, to avoid including a single header more than once (directly and indirectly through another header file) a macro is defined in the original header and this macro is tested before including the header anywhere so as to be sure that if the macros is already defined then there is no need to include the header as it has already been included (directly or indirectly). Well, mostly these type of testing macros are used in a big project involving many source and header files. Now, one would question where are these testing macros used. Since both of the macros are defined so both the printf statements executed. Now, if we look at the output, we will see : $. Inside the main function, the macros are used only in testing conditions.As clear from the definition, the macros are without any values.

#Macro definition in c code

So, the above code just defines two macros MACRO1 and MACRO2.#ifdef MACRO2 // test whether MACRO2 is defined. #ifdef MACRO1 // test whether MACRO1 is defined. As an example, lets look at the following piece of code : #include The most basic use of macros is to define them without values and use them as testing conditions. Lets understand the concept of macros using some example codes. Macros can also be defined without any value or piece of code but in that case they are used only for testing purpose.

macro definition in c

This is where the concept of a macro fits in.Ī Macro is typically an abbreviated name given to a piece of code or a value. There has to be a way out through which one can make the change at one place and it would get reflected at all the places.

macro definition in c

Then changing the value all over the code does not make any sense.

macro definition in c

Also there is a possibility that the in future, the piece of code or value would change. Sometimes while programming, we stumble upon a condition where we want to use a value or a small piece of code many times in a code.








Macro definition in c