Liste des Groupes | Revenir à cl c |
Thiago Adams <thiago.adams@gmail.com> writes:Another interesting exampleEm 4/25/2025 4:05 PM, Keith Thompson escreveu:1/0 is not a constant expression.Thiago Adams <thiago.adams@gmail.com> writes:>Does anyone know of any platform where integer division by zeroDivision by a constant zero is a constraint violation in a context
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.
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.
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?)I consider it motivation not to write code like that.
>
This sample is a motivation to make integer divided by
zero a constrain.
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.
Les messages affichés proviennent d'usenet.