宏观扩张的奇怪行为 [英] Strange behavior of Macro-expansion

查看:106
本文介绍了宏观扩张的奇怪行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里的code:

#include <stdio.h>
#include <stdio.h>
#define VAL1(a,b)    a*b
#define VAL2(a,b)    a/b
#define VAL3(a,b)    ++a%b
int main()
{
    int a = 1;
    int b = 2;
    int c = 3;
    int d = 3;
    int e = 5;

    int result = VAL2(a,d)/VAL1(e,b)+VAL3(c,d);  // result = 1
    //int result = a/d/e*b+++c%d;                // result = 0

    printf("%d\n", result);

    return 0;
}

为什么不是两个语句的结果一样吗?

Why aren't the results of two statements the same?

推荐答案

在一种情况下,你有 + + ,并在其他情况下,你有 + + + + + + 是不同的数据流的标记的。因为它是被粘贴标记宏粘贴不改变符号化。

In one case you have + ++ and in the other case you have ++ +. + ++ and ++ + are different streams of tokens. Macro pasting doesn't change tokenization because it's tokens that are pasted.

如果你一拳你的程序为C pre处理器,你会得到了这一点,该行:

If you punch your program into a C pre-processor, you'll get this out for that line:

int result = a/d/e*b+ ++c%d;

注意,preprocessor不得不插入空格,因为一个是 + 标记和 ++ 标记。

这篇关于宏观扩张的奇怪行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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