Scope of macros in C?

后端 未结 7 1569
不思量自难忘°
不思量自难忘° 2020-12-17 08:03

How these macros are evaluated?

# define i 20
void fun();

int main(){
  printf(\"%d\",i);
  fun();
  printf(\"%d\",i);
  return 0;
}

void fun(){
  #undef          


        
相关标签:
7条回答
  • 2020-12-17 08:38

    Macros are evaluated during the C preprocessor stage. The C preprocessor stage occurs separately from the compilation stage. Because of this, Macros do not follow regular scope. They are instead evaluated in the order in which they appear in the source file (so from the top to the bottom).

    In your first code example it outputs 2020, despite it calling fun() in the main function which supposedly should change the value of i to 30, but because the fun function appears below where it was called, the value does not change as the preprocessor has not reached that point yet.

    In your second code example it outputs 3030 because the fun function is above the main function. Therefore the opposite occurs as the preprocessor has already gone through the fun function and changed the value of i to 30

    0 讨论(0)
  • 2020-12-17 08:47

    C Preprocessor works top to bottom irrespective of function calls. It is effective from that point (line) in whatever the file that macro is defined, until corresponding undef or till end of the translation unit.

    So, your code would become,

    # define i 20
                   // from now on, all token i should become 20
    void fun();
    int main()
    {
      printf("%d",i);   // printf("%d",20);
      fun();
      printf("%d",i);   // printf("%d",20);
      return 0;
    }
    void fun()
    {
    #undef i
                  // from now on, forget token i
    #define i 30
                  // from now on, all token i should become 30
    }
    

    Your second code would become,

    # define i 20
                   // from now on, all token i should become 20
    void fun()
    {
    #undef i
                   // from now on, forget i
    #define i 30
                   // from now on, all token i should become 30
    }
    int main()
    {
      printf("%d",i);    //  printf("%d",30);
      fun();
      printf("%d",i);    // printf("%d",30);
      return 0;
    }
    
    0 讨论(0)
  • 2020-12-17 08:49

    Preprocessor macros have no scope, as they are not part of the C language. Instead it's a kind of search-replace program that is run before the compiler proper runs.

    The preprocessor simply goes though any file, doesn't have to be a C source file, and when it finds a macro invocation it simply replaces it with the text in the macro body.

    0 讨论(0)
  • 2020-12-17 08:52

    There is simply no scope.

    Macros are substituted by the preprocessor. So their expansion is defined by their position in the source, top to bottom.

    0 讨论(0)
  • 2020-12-17 08:55

    Macros take effect on the text of the source, in a separate stage before compiling. The macros no longer exist in any form in the compiled code, and they are not evaluated at runtime.

    Whatever macro definition is in effect when the macro is invoked (during a textual scan of the source) will be substituted into the source text.

    0 讨论(0)
  • 2020-12-17 09:02

    There's no scope involved at all. Macros are handled at the preprocessing stage separately and independently from the compilation stage and have no concept of a C scope. Your examples could just as easily be:

    #define i 20
    
    void fun();
    
    int main()
    {
      printf("%d",i);
      fun();
      printf("%d",i);
      return 0;
    }
    
    void fun()
    {
    }
    
    #undef i
    #define i 30
    

    And:

    #define i 20
    #undef i
    #define i 30
    
    void fun()
    {
    }
    
    int main()
    {
      printf("%d",i);
      fun();
      printf("%d",i);
      return 0;
    }
    

    You can see from those why it behaves the way it does.

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