C/C++的输出

来源:互联网 发布:mysql数据库备份还原 编辑:程序博客网 时间:2024/06/07 02:42
1、
#include<iostream>using namespace std;//-----------------------int hello(){    cout << "hello " <<endl; return 1;}int world(){    cout << "world " <<endl; return 3;}//=========================int main(){    cout << hello()<<world()<<endl;    return 0;} 

输出结果会是什么呢?自己预计会是hello 1 world 3,顶多是hello world 13

但是实际结果,是world hello 13,为什么呢?

自己调试了运行时栈调用顺序,发现会在main后进入world函数再是hello函数,那为什么会这样呢?

经同学指导,得知是,“<<”流运算符进行输出的时候,根据流动方向,是从右往左

所以我们main后,首先先将endl压栈(endl和cout一样,为std命名空间)

然后执行到world函数,输出world,然后将返回值3压栈

然后执行到hello函数,输出hello ,然后将返回值1压栈

然后到cout,开始输出,于是就出1,3,endl;

于是构成了最初预想的这个结果

另外的测试是

int i = 4;cout << ++i << i++ << endl;
将会输出6,4.

不得不说,被最初级的cout打败,蛮有挫折感,不过有收获就好

////////////////////////////////////////////////////////////////////////////////////////////

2、2010.10.29补充c的一个问题

#include<stdio.h>int main(){    int i = 43;    printf("%d /n",printf("%d",printf("%d",i)));    return 0;}

其实,这里会输出4321,主要的一个,就是printf的返回值,是输出字符的长度,即""内的长度,如果加个空格,则会多1
另外,printf的入栈顺序是从右至左,所以出现printf i++ ,++i什么的,也许会考
over
0 0
原创粉丝点击