Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.0k views
in Technique[技术] by (71.8m points)

c - Is #define supposed to add spaces around macros?

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

to

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

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Only much older preprocessors didn't insert that extra space -- note that original entry was submitted over 20 years ago in 1988, before the 1989 version of the C standard was standardized. You can pass the -traditional-cpp flag to the GCC preprocessor, which causes it to imitate the behavior of old-fashioned C preprocessors, as opposed to ISO C preprocessors.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...