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 ???
As and pointed out, the difference comes from operator precedence and the fact that the stream insertion is happening as a side effect before the logical && is evaluated. was right that the insertion produces a value the logical operator sees, but to be precise the insertion operator returns the stream (an ostream&) which converts to a boolean-like value representing the stream state; that conversion is what && tests.
A plain-language evaluation (with i starting at 0, j at 1): the cout form performs the insertion first, so the j value is printed as a side effect and the insertion expression yields a true-ish stream object; because the left side of && is true, the right side (i++) is evaluated (yielding 0) and the overall logical result is false — but that false is not printed. In the printf form the boolean expression is evaluated first and its numeric result (0 or 1) is what printf prints. Operator precedence details: see the C++ operator precedence table and logical operator semantics on cppreference: operator precedence and logical operators.
Safer alternatives: evaluate the boolean expression into a named bool/int first, or do the cout insertion in a separate statement so side effects and intent are explicit. Example pattern:
int a = 1, b = 0;
bool result = (a != 0) && (b != 0);
std::cout << static_cast<int>(result) << '\n'; Also note that streams are convertible to a boolean-like state (see std::basic_ios::operator bool), so mixing insertion, conversions and logical operators in one expression is fragile and easy to misread — prefer clearer, separated steps.
Jump to Post— Salem 6,009> 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.
> 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.