If I have a C file foo.c and while I have given -DMACRO=1
as command line option for compilation. However, if within the header file also I have
#define MACRO 2
Which of these will get precedence?
Function-like macros can take arguments, just like true functions. To define a macro that uses arguments, you insert parameters between the pair of parentheses in the macro definition that make the macro function-like. The parameters must be valid C identifiers, separated by commas and optionally whitespace.
Speed versus size The main benefit of using macros is faster execution time. During preprocessing, a macro is expanded (replaced by its definition) inline each time it is used. A function definition occurs only once regardless of how many times it is called.
Macros are used to make a sequence of computing instructions available to the programmer as a single program statement, making the programming task less tedious and less error-prone. (Thus, they are called "macros" because a "big" block of code can be expanded from a "small" sequence of characters.)
The command line options apply ahead of any line read from a file. The file contents apply in the order written. In general, you will get at least a warning if any macro is redefined, regardless of whether the command line is involved. The warning may be silenced if the redefinition doesn't matter, perhaps because both definitions are identical.
The right way to answer a question like this is to build a small test case and try it. For example, in q3965956.c put the following:
#define AAA 2
AAA
and run it through the C preprocessor, perhaps with gcc -E
:
C:>gcc -DAAA=42 -E q3965956.c # 1 "q3965956.c" # 1 "" # 1 "" # 1 "q3965956.c" q3965956.c:1:1: warning: "AAA" redefined :1:1: warning: this is the location of the previous definition 2 C:>
You can see from the output that the macro expanded to the value given by the #define
in the file. Furthermore, you can see from the sequence of #
directives that built-in definitions and the command line were both processed before any content of line 1 of q3965956.c
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With