i++和++i的研究

int x=1;

x=(x++)+(++x);

printf("%d",x);

what is the value of x?

=========================================

( ˇˍˇ ):compiler executes it by using a stack . follow the same approach. put the expression (x++)+(++x) in stack from left to right . You'll get ++x + x++ last in, first out (LIFO) applies. -For solving, first ++x comes out of stack . ++x means (increment then Use). So, x is incremented, x becomes 2.Put back the result 2 in stack. -again LIFO applies, 2 comes out followed by the operator +. Nothing can be done on 2+, So take out the next operand x++. Post-operator means (use the value and then increment). So, you use x=2 in addition (2+2=4) , but after using x you have to increment it ( 4++ =5) -now this result is stored in x and printed.

( ⊙o⊙?):but the execution of increment operator is from right to left but not left to right

( ˇˍˇ ):That is right. And the LIFO approach ensures this.Refer the book 'The C Programming Language' by Brian kernighan and Dennis Ritchie. This book states that "Function calls, nested assignment statements, and increment and decrement operators cause 'side-effects'- some variable is changed as a by-product of the evaluation of an expression. In any expression involving side-effects, there can be subtle dependencies on the order in which variables taking part in the expression are updated. One unhappy situation is typified by the statement a[i]=i++; The question is whether the subscript i is the old value of i or the new. Compilers can interpret this in different ways, and generate different answers depending on the machine architecture. So it is left to the discretion of Compiler. Moral is that writing code that depends on the order of evaluation is a bad programming practice. So before programming can be done, it is better to know about the machine on which this code is going to execute."

( ⊙o⊙?):ya i do agree with the output, but i still have a doubt the brackets specification is that all the operations must be completed first before proceeding to the next computation of the equation if we see in that way there are no other operation in the bracket other than the ++ which means incrementing which would bluntly increment by one in both x++ and ++x so the output should be four only ..... hope i'm pertinent ....

( ˇˍˇ ):I cross-checked the output. Executed the program on TurboC compiler. The result '5' is correct. And even on removing brackets, the result is still the same.

( ⊙o⊙?):ya i too did the output is correct i do agree with that..... then the meaning of the bracket would go wrong or the fact is that increment operator has a higher priority than the brackets...???

( ˇˍˇ ):The priority of brackets is highest, no doubt. There should be another explanation to your question.From what I can understand, brackets do not make any difference here because the three terms x++, + , ++x were going to be put in the stack in the same order.the relevance of bracket would be more prominent here if the bracket contained a binary operator instead of unary(increment)

( ⊙o⊙?):i understand... but its a bit vague for me??? in a clear way pls???

( ˇˍˇ ):Brackets are used in this expression just to avoid confusion of too many '+' signs. In order to understand the evaluation of this expression , you have to see this expression the way compiler sees it. It sees 3 term: x++, +, ++x Now , in order to evaluate, it starts pushing the terms in a stack from Left to right. And then LIFO for popping out

(╯▽╰):Your response to the question below needs to be a well-developed paragraph. Start with a strong topic sentence and follow with supporting sentences. Give examples that can help explain your thoughts on the topic. Be careful to proofread to check for mistakes in spelling and writing conventions. How did the British cause nationalism to develop in India?

知识共享许可协议
《i++和++i的研究》常伟华 创作。
采用 知识共享 署名-相同方式共享 3.0 中国大陆 许可协议进行许可。
相邻依据:发表时间
  • 多说评论
  • 签名
  • 新浪微博
  • 默认评论
  • Tab Header 5

0 条评论 / 点击此处发表评论

Tab Content 5

开发技术


开发平台和工具

sitemap     166.59ms