1 votes 1 votes #include<stdio.h> int main() { int i= 0; int j= ++i + i++; printf("%d\n",j) } I think output is $2$ but output is $3$. How is it possible? pls explain....... Programming in C programming-in-c + – Raj Kumar 7 asked Mar 14, 2018 • edited Mar 14, 2018 by Sukanya Das Raj Kumar 7 413 views answer comment Share Follow See all 0 reply Please log in or register to add a comment.
0 votes 0 votes It is "undefined behaviour of C". Different compilers will give different outputs. Please refer: 1. https://www.geeksforgeeks.org/undefined-behavior-c-cpp/ 2. https://www.geeksforgeeks.org/sequence-points-in-c-set-1/ Akhilesh Singla answered Mar 14, 2018 Akhilesh Singla comment Share Follow See all 2 Comments See all 2 2 Comments reply Raj Kumar 7 commented Mar 15, 2018 reply Follow Share I don't know about other compiler but gcc compiler give output 3. 0 votes 0 votes Akhilesh Singla commented Mar 15, 2018 reply Follow Share To get out of sequence problem(or undefined behaviour) modern compilers try to use either stack or registers to evaluate a same variable. Depending on which compiler is using which approach (stack or registers), you will get a different answer. But none of them is correct because C's developers did not define behaviour for such operations on a single variable and thus called undefined behaviour. Go through the links I gave, you will understand it. 0 votes 0 votes Please log in or register to add a comment.
0 votes 0 votes output are depend on the compiler but i you run this code in standard turbo c compiler then i will give output as 2 Pushkar Kariyare answered Mar 15, 2018 Pushkar Kariyare comment Share Follow See all 0 reply Please log in or register to add a comment.