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