0

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

4
Contributors
3
Replies
4
Views
11 Years
Discussion Span
Last Post by ~s.o.s~
0

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

0

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

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.

0
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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.