int i=0,j=1;
cout<<(j++)&&(i++); //output=1;
i=0,j=1;
printf("%d",(j++)&&(i++)); //output=0
can anyone please tell why this difference in output ???
> cout<<(j++)&&(i++);
What does this output - with extra ( )
cout<< ((j++)&&(i++)) ;
If it's 0, look up the operator precedence table in your handy book.
int i=0,j=1;
cout<<(j++)&&(i++); //output=1;i=0,j=1;
printf("%d",(j++)&&(i++)); //output=0can anyone please tell why this difference in output ???
Like the previous poster said, << is taking precedence over &&, so its running like this:
(cout << (j++)) && (i++);
printf is doing what you want, I am guessing.
int i=0, j=1;
cout<<(j++)&&(i++); //output=1
Is taken as (cout << (j++) ) && i++
.
Here the function cout
returns the characters successfully printed which is of type int
basciallly a number which when AND with a number gives another number. In the end the above stmt will look to the compiler like someNumber;
which is successfullly parsed by the compiler without generating any error and the required output is generated as a side effect of the cout
stmt.
Hope this explanation helped u.
Bye.
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.