Consider this program:
#include <stdio.h>
int main(void)
{
int x;
while ( 1 == scanf("%d", &x) )
printf("%c\n", "hello"[x]);
}
The compiler must compile this successfully because the program has no UB so long as the user does not enter any numbers outside the range 0
-4
.
However, according to this thread UB can travel back in time. Now consider this program:
int main(void)
{
printf("hello\n");
"hello"[6];
}
Any invocation of this program results in undefined behaviour, and since that can time-travel, the entire behaviour of this program on any invocation is undefined. Can the compiler therefore reject the program and not generate an executable? (We might say that the UB travels back in time to the compilation stage!)