Wednesday, March 27, 2013

Define macro through command line

http://www.thegeekstuff.com/2012/05/c-macros/


$ gcc -Wall -DMACRO2 macro.c -o macro

Defining macros with values from command line

Not only the macros can be defined from command line (as shown in one of the sections above) but also they can be given values from command line. Lets take the following example :
#include <stdio.h>

int main(void)
{
#ifdef MACRO1 // test whether MACRO1 is defined...
    printf("\nMACRO1 Defined with value [%d]\n", MACRO1);
#endif

    return 0;
}
In the code above, the macro MACRO1 is being tested and its value is being used but it is not defined anywhere. Lets define it from the command line :
$ gcc -Wall -DMACRO1=25 macro.c -o macro
$ ./macro

MACRO1 Defined with value [25]

No comments:

Post a Comment