Mixed increment operators with logical operators [duplicate]

穿精又带淫゛_ 提交于 2019-12-29 01:25:49

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/15938941/mixed-increment-operators-with-logical-operators

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!