2 2 votes #include <stdio.h> int main() { int a = 5, b; a = ++a + a++ + ++a; printf("%d",a); return 0; } What will be the output of the code? Programming in C programming-in-c bad-question + – malaviya_parth 1.8k views answer comment Share Follow Print See 1 comment 1 1 comment reply Viggy_27 commented Jan 17, 2024 reply Follow flag . 0 0 replyShare Please log in or register to add a comment.
0 0 votes Answer is 20 Explaination : so a= 6+6+8 = 20 SurajS27 answered Jan 16, 2024 SurajS27 comment Share Follow See all 5 Comments 5 5 Comments reply Show 2 previous comments Arjun commented Jan 17, 2024 reply Follow flag obviously. Infact 3 * undefined behaviour. 1 1 replyShare SurajS27 commented Jan 18, 2024 reply Follow flag are you sure about that error because i am getting the answer when i am running that code 0 0 replyShare Arjun commented Jan 18, 2024 reply Follow flag Why should “undefined behaviour” produce “error”? Still if you run in gcc with “-Wall -O3” it’ll probably warn you when it can identify undefined behaviour. 0 0 replyShare Please log in or register to add a comment.
0 0 votes The answer will be a =21 While all the calls are on the same line then increment will be done on every call.so in first call a=6, in second a=7 and in third call a=8. pankaj kumar 70m answered Jan 16, 2024 pankaj kumar 70m comment Share Follow 0 reply Please log in or register to add a comment.
0 0 votes 20 prasantkr.singh answered Jan 17, 2024 prasantkr.singh comment Share Follow See 1 comment 1 1 comment reply malaviya_parth commented Jan 17, 2024 reply Follow flag I too solved and got 20 but, Run it in the online compiler, it will print 21, that’s why I am confused and was thinking whether I am missing something or not. 0 0 replyShare Please log in or register to add a comment.
0 0 votes Rule 1 : unary having higher precedence than binary : So lets solve all unary first then take binary Rule 2: The precedence of post-increment is significantly higher than that of pre-increment in C. Furthermore, when it comes to associativity, pre-increment has right-to-left associativity, whereas post-increment has left-to-right associativity. Ref: https://www.upgrad.com/tutorials/software-engineering/c-tutorial/pre-increment-and-post-increment-in-c/#:~:text=The%20precedence%20of%20post%2Dincrement,left%2Dto%2Dright%20associativity. a=5 a= ++a + (a++) + ++a; [post increment higher] a= ++a + 6 + (++a); [pre increment right to left associativity] a= (++a) + 6 + 7; a= 8 + 6 + 7; a= 21 So the ans is 21. Open for any corrections Viggy_27 answered Jan 17, 2024 Viggy_27 comment Share Follow See all 7 Comments 7 7 Comments reply malaviya_parth commented Jan 17, 2024 reply Follow flag As post increment then how can you take (a++) value as 6, it would be 5 no then. After giving the value as 5 it will be incremented right? 0 0 replyShare Viggy_27 commented Jan 17, 2024 reply Follow flag a++ itself will be solved first like at first as u told it takes 5 then post increments then it becomes 6 ( so only I have precided then in brackets) later takes pre increment (on right) and with that 6 becomes 7, again comes to first that 7 will be resolved to 8 after all the unary operations it takes up binary and sums up all in + associativity (left to right rule). even though its post or pre the precedence between them is won by post so the 5 gets converted to 6 and then further.... Though u might find ans being approached in a different way of getting 21 but this method is having proof of resolving precedence first and then associativity and then binary… U can apply to other sums and even check this method… to my knowledge this works. Let me know if there’s any correction :)) 0 0 replyShare Arjun commented Jan 17, 2024 reply Follow flag @Viggy_27 May be you can google “undefined behaviour in C”. 0 0 replyShare Viggy_27 commented Jan 18, 2024 reply Follow flag Okay Arjun sir, but whenever this undefined behavior of C topic comes in… the whole compiler design subject looks doubtful to me… as we write so many grammars for the order of syntax and maintain the precedence and associativity in analyzing operations by using syntax trees and semantic rules how come this c behaves as undefined, whether its because of the ambiguity in satisfying too many operations at once or is it not yet resolved as how we write grammars to some operations like these…. 0 0 replyShare Viggy_27 commented Jan 18, 2024 reply Follow flag . 0 0 replyShare Arjun commented Jan 18, 2024 reply Follow flag @Viggy_27 the above undefined behaviour is connected to the backend part of compilers. In other words, it has nothing do with the syntax analysis or the semantic analysis parts. When you try to do code optimisation or code generation - - in Master level courses, you can understand why it makes sense. 0 0 replyShare Viggy_27 commented Jan 18, 2024 reply Follow flag oh okay sir... Thanks for responding Sir and hope with your guidance and blessings I would study this topic in Masters through GATE !! 1 1 replyShare Please log in or register to add a comment.