Integer division by zero is undefined and should result floating point exception and this is what happens why I write the following code
int j = 0 ;
int x = 1 / j;
In this case the program just crashes with FPE, but if I won't use variable and will go with literal values like this int x = 1 / 0;
the the program doesn't crash, but just assigns some random value to x
! I tried to detect if int x = 1 / 0;
really causes a crash by adding custom SIGFPE
handler, but as I thought it is never invoked during literal zero division, but it does when I store 0
in a variable first. So, it seems that the literal zero division never actually crashes the program( I mean the division itself never happens), so does the compiler(GCC in my case) performs any tricks(e.g. substituting 0
with random number) in this case? or I misunderstood something ?