5 votes 5 votes #include <stdio.h> int main() { int y = 2; int z = y +(y = 10); printf("%d\n", z); } Programming in C programming-in-c + – Mandeep Singh asked Oct 22, 2016 • edited Oct 22, 2016 by Prashant. Mandeep Singh 18.3k views answer comment Share Follow See all 2 Comments See all 2 2 Comments reply Prashant. commented Oct 22, 2016 i reshown by Arjun Oct 23, 2016 reply Follow Share compiler dependent . sequence point problem. 4 votes 4 votes Mandeep Singh commented Oct 22, 2016 reply Follow Share Can higher precedence of parenthesis force first evaluation or it should always be random? 0 votes 0 votes Please log in or register to add a comment.
Best answer 7 votes 7 votes int z = y +(y = 10); Here, we are modifying y and again reading y without an intermediate sequence point (arithmetic operators are not sequence point in C, but logical || and && are). So, undefined behaviour. Operator precedence does not specify execution order as this restricts instruction level parallelism. https://gateoverflow.in/62411/undefined-behaviour-in-c Arjun answered Oct 23, 2016 • selected Oct 23, 2016 by Kapil Arjun comment Share Follow See all 4 Comments See all 4 4 Comments reply santhoshdevulapally commented Oct 25, 2016 reply Follow Share No sir,it is executed correctly,and it gives the output as 20. it first evaluate the expression in braces then it is added to the variable 'y'; 2 votes 2 votes Arjun commented Nov 26, 2016 reply Follow Share Yes, real tragedy that people are good enough only to make such questions :( 0 votes 0 votes Arjun commented Nov 26, 2016 reply Follow Share If you cannot expand your mind to understand what "undefined behaviour" is, whose problem is it? What explanation do you want to know why the output is 20? You can simply compile the code with "-S" option and see the assembly code generated. But what is the point in doing it- as the C standard requires no compiler to follow this rule - meaning another C compiler or even the same compiler for next version can change it and produce a different output. I do not know why any one cannot understand that programming is meant for producing stuffs which are useful to the world and not to make confusing questions to confuse students in exams. 2 votes 2 votes Arjun commented Nov 26, 2016 reply Follow Share @sittian Do you know Orkut? During my college days I used to explain these stuffs a lot in orkut groups without knowing about undefined behaviour. Just because I got to IISc. and be with better people I realized my mistake and want every others not to waste time on such stupidities like I had done. If you want explanation for this, I can give better explanation than anyone -- but as I had told that is stupidity and waste of time for any good exam like GATE. In more than 25 years of GATE not even one such question was asked in GATE -- IIT profs are not idiots. 2 votes 2 votes Please log in or register to add a comment.
0 votes 0 votes () having highest precedence first evaluate (y=10), and modified the value of y =2 to y=10 hence z=y+(y=10) gives value of z as z=10+10 which is equal to 20 turendar answered Aug 17, 2018 turendar comment Share Follow See all 0 reply Please log in or register to add a comment.