O/P:Code://Compiled on gcc //I know that this is dependent on compiler but how we are getting o/p as 4 on gcc #include<stdio.h> int main() { int i=1,j=2,k; k=(i--,j--) + j-- ; printf("%d",i); }
4
O/P:Code://Compiled on gcc //I know that this is dependent on compiler but how we are getting o/p as 4 on gcc #include<stdio.h> int main() { int i=1,j=2,k; k=(i--,j--) + j-- ; printf("%d",i); }
4
Same reason you were given by Laser
Read me this time.
> //I know that this is dependent on compiler but how we are getting o/p as 4 on gcc
Do gcc -S prog.c, then look through prog.s to see where all the mov/sub/dec instructions are.
Or learn to stop worrying about how UB 'works' for each and every compiler you might come across
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
Since the code invokes undefined behaviour (modifying j twice between two sequence points) the compiler is allowed to do anything.
My guess is that the value being output is the 42nd byte of the output executable file. Why the 42nd byte? To honour the "Hitchhiker's Guide to the Galaxy". Why does it have the value 4? That is the first digit of 42. Since any explanation what what you see is possible when you invoke undefined behaviour, this is as feasible an explanation as any.
Better yet, don't bother seeking explanations for particular cases of undefined behaviour. Spend your time learning how to AVOID invoking undefined behaviour.
Um....Zooey Deschanel in those shorts.