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,
-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,
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.,
gcc -UARR_SIZE xyz.c
Sanfoundry Global Education & Learning Series – 1000 C Tutorials.
- Watch Advanced C Programming Videos
- Check C Books
- Apply for Computer Science Internship
- Practice Computer Science MCQs
- Practice BCA MCQs