Scope of #define preprocessor in C

前端 未结 6 1853
孤街浪徒
孤街浪徒 2020-12-09 09:19

The scope of #define is till the end of the file. But where does it start from. Basically I tried the following code.

 #include
          


        
6条回答
  •  醉话见心
    2020-12-09 09:48

    The C preprocessor runs through the file top-to-bottom and treats #define statements like a glorified copy-and-paste operation. Once it encounters the line #define pi 3.14, it starts replacing every instance of the word pi with 3.14. The pre-processor does not process (or even notice) C-language scoping mechanisms like parenthesis and curly braces. Once it sees a #define, that definition is in effect until either the end of the file is reached, the macro is un-defined with #undef, or (as in this case) the macro is re-defined with another #define statement.

    If you are wanting constants that obey the C scoping rules, I suggest using something more on the lines of const float pi = 3.14;.

提交回复
热议问题