Honeslty have no idea on how is the output being generated ?

#include <iostream>

int main(int argc, char** argv)
{
	int i=1,j=-1,k=0,l=2,m;  
	m=i++||j++&&k++;  
	printf("%d %d %d %d %d",i,j,k,l,m); 
	return 0;
}

output : 2 -1 0 2 1 evwn though j++ is is present that value is not being incremented

Recommended Answers

All 2 Replies

The result of OR is true if the first operand is true. Therefore, if it is the case (and it is, since i++ yields 1), the second operand (that is, j++&&k++) is not calculated at all. Such behaviour is required by the Standard.

Though evaluation of expression varies from compiler to compiler, but generally it will be from left to right.
The evaluation of logical operation is done as per truth table, which suggests that for OR operation any one operand yielding to true results the expression to be true.

So, only first operand of the OR operation was enough for evaluating the result of the expression.

Note: The OR and AND operations are having the same priority.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.