Path: ...!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 13:31:25 -0700 Organization: None to speak of Lines: 44 Message-ID: <87o6wkouea.fsf@nosuchdomain.example.com> References: <87selwoydy.fsf@nosuchdomain.example.com> MIME-Version: 1.0 Content-Type: text/plain Injection-Date: Fri, 25 Apr 2025 22:31:27 +0200 (CEST) Injection-Info: dont-email.me; posting-host="9447933531b49a0efe9a81ccad1675e3"; logging-data="812425"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18FO99zHAGbgu+EzzkjNAJi" User-Agent: Gnus/5.13 (Gnus v5.13) Cancel-Lock: sha1:9fuSlcHQ4/cyMjKVkdKZeBkCPoA= sha1:apsvU2nNR4hU0GWhU4xMdOGE/Zg= Bytes: 2685 Thiago Adams writes: > Em 4/25/2025 4:05 PM, Keith Thompson escreveu: >> 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. > > Consider this sample > > int main(){ > int a[1/0]; > } > > 1/0 does not have a value in compile time, > So I believe compilers are making "a" a VLA because 1/0 is > not constant. 1/0 is not a constant expression. A conforming compiler that supports VLAs (C99, or optionally C11 or later) would make `a` a VLA, with undefined behavior at runtime when 1/0 is evaluated. For a conforming compiler that doesn't support VLAs (C89/C90, or optionally C11 or later) the declaration is a constraint violation. > (But what old c89 compilers where doing in this case?) > > This sample is a motivation to make integer divided by > zero a constrain. I consider it motivation not to write code like that. Sure, I wouldn't mind if using / or % with a right operand that's a constant expression with value zero (either integer or floating-point) were a constraint violation, but some compilers are going to warn about it anyway, and I doubt that such a language change would catch many errors. -- Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com void Void(void) { Void(); } /* The recursive call of the void */