Command Line Macro Definitions in C

This C Tutorial explains Command Line Macro Definitions in C programming.

Linux gcc compiler provides the ability to define symbols on command line that initiates the compilation. This feature is useful when compiling different versions of a program from the same source file. For ex. size of an array on a machine with small memory defined to be small in contrast to machine with lots of memory. If the array is declared using symbol like this

    int name[ARR_SIZE];

ARR_SIZE can be defined on the command line when program is compiled. We have two ways to do this on gcc,


In the first form, ARR_SIZE is defined to be 1 while second form defines ARR_SIZE to be equal to value given on command line. Let’s see how can we use this on command line to compile a program,

    gcc -DARR_SIZE=100 xyz.c

The other benefit we get by defining the parametrizing quantities such as array sizes in the program. If array size were given as a literal constant or if array were used within a loop that used a literal constant as a limit, this technique would not work. Further, whenever array size is referenced, it must be a symbolic constant.

Compilers which offer command-line definitions of symbols usually offer command-line undefinition of symbols. On gcc, -U option performs this. For ex.,

Note: Join free Sanfoundry classes at Telegram or Youtube
    gcc -UARR_SIZE xyz.c

Sanfoundry Global Education & Learning Series – 1000 C Tutorials.

If you wish to look at all C Tutorials, go to C Tutorials.


Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.