Question

I have a question concerning pre and post increments with logical operators if I have this code

void main()
{int i = - 3 , j = 2 , k = 0 , m ;
m=++i||++j&&++k;
printf("%d %d %d %d",i,j,k,m);}

knowing that the increment and the decrement operators have higher precedence than && and || So they'll be executed first Then the && is higher than
means -2||3&&1 which gives the values -2 3 1 1 for the printf

but the output I get when trying on VS2010 is -2 2 0 1

Does anyone have any explanation for that ? Regards,,

Was it helpful?

Solution

This is what you get from short circuiting. ++i is -2, and the rest doesn't have to be evaluated (and isn't according to the standard). The left side of || is true because -2 is not 0, so the whole expression is true.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top