C中#define预处理器的范围

时间:2023-01-17 15:31:08

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

#define的范围直到文件末尾。但它从哪里开始。基本上我尝试了以下代码。

 #include<stdio.h>
 #include<stdlib.h>
 #define pi 3.14
 void fun();
 int main()
{
 printf("%f \n",pi);
 #define pi 3.141516
    fun();
return 0;
}
void fun(){
printf("%f \n",pi);}

The output of the above program comes out to be

上述程序的输出结果如下

3.140000
3.141416

Considering preprocessing for main the value of pi should be 3.141516 and outside main 3.14. This is incorrect but please explain why.

考虑到主要的预处理,pi的值应该是3.141516并且在主3.14之外。这是不正确的,但请解释原因。

5 个解决方案

#1


16  

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.

C预处理器从上到下运行文件,并将#define语句视为美化的复制和粘贴操作。一旦遇到#define pi 3.14行,它就开始用3.14替换单词pi的每个实例。预处理器不处理(或甚至注意)括号和花括号等C语言范围机制。一旦看到#define,该定义就会生效,直到达到文件的末尾,用#undef取消定义宏,或者(如本例所示)用另一个#define语句重新定义宏。

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;.

如果你想要遵守C范围规则的常量,我建议在const float pi = 3.14;的行上使用更多的东西。

#2


5  

The scope of a #define is from the occurrence, to the end of the file, regardless of any intervening C scopes.

#define的范围是从文件的出现到结尾,而不管任何介入的C作用域。

#3


1  

Preprocessor has no concept of "scope" -- it manipulates the text of the program, without any idea of what the text is

预处理器没有“范围”的概念 - 它操纵程序的文本,而不知道文本是什么

Symbol is defined from its definition until the end of the compilation unit (a source file and and files it includes)

符号从其定义定义到编译单元结束(源文件及其包含的文件)

#4


1  

When you have preprocessor question:

当您有预处理器问题时:

gcc -E foo.c > foo.i; vim foo.i

gcc -E foo.c> foo.i; vim foo.i

#5


0  

As far as I know, the preprocessor uses #define statements in the order that it encounters them. In that case, your first printf statement correctly prints 3.14 and the second 3.141516 (is there a typo in the output from your program?).

据我所知,预处理器按照它们遇到的顺序使用#define语句。在这种情况下,你的第一个printf语句正确打印3.14和第二个3.141516(你的程序输出中是否有拼写错误?)。

#1


16  

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.

C预处理器从上到下运行文件,并将#define语句视为美化的复制和粘贴操作。一旦遇到#define pi 3.14行,它就开始用3.14替换单词pi的每个实例。预处理器不处理(或甚至注意)括号和花括号等C语言范围机制。一旦看到#define,该定义就会生效,直到达到文件的末尾,用#undef取消定义宏,或者(如本例所示)用另一个#define语句重新定义宏。

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;.

如果你想要遵守C范围规则的常量,我建议在const float pi = 3.14;的行上使用更多的东西。

#2


5  

The scope of a #define is from the occurrence, to the end of the file, regardless of any intervening C scopes.

#define的范围是从文件的出现到结尾,而不管任何介入的C作用域。

#3


1  

Preprocessor has no concept of "scope" -- it manipulates the text of the program, without any idea of what the text is

预处理器没有“范围”的概念 - 它操纵程序的文本,而不知道文本是什么

Symbol is defined from its definition until the end of the compilation unit (a source file and and files it includes)

符号从其定义定义到编译单元结束(源文件及其包含的文件)

#4


1  

When you have preprocessor question:

当您有预处理器问题时:

gcc -E foo.c > foo.i; vim foo.i

gcc -E foo.c> foo.i; vim foo.i

#5


0  

As far as I know, the preprocessor uses #define statements in the order that it encounters them. In that case, your first printf statement correctly prints 3.14 and the second 3.141516 (is there a typo in the output from your program?).

据我所知,预处理器按照它们遇到的顺序使用#define语句。在这种情况下,你的第一个printf语句正确打印3.14和第二个3.141516(你的程序输出中是否有拼写错误?)。