Liste des Groupes | Revenir à cl c |
Thiago Adams writes:Does anyone know of any platform where integer division by zero>
returns a number, or in other words, where it's not treated as an
error? I'm asking because division by zero is undefined behaviour, but
I think division by a constant zero should be a constraint instead.
Division by a constant zero is a constraint violation in a context that
requires a constant expression.
>
I wrote this quick and dirty program:
>
#include <stdio.h>
#include <time.h>
int main(void) {
int one = time(NULL) / 1000000000;
int zero = one - 1;
int ratio = one / zero;
printf("%d\n", ratio);
}
>
It's not portable, but on the systems where I've tried it it sets
one to 1 and zero to 0 in a way that the compiler can't detect,
so the division won't be optimized away. (At least, it does so if
you run it between 2001 and 2286.)
>
On x86_64, it dies with a floating point exception.
>
On aarch64, it prints 0.
Les messages affichés proviennent d'usenet.