Say I have five files: main.c, sample1.c, sample1.h, sample2.c
and sample2.h
, where in each of these files DEBUG_PRINTS
is defined as follows:
#ifndef DEBUG_PRINTS
#define DEBUG_PRINTS 0
#endif
and the code is compiled using gcc:
gcc -Wall main.c sample1.c sample2.c -o main
Now I change the value of DEBUG_PRINTS
in one file to 1
. Without printing to the terminal how can you determine, which value will be applied?
Additionally, how can I define DEBUG_PRINTS
locally per file - the same as using the static
keyword for variables?
Each file is compiled separately. Macros from one file are not visible in any other file. Once the files are independently compiled, the resulting objects are linked together to create an executable.
#ifndef
means if the macro isn't defined at all in that file before or in an header you included. In your case, it defaults to 0.
If you change DEBUG_PRINTS
in that one file to 1
, it will override that #define
in #ifndef
found in the header file. You are basically locally defining or overriding the default DEBUG_PRINTS
in the imported header file to 1
.
Otherwise, macros are visible only to this file or header.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.