输出如何是4321 [英] How the output is 4321

查看:85
本文介绍了输出如何是4321的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

int i = 43; printf(%d,(printf(%d,(printf(%d,i)))));



什么我试过了:



我的输出是434343.任何人都可以帮我吗

解决方案

< blockquote>不,你的输出是4321:

 printf(%d,(printf(%d,(printf(%d,i))) )); 
^ ^
打印43并返回打印的字符数:2
^ ^
打印2并返回打印的字符数:1
^ ^
打印1并返回打印的字符数:1



这相当于说:

 i = printf(%d,i); 
i = printf(%d,i);
printf(%d,i);

因为必须首先执行最里面的函数调用。


看看这里< a href =http://www.cplusplus.com/reference/cstdio/printf/> printf - C ++参考 [ ^ ]

 int i = 43; 
printf(%d,(printf(%d,( printf(%d,i)))));

粗体printf首先完成并打印43,printf的返回值为2(如打印的2个字符)。

 int i = 43; 
printf(%d,( printf (%d,(printf(%d,i)>))));

这个粗体printf是下一个,操作数是2(第一次调用的返回值),因此打印数字2,此printf的返回值为1,因为只有一个字符是打印。

 int i = 43; 
printf (%d,(printf(%d,(printf(%d,i)>))));

这里的粗体printf最后以数字1作为操作数,因此打印1。



所以预期的输出将是4321。


int i =43; printf("%d",(printf("%d",(printf("%d",i)))));

What I have tried:

My output is 434343. Can any one help me please

解决方案

No, your output is 4321:

printf("%d",(printf("%d",(printf("%d",i)))));
                          ^             ^
                            Prints "43" and returns the number of characters printed: 2
             ^                            ^
                Prints "2" and returns the number of characters printed: 1
^                                           ^
   Prints "1" and returns the number of characters printed: 1


It's the equivalent of saying:

i = printf("%d",i);
i = printf("%d",i);
printf("%d",i);

As the "innermost" function call has to be executed first.


Take a look here printf - C++ Reference[^]

int i =43; 
printf("%d",(printf("%d",(printf("%d",i)))));

the bold printf is done first and prints "43", the return value of the printf is 2 (as in 2 characters printed).

int i =43; 
printf("%d",(printf("%d",(printf("%d",i)>)))); 

this bold printf is next and the operand is 2 (the return value from the first call), and so the number "2" gets printed, the return value from this printf is 1 as only one character was printed.

int i =43; 
printf("%d",(printf("%d",(printf("%d",i)>))));

the bold printf here is done last with the number 1 as the operand, so "1" gets printed.

So the expected output would be "4321".


这篇关于输出如何是4321的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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