Look at the following code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(void)
{
int i = 2;
srand(time(NULL));
if (rand() % 2 == 0)
i = i++ + 2;
printf("%d", i);
}
Now, sometimes the output of this program is 2
while sometimes it is 4
— (Now, this value 4
depends on the compiler and the value be different on a different compiler, provided that the undefined statement i = i++ + 2;
is executed) — with the issuance of a warning in both the cases.
So, when I get the output 2
the statement i = i++ + 2;
is not executed by the compiler.
So does my program encounter an undefined behavior in that case too, even if the statement is not executed since the compiler(GCC, in my case) issue a warning with it?