而作C preprocessor条意见或先展开宏? [英] Does the C preprocessor strip comments or expand macros first?

查看:64
本文介绍了而作C preprocessor条意见或先展开宏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑这个(可怕的,可怕的,没有好,很不好)code结构:

Consider this (horrible, terrible, no good, very bad) code structure:

#define foo(x) // commented out debugging code

// Misformatted to not obscure the point
if (a)
foo(a);
bar(a);

我见过两种编译器'preprocessors产生这个code不同的结果:

I've seen two compilers' preprocessors generate different results on this code:

if (a)
bar(a);

if (a)
;
bar(a);

显然,这是一个便携式code基坏事。

Obviously, this is a bad thing for a portable code base.

我的问题:什么是应该与此做preprocessor?的Elid首先评论或首先展开的宏?

My question: What is the preprocessor supposed to do with this? Elide comments first, or expand macros first?

推荐答案

不幸的是,原来的 ANSI C规格明确排除在第4节的任何preprocessor特征(本规范描述只有C语言,未作任何规定无论是库或preprocessor)。

Unfortunately, the original ANSI C Specification specifically excludes any Preprocessor features in section 4 ("This specification describes only the C language. It makes no provision for either the library or the preprocessor.").

借助 C99规范处理这明确地,虽然。这些意见将被替换的转换阶段一个空间,其中preprocessing指令解析之前发生的。 (第6.10节了解详情)。

The C99 specification handles this explicity, though. The comments are replaced with a single space in the "translation phase", which happens prior to the Preprocessing directive parsing. (Section 6.10 for details).

VC ++ 和的GNU C编译器都遵循这种模式 - 其他编译器可能不兼容,如果他们年纪大了,但如果它的C99兼容的,你应该是安全的。

VC++ and the GNU C Compiler both follow this paradigm - other compilers may not be compliant if they're older, but if it's C99 compliant, you should be safe.

这篇关于而作C preprocessor条意见或先展开宏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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