Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mixed increment operators with logical operators [duplicate]

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

like image 246
user2268349 Avatar asked Mar 02 '26 02:03

user2268349


1 Answers

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.

like image 84
chris Avatar answered Mar 04 '26 15:03

chris



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!