为什么 printf 不在这里打印? [英] why printf is not printing here?

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

问题描述

我知道 printf 执行后返回一些非零值 {returns no.字符} 现在在这个例子中我使用了多个 printf's 和现在.

I know that printf after execution returns some non zero value {returns no. of charecters} Now in this example i used multiple printf's and now.,

 /* As far as i was cocerned Precedence of && is more than ||,
  *and these logical operators check from left to right  
  *So compiler should come to hello and print "hello" then "nice to see you" then "hie" 
  *as all are true it should print "hola" 
  *but i wonder, why here the output is only "hie" and "hola"?
  */  

 #include<stdio.h>
 main()
 {
    if(printf("hie")|| printf("hello")&& printf("nice to see you"))
    printf("\thola\n");
 }

推荐答案

为什么这里的输出只有hie"和hola"?

逻辑与(&&)的优先顺序大于逻辑或(||).同意.但是,这并不意味着程序必须按该顺序进行评估.它只是说将表达式组合在一起.因此,

Why here the output is only "hie" and "hola"?

Order of precedence of Logical AND (&&) is greater than Logical OR (||). Agreed. But, it doesn't mean that a program has to evaluate in that order. It just says to group together the expressions. Hence,

if(printf("hie")|| printf("hello")&& printf("nice to see you"))

相当于,

 if(printf("hie")  ||  (printf("hello")&& printf("nice to see you")) )

'||' 运算符的短路评估发生:

Short circuit evaluation of '||' operator happens:

[C11: §6.5.14/4] 不同于按位 |运算符 ||运算符保证从左到右的评估;如果计算第二个操作数,则在第一个和第二个操作数的计算之间存在一个序列点.如果第一个操作数比较不等于 0,则不计算第二个操作数.

[C11: §6.5.14/4] Unlike the bitwise | operator, the || operator guarantees left-to-right evaluation; if the second operand is evaluated, there is a sequence point between the evaluations of the first and second operands. If the first operand compares unequal to 0, the second operand is not evaluated.

所以,hie 首先打印并返回一个 非零值,满足 || 条件,然后是 if 条件返回 true.

So, hie gets printed first and returns a non-zero-value, satisifies the || condition, followed by if condition returning true.

其中一个答案指出

求值顺序不依赖于优先级、关联性或(必然)依赖于明显的依赖关系.

Order of evaluation does not depend on precedence, associativity, or (necessarily) on apparent dependencies.

虽然这更接近,但这并不完全正确.尽管优先级与求值顺序不是一回事.在某些情况下,优先级间接影响评估顺序.

Though this is closer but this is not completely true. Though precedence is not the same thing as order of evaluation. There are cases wherein the precedence indirectly influences the order of evaluation.

考虑,

1 + 2 * 3

很明显,*的优先级高于+.当两个运算符共享一个操作数时,优先级潜入图片中,并且操作数与具有最高优先级的运算符分组.在上面的语句*+共享相同的操作数2,优先级告诉乘法运算符应用于23.+ 应用于 1 和乘法的结果.因此,编译器将上述语句解析为,

It is obvious that, order of precedence of * is higher than +. When two operators share an operand, precedence dives into picture, and the operand is grouped with the operator with the highest precedence. In the above statement * and + share the same operand 2, precedence tells that multiplication operator is applied to 2 and 3. + is applied to 1 and the result of multiplication. So, compiler parses the above statement as,

1 + (2 * 3)

1 + (2 *3)

上述表达式求值顺序的限制是,没有乘法结果就不能完成加法.因此,在这种情况下,乘法(较高优先级)在加法(较低优先级)之前进行评估

The constraint on the order of evaluation of above expression is that, addition can't be completed without the result of multiplication. So, in this case, multiplication(higher precedence) is evaluated before addition(lower precedence)

在您的 if() 语句中,优先级告诉编译器以这样一种方式解析语句,即它具有包含第二个和第三个 printf() 的隐式括号.如前所述,这并不意味着必须首先评估这些.

In your if() statement, precedence tells to compiler to parse the statement in such a way that it has an implicit paranthesis enclosing 2nd and 3rd printf(). That doesn't mean those has to be evaluated first as explained earlier.

所以,我们看到了两种情况.一方面,优先级不控制/影响评估顺序,另一方面,优先级具有间接影响.

So, we have seen two cases. In one, precedence doesn't control/influence order of evaluation and in other precedence had an indirect influence.

__

虽然优先级可能影响求值顺序,但它不会决定求值顺序"

"While precedence may influence the order of evaluation, it doesn't determine the order of evaluation"

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

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