#define 是否应该在宏周围添加空格? [英] Is #define supposed to add spaces around macros?

查看:26
本文介绍了#define 是否应该在宏周围添加空格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 http://www0.us.ioccc 查看该计划.org/1988/westley.c,在 另一个 SO 答案 - 它应该打印 pi 的值,大约 3.142,但是当我编译并运行它时,我得到 0.250.看起来当 GCC 预处理器(测试的 4.1.2 和 3.4.6)在代码上运行时,它会转换

I was looking at the program at http://www0.us.ioccc.org/1988/westley.c, mentioned in another SO answer - it's supposed to print the value of pi, about 3.142, but when I compile it and run it I get 0.250. It looks like when the GCC preprocessor (both 4.1.2 and 3.4.6 tested) runs on the code, it converts

#define _ -F<00||--F-OO--;
_-_-_

-F<00||--F-OO--;- -F<00||--F-OO--;- -F<00||--F-OO--;

但我认为,要让程序正常运行,它应该是

but I think, for the program to work, it should be

-F<00||--F-OO--;--F<00||--F-OO--;--F<00||--F-OO--;

即GCC 在宏"扩展之前插入了一个额外的空间.这是 #define 应该工作的方式吗?(自 1988 年以来这种情况发生了变化吗?)

i.e. GCC is inserting an extra space before the "macro" expansion. Is that the way #define is supposed to work? (Has that changed since 1988?)

编辑:另外,任何有关如何防止这些空间出现的信息都将不胜感激.

EDIT: Also, any information about how to prevent those spaces from showing up would be appreciated.

推荐答案

预处理器对标记进行操作,而不是严格的文本.所以从技术上讲,它不会在两者之间放置一个空格",但除非你明确告诉它将两个标记与 ## 运算符一起粘贴,否则它不会这样做.在这种情况下,跨宏行的两个 - 被计为两个不同的标记 - 都表示一元减号,而不是减量.

The preprocessor operates on tokens, not strictly text. So technically it doesn't "put a space" in between, but unless you explicitly tell it to paste two tokens together with the ## operator, it won't do it. In this case the two -'s across macro lines are counted as two different tokens - both meaning unary minus, not decrement.

另见:http://en.wikipedia.org/wiki/C_preprocessor#Token_concatenation

这篇关于#define 是否应该在宏周围添加空格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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