What is the scope of a #define?
<p>What is the scope of a #define?</p>

<p>I have a question regarding the scope of a #define for C/C++ and am trying to bet understand the preprocessor. </p>

<p>Let's say I have a project containing multiple source and header files. Let's say I have a header file that has the following:</p>

<pre><code>// header_file.h

#ifndef __HEADER_FILE
#define __HEADER_FILE

#define CONSTANT_1          1
#define CONSTANT_2          2

#endif
</code></pre>

<p>Let's then say I have two source files that are compiled in the following order:</p>

<pre><code>// source1.c

#include header_file.h

void funct1(void)
{
    int var = CONSTANT_1;
}


// source2.c    

#include header_file.h

void funct2(void)
{
    int var = CONSTANT_2;
}
</code></pre>

<p>Assuming I have included all the other necessary overhead, this code should compile fine. However, I'm curious as to what #defines are remembered between compilations. When I compile the above code, are the contents of each #include actually included, or are the include guards actually implemented?</p>

<p>TLDR: Do #defines carry over from one compilation unit to the next? Or do #define only exist within a single compilation unit?</p>

<p>As I type this out, I believe I'm answering my own question and I will state my believed answer. #defines are constrained to a single compilation unit (.c). The preprocessor essentially forgets any #defines when it goes from one compilation unit to the next. Thus in the above example I listed, the include guards do not come into play. Am I correct in this belief?</p>