69,373
社区成员
发帖
与我相关
我的任务
分享
#define MEAN(x, y) (x + y) / 2 //compiled in code::block
int main (void)
{
printf ("%d\n", MEAN (1, 3)); //print: 2
return 0;
}
# define identifier lparen identifier-list_opt ) replacement-list new-line
# define identifier lparen ... ) replacement new-line
# define identifier lparen identifier-list , ... ) replacement-list new-line
defines a function-like macro with arguments, similar syntactically to a function call. The parameters are specified by the optional list of identifiers, whose scope extends from their declaration in the identifier list until the new-line character that terminates the #define preprocessing directive. Each subsequent instance of the function-like macro name followed by a ( as the next preprocessing token introduces the sequence of preprocessing tokens that is replaced by the replacement list in the definition (an invocation of the macro). The replaced sequence of preprocessing tokens is terminated by the matching ) preprocessing tokens. Within the sequence of preprocessing tokens making up an invocation of a function-like macro, new-line is considered a normal white-space character.
ps:抄错或者打错字本人概不负责。。
#define MEAN(x, y) (x + y) / 2
int main (void)
{
printf ("%d\n", MEAN (1,3));
return 0;
}
[/quote]
恩,编译器还是做了点工作[/quote]
编译器干啥了?宏展开不是预处理器干滴吗?
#define MEAN(x, y) (x + y) / 2
int main (void)
{
printf ("%d\n", MEAN (1,3));
return 0;
}
[/quote]
恩,编译器还是做了点工作
#define MEAN(x, y) (x + y) / 2
int main (void)
{
printf ("%d\n", MEAN (1,3));
return 0;
}
//compiled in code::block
#define MEAN(x, y) ((x)+(y))/2
int main (void)
{
printf ("%d\n", (1 + 3) / 2);
return 0;
}
VC选项: /P#define identifier token-sequence
不带参数的宏,identifier前后的空格都会被丢弃。
#define identifier( identifier-list ) token-sequence
带参数的宏,第一个identifier和'('之间不能有空格,不然就会被解释成第一种。