为什么一个函数中的预处理器指令会影响另一个函数的编译? [英] Why does the preprocessor directive in one function affect the compilation of another?

查看:62
本文介绍了为什么一个函数中的预处理器指令会影响另一个函数的编译?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下程序成功编译并打印 1000 ,甚至没有从我们的 main()中调用 foo()函数, / code>函数。

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

#include<stdio.h>


void foo()
{
    #define ans 1000
}

int main() {

  printf("%d", ans);
  return 0;
}


推荐答案

#define 由预处理器运行,该预处理器在编译器之前进行。预处理程序完成后,代码将如下所示:

#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)

在主程序中同时包含此文件和stdio.h时会发生什么?好吧,stdio.h将被包含两次。这就是debug.h应该看起来像这样的原因:

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

文件stdio.h具有相同的构造。这里最主要的是,它是在编译器之前运行的。 define是一个简单的替换命令。它对范围或类型一无所知。但是,正如您在此处看到的那样,其中内置了一些基本逻辑。预处理程序要做的另一件事是删除所有注释。

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.

您可以在此处阅读有关C预处理程序的更多信息: http://www.tutorialspoint.com/cprogramming/c_preprocessors.htm

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

这篇关于为什么一个函数中的预处理器指令会影响另一个函数的编译?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆