什么会被这一行,为什么进行打印? [英] What will be printed by this line and why?

查看:161
本文介绍了什么会被这一行,为什么进行打印?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么会被这一行打印?

 的printf(你好\\ N)| (输出(再见\\ n)||的printf(世界\\ n));

测试后,印:

 你好
再见

但我仍然不知道为什么,仍然坚持它,任何暗示将非常AP preciated!顺便说一下我知道的printf()返回输出的字符数,但仍然没有得到背后的逻辑。


解决方案

  | - 位OR运算符
|| - 逻辑或操作符的printf(你好\\ N)| (输出(再见\\ n)||的printf(世界\\ n));

在这种情况下,无论是的printf(你好\\ n)的printf(再见\\ n)它们之间|既然你已经使用>将被执行。因为它是按位操作,都将被执行。此外的printf 返回打印的字符数。因此,在你的情况,的printf(你好\\ N)|的printf(再见\\ n)将导致 6 | 8 = 0110 | 1000 = 1110 = 14(十进制)。

因此​​,前pression变得等同于 14 ||的printf(世界\\ n)。自14(即非零)本身符合逻辑或检查,就不会继续的printf(世界\\ n)这将是6。

What will be printed by this line?

printf("hello\n") | (printf("goodbye\n") || printf("world\n"));

After tested, it printed:

hello
goodbye

But I still don't know why and still stuck on it, any hints will be really appreciated! By the way I know that printf() returns the number of characters output but still do not get the logic behind that.

解决方案

|  - Bitwise OR operator
|| - Logical OR operator

printf("hello\n") | (printf("goodbye\n") || printf("world\n"));

In this case, both printf("hello\n") and printf("goodbye\n") will be executed since you have used | between them. Since it is bitwise operation, both will be executed. Also printf returns the number of characters printed. Hence in your case, printf("hello\n") | printf("goodbye\n") will result in 6 | 8 = 0110 | 1000 = 1110 = 14 (in decimal).

Hence the expression becomes equivalent to 14 || printf("world\n"). Since 14 (being non-zero) itself satisfies the Logical OR check, it will not proceed to printf("world\n") which would be 6.

这篇关于什么会被这一行,为什么进行打印?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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