加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

解释一个中期C的例子

发布时间:2020-12-16 10:24:18 所属栏目:百科 来源:网络整理
导读:我的老师刚刚将此作为即将到来的C中期项目的几个程序示例之一.代码是: //What does this program print?#include iostreamusing namespace std;#define foo1( a ) a * aint j = 6;inline intfoo2( int a ) { return a * a;}voidgoo1( int x ){ x = 3;}voidg
我的老师刚刚将此作为即将到来的C中期项目的几个程序示例之一.代码是:

//What does this program print?

#include <iostream>
using namespace std;

#define foo1( a )  a * a

int j = 6;

inline  int
foo2( int a ) 
{
    return a * a;
}

void
goo1( int& x )
{
    x = 3;
}

void
goo2( int  x )
{
    x = 3;
}

int main()
{
    int i,j = 12;

    cout << "foo1 = " << foo1( 3 + 2 ) << "n";
    cout << "foo2 = " << foo2( 3 + 2 ) << "n";

    i = 5;
    goo1( i );
    cout << "goo1 = " << i << "n";
    i = 5;
    goo2( i );
    cout << "goo2 = " << i << "n";

    cout << "  j = " <<   j << "n";
    cout << "::j = " << ::j << "n";
}

有人可以解释一下foo1 = 11.我一直在尝试不同的数字一小时,我无法确切地知道如何计算.

解决方法

foo1是一个宏.宏执行严格的查找和替换,因此foo1(3 2)被替换为3 2 * 3 2. 按照操作顺序,3 2 * 3 2 = 3 6 2 = 11.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读