【问题标题】:Why macro function isn't call by value?为什么宏函数不按值调用?
【发布时间】:2014-04-02 06:25:51
【问题描述】:
#include<stdio.h>
#define exch(A,B) {int t=A;A=B;B=t;}
int main(void)
{
    int a=1,b=4;
    exch(a,b);
    printf("%d\t%d\n",a,b);
    return 0;
}

打印:a=4,b=1

在输出中,a和b已经交换了;为什么宏不按值调用?请帮我解释一下结果。

【问题讨论】:

    标签: c macros c-preprocessor call-by-value


    【解决方案1】:

    因为宏不是函数,预处理器只是替换文本。宏替换后,你的程序变成:

    //code in stdio.h
    
    int main(void)
    {
        int a=1,b=4;
        {int t=A;A=B;B=t;}
        printf("%d\t%d\n",a,b);
        return 0;
    }
    

    看到了吗?交换中没有函数调用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-03
      相关资源
      最近更新 更多