关于短路行为的问题 [英] Question regarding the short circuit behavior
问题描述
大家好,
我有一个关于下面提到的代码的问题
#include< stdio.h>
int main(void)
{
int x = 0;
int y = 0;
if( x ++&& ++ y)
{
++ x;
}
printf(" ;%d%d \ nn,x,y);
返回0;
}
输出程序
1 0
但是我的理解是程序的输出应该是2 1.我想b / b
想解释我的理解。
最初x是0。在if(x ++&& ++ y)中x将为1&&介绍
序列点,这就是为什么x的值为1。然后根据规则C
编译器不会停止评估if(x ++&& ++ y)。因为它确实
不知道&的结果所以y的值将是1
..现在if(x ++&& ++ y)变为if(1&& 1)如果(1)。所以++ x将会执行
。因此x和y的最终值是2,1。但事实并非如此。
我在哪里出错了?
问候,
Somenath
Hi All,
I have one question regarding the bellow mentioned code
#include<stdio.h>
int main(void)
{
int x = 0;
int y = 0;
if ( x++ && ++y)
{
++x;
}
printf("%d %d\n",x, y);
return 0;
}
Output of the program
1 0
But my understanding is the output of the program should be 2 1. I
would like to explain my understanding.
Initially x is 0 . in "if ( x++ && ++y)" x will be 1 as && introduce
sequence point that''s why x will have value 1 . Then as per rule C
compiler will not stop evaluating the "if ( x++ && ++y)" as it does
not know the result of & so value of y will be 1
..Now "if ( x++ && ++y)" becomes "if ( 1 &&1 )" is if(1) . So ++x will
be executed .So the final value of x and y is 2,1 .But it is not so .
Where am I going wrong ?
Regards,
Somenath
推荐答案
12月13日,9:56 pm,somenath< somenath ... @ gmail.comwrote:
On Dec 13, 9:56 pm, somenath <somenath...@gmail.comwrote:
大家好,
我有一个关于下面提到的代码的问题
#include< stdio.h>
int main(无效)
{
int x = 0;
int y = 0;
if(x ++&& ++ y)
{
++ x;
}
printf("%d%d \ n",x,y);
返回0;
}
节目输出
1 0
但我的理解是程序的输出应该是2 1.我想b / b
想解释一下我的理解。
最初x为0。在if(x ++&& ++ y)中x将为1&&介绍
序列点,这就是为什么x的值为1。然后根据规则C
编译器不会停止评估if(x ++&& ++ y)。因为它确实
不知道&的结果所以y的值将是1
。现在如果(x ++&& ++ y)"变为if(1&& 1)如果(1)。所以++ x将会执行
。因此x和y的最终值是2,1。但事实并非如此。
我在哪里出错了?
Hi All,
I have one question regarding the bellow mentioned code
#include<stdio.h>
int main(void)
{
int x = 0;
int y = 0;
if ( x++ && ++y)
{
++x;
}
printf("%d %d\n",x, y);
return 0;
}
Output of the program
1 0
But my understanding is the output of the program should be 2 1. I
would like to explain my understanding.
Initially x is 0 . in "if ( x++ && ++y)" x will be 1 as && introduce
sequence point that''s why x will have value 1 . Then as per rule C
compiler will not stop evaluating the "if ( x++ && ++y)" as it does
not know the result of & so value of y will be 1
.Now "if ( x++ && ++y)" becomes "if ( 1 &&1 )" is if(1) . So ++x will
be executed .So the final value of x and y is 2,1 .But it is not so .
Where am I going wrong ?
x被评估为零,然后递增。这就是x +
+与++ x相反的含义。
是否清楚?
x is evaluated as zero and then incremented. That is the meaning of x+
+ as opposed to ++x.
Is it clear?
此致,
Somenath
Regards,
Somenath
12月14日上午11点05分,user923005< dcor ... @ connx.comwrote :
On Dec 14, 11:05 am, user923005 <dcor...@connx.comwrote:
12月13日晚上9:56,somenath< somenath ... @ gmail.comwrote:
On Dec 13, 9:56 pm, somenath <somenath...@gmail.comwrote:
大家好,
我有一个关于下面提到的代码的问题
Hi All,
I have one question regarding the bellow mentioned code
#include< stdio.h>
#include<stdio.h>
int main(void)
{
int x = 0;
int y = 0;
if(x ++&& ++ y)
{
++ x;
}
printf("%d%d \ n",x,y);
返回0;
int main(void)
{
int x = 0;
int y = 0;
if ( x++ && ++y)
{
++x;
}
printf("%d %d\n",x, y);
return 0;
}
}
程序输出
1 0
Output of the program
1 0
但我的理解是程序的输出应该是2 1.我想要b $ b想要解释我的理解。
But my understanding is the output of the program should be 2 1. I
would like to explain my understanding.
最初x为0。在if(x ++&& ++ y)中x将为1&&介绍
序列点,这就是为什么x的值为1。然后根据规则C
编译器不会停止评估if(x ++&& ++ y)。因为它确实
不知道&的结果所以y的值将是1
。现在如果(x ++&& ++ y)"变为if(1&& 1)如果(1)。所以++ x将执行
。因此x和y的最终值是2,1。但事实并非如此。
Initially x is 0 . in "if ( x++ && ++y)" x will be 1 as && introduce
sequence point that''s why x will have value 1 . Then as per rule C
compiler will not stop evaluating the "if ( x++ && ++y)" as it does
not know the result of & so value of y will be 1
.Now "if ( x++ && ++y)" becomes "if ( 1 &&1 )" is if(1) . So ++x will
be executed .So the final value of x and y is 2,1 .But it is not so .
我哪里错了?
Where am I going wrong ?
x被评估为零,然后递增。这就是x +
+与++ x相反的含义。
清楚了吗?
x is evaluated as zero and then incremented. That is the meaning of x+
+ as opposed to ++x.
Is it clear?
我的问题是为什么x被评估为零?因为编译器将停止仅在序列点评估一个表达式的
。这不是真的吗?我认为我错过了if的概念。声明工作
My question is why x is evaluated as zero ? Because compiler will stop
evaluating one expression only in sequence point . Is this not true? I
think I missing the concept how "if" statement work
somenath写道:
somenath wrote:
12月14日上午11:05,user923005< dcor。 .. @ connx.comwrote:
On Dec 14, 11:05 am, user923005 <dcor...@connx.comwrote:
> 12月13日晚上9:56,somenath< somenath ... @ gmail.comwrote:
>On Dec 13, 9:56 pm, somenath <somenath...@gmail.comwrote:
>>大家好,
我有一个关于下面提到的代码的问题
#include< stdio.h>
int main(void)
{
int x = 0;
int y = 0;
if(x ++&& ++ y)
{
++ x;
}
printf("%d%d \ n",x ,y);
返回0;
}
程序输出
1 0
>>Hi All,
I have one question regarding the bellow mentioned code
#include<stdio.h>
int main(void)
{
int x = 0;
int y = 0;
if ( x++ && ++y)
{
++x;
}
printf("%d %d\n",x, y);
return 0;
}
Output of the program
1 0
>>我哪里错了?
>>Where am I going wrong ?
x被评估为零,然后递增。这就是x +
+与++ x相反的含义。
是否清楚?
x is evaluated as zero and then incremented. That is the meaning of x+
+ as opposed to ++x.
Is it clear?
我的问题是为什么x被评估为零?因为编译器将停止仅在序列点评估一个表达式的
。这不是真的吗?我认为我错过了if的概念。声明工作
My question is why x is evaluated as zero ? Because compiler will stop
evaluating one expression only in sequence point . Is this not true? I
think I missing the concept how "if" statement work
这与if语句无关。 x ++是一个后增量
运算符,返回x的值,然后x递增。如果你用
来写++ x,那么预增量形式,x会增加,然后再按
评估为1.
-
Ian Collins。
It''s nothing to do with the if statement. x++ is a post-increment
operator, the value of x is returned and x is then incremented. If you
were to write ++x, the pre-increment form, x would be incremented and
then evaluated as 1.
--
Ian Collins.
这篇关于关于短路行为的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!