S
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
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