Why does the preprocessor directive in one function affect the compilation of another?

前端 未结 3 2085
温柔的废话
温柔的废话 2021-01-20 01:03

Following program compiles successfully and print 1000 without even calling a foo() function from our main() function. How is it p

相关标签:
3条回答
  • 2021-01-20 01:06

    The #define is processed by the preprocessor before the compiler does anything. It is a simple text replacement. The preprocessor doesn't even know if the line of code is inside or outside a function, class or whatever [Ref: https://stackoverflow.com/a/36968600/5505997]. Clearly you do not need to call the function to set the value and obviously you will not get any error during compile.

    0 讨论(0)
  • 2021-01-20 01:07

    #defineis run by the preprocessor which is staged before the compiler. After the preprocessor is done, the code will look like this:

    /* Everything that is inside stdio.h is inserted here */
    
    void foo()
    {
    }
    
    int main() {
    
      printf("%d", 1000);
      return 0;
    }
    

    And this is what actually get compiled.

    The preprocessor is very important to make header files work. In them, you see this structure:

    #ifndef foo
    #define foo
    /* The content of the header file */
    #endif
    

    Without this, the compiler would complain if a header file is included more than once. You may ask why you would want to include a header file more than once. Well, header files can include other header files. Consider this macro, which is useful for debugging. It prints the name of the variable and then the value. Note that you would have to do a separate version for different types.

    #define dbg_print_int(x)  fprintf(stderr, "%s = %d", #x, x)
    

    This is pretty versatile, so you may want to include it in a header file for own use. Since it requires stdio.h, we include it.

    /* debug.h */
    #include <stdio.h>
    #define dbg_print_int(x)  fprintf(stderr, "%s = %d", #x, x)
    

    What happens when you include this file and also include stdio.h in you main program? Well, stdio.h will be included twice. That's why debug.h should look like this:

    /* debug.h */
    #ifndef DEBUG_H
    #define DEBUG_H
    #include <stdio.h>
    #define dbg_print_int(x)  fprintf(stderr, "%s = %d", #x, x)
    #endif
    

    The file stdio.h has the same construct. The main thing here is that this is run before the compiler. The define is a simple replacement command. It does not know anything about scope or types. However, as you can see here, there is some basic logic built into it. Another thing that the preprocessor does is to remove all the comments.

    You can read more about the C preprocessor here: http://www.tutorialspoint.com/cprogramming/c_preprocessors.htm

    0 讨论(0)
  • 2021-01-20 01:16

    As others have stated, #define is a preprocessor directive, not C source code. See Wiki here.
    Point being, in your code #define ans 1000 is not a variable definition, meaning that even if you were calling foo() in the main, you would still not be setting "ans" at runtime, because it is simply not a variable. It is just telling the preprocessor what to do with the "label" "ans", when it finds it in your source code.

    In this example, the main() will essentially be calling an empty foo() function:

    int main() 
    {
      foo();              // Calls an empty function
      printf("%d", ans);  // ans will have been substituted by 1000 by the time you start executing you code
      return 0;
    }
    

    The definition of "ans" will simpy not exist anymore by the time you start executing you main(). This is what the preprocessor does (in part). It finds all the #defines declared in your entire source code and tries to find places in your code where you have used these defines. If you have not used them, it moves on (don't care), if you have, it substitutes the label by the actual defined value.

    0 讨论(0)
提交回复
热议问题