Sujet : Re: integer divided by zero
De : Keith.S.Thompson+u (at) *nospam* gmail.com (Keith Thompson)
Groupes : comp.lang.cDate : 25. Apr 2025, 20:05:13
Autres entêtes
Organisation : None to speak of
Message-ID : <87selwoydy.fsf@nosuchdomain.example.com>
References : 1
User-Agent : Gnus/5.13 (Gnus v5.13)
Thiago Adams <
thiago.adams@gmail.com> 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.
-- Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.comvoid Void(void) { Void(); } /* The recursive call of the void */