Path: csiph.com!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.c Subject: Re: integer divided by zero Date: Fri, 25 Apr 2025 12:05:13 -0700 Organization: None to speak of Lines: 32 Message-ID: <87selwoydy.fsf@nosuchdomain.example.com> References: MIME-Version: 1.0 Content-Type: text/plain Injection-Date: Fri, 25 Apr 2025 21:05:14 +0200 (CEST) Injection-Info: dont-email.me; posting-host="9447933531b49a0efe9a81ccad1675e3"; logging-data="633588"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+sifVHRelBnmkuYfCwg7Hh" User-Agent: Gnus/5.13 (Gnus v5.13) Cancel-Lock: sha1:N6tWtyHo3l4ZFrWn9L9lAICm5iM= sha1:QILn0xb19hUbz5XLVtjJ/U6Wuwc= Xref: csiph.com comp.lang.c:392894 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 #include 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.com void Void(void) { Void(); } /* The recursive call of the void */