C预处理程序进行几次? [英] How many passes does the C preprocessor make?

查看:71
本文介绍了C预处理程序进行几次?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C预处理程序对代码进行了几次传递?

How many passes does the C preprocessor make over the code?

我在gcc 4.7.2上测试了以下代码

I tested following code on gcc 4.7.2

#define a 5
#define b a
#define c b
#define d c
#define e d
#define f e
#define g f
#define h g
#define j h
#define k j
#define l k
#define m l

int main(void) {return d;}

没有错误:

$ gcc -E 1.c
# 1 "1.c"
# 1 "<command-line>"
# 1 "1.c"
# 14 "1.c"
int main(void) {return 5;}

这是标准行为吗?

推荐答案

C预处理程序继续运行直到没有其他可扩展的内容为止。这不是通行证的问题;

The C preprocesor keeps going until there's nothing more to expand. It isn't a question of passes; it's a question of completeness.

它确实避免了宏的递归扩展。宏一旦扩展一次,就不会在替换文本中重新扩展。

It does avoid recursive expansion of macros. Once a macro has been expanded once, it is not re-expanded in the replacement text.

唯一的标准有关宏扩展限制的说明,请参见第5.2.4.1节翻译限制,其中指出:

The only thing the standard says about limits on macro expansion is in §5.2.4.1 Translation limits, where it says:


实现应能够翻译和执行至少一个
包含以下每个限制中至少一个实例的程序: 18)

...


  • 4095在一个预处理翻译单元中同时定义的宏标识符

18)实现应尽可能避免施加固定的翻译限制。

18) Implementations should avoid imposing fixed translation limits whenever possible.

因此,预处理器必须至少能够处理4095个宏,并且如果其中一个宏以外的所有宏都按顺序扩展到另一个宏(如您的示例),则结果必须正确。

So, the preprocessor must be able to handle at least 4095 macros, and if all but one of those macros expand to another macro sequentially, like in your example, the result must be correct.

这篇关于C预处理程序进行几次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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