What are Command Line Macro Definitions

«
»
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,

advertisement
    -DARR_SIZE
    -DARR_SIZE=value

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,

advertisement
    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.,

advertisement
    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.

advertisement
advertisement
advertisement
advertisement
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn