Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #392902

Re: integer divided by zero

From Keith Thompson <Keith.S.Thompson+u@gmail.com>
Newsgroups comp.lang.c
Subject Re: integer divided by zero
Date 2025-04-25 16:09 -0700
Organization None to speak of
Message-ID <87frhvq1o3.fsf@nosuchdomain.example.com> (permalink)
References <vughb7$g6cm$1@dont-email.me> <87selwoydy.fsf@nosuchdomain.example.com> <vugo19$m2n2$1@dont-email.me> <87o6wkouea.fsf@nosuchdomain.example.com> <20250425141912.683@kylheku.com>

Show all headers | View raw


Kaz Kylheku <643-408-1753@kylheku.com> writes:
> On 2025-04-25, Keith Thompson <Keith.S.Thompson+u@gmail.com> wrote:
>> 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.
>
> My interpretation (looking at n3301) is that 1/0 is a constant
> expression, which violates a constraint.
>
> ("Each constant expression shall evaluate to a constant that is in the
> range of representable values for its type.")
>
> The constraint makes it clear that there may be constant expressions
> which evaluate out of range. (They are constant expressions in form:
> constant operands, subject to the permitted operators.)
>
> The constraint's purpose isn't to give a classifying requirement in the
> sense that expressions not meeting the constraint are not taken to be
> constant. It is for diagnostic use: constant expressions not meeting the
> constraint are to be diagnosed.
>
> According to this interpretation the declarator a[1/0] would be deemed
> to be a regular array, not VLA, and so a constraint violation occurs
> regardless of VLA support.

Also looking at n3301, the syntax is:

    constant-expression:
        conditional-expression

That doesn't imply that all conditional-expressions are
constant-expressions.  If it did, then

    time_t t = time(NULL);

would be a constraint violation; `time(NULL)` is a
conditional-expression, but it violates the constraint on function
calls -- but a constant expression is not required in that context
(assuming it's at block scope).

A construct is a constant-expression only if it appears in a place where
the grammar requires a constant-expression.  An array-declarator is not
one of those contexts; the syntax requires an assignment-expression.

There's a constraint on array declarators: "If the expression is
a constant expression, it shall have a value greater than zero."
That wording is a little iffy, but the meaning is clear enough.
It might have been more pedantically correct to say "If the
expression would be a constant expression if it appeared in a
context that requires a constant expression, ...".

None of these:
    int foo[time() % 10 + 1];
    int bar[1 / 0];
    int baz[INT_MAX + 1];
violates any constraint, even though constant expressions may not
include function calls, division by zero, or signed overflow.

A vaguely analogous case:
    int n = 1 + 2 * 3;
`1 + 2` is not an additive expression.  It looks like one, but it
appears in a context that syntactically doesn't allow an additive
expression.

-- 
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
void Void(void) { Void(); } /* The recursive call of the void */

Back to comp.lang.c | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

integer divided by zero Thiago Adams <thiago.adams@gmail.com> - 2025-04-25 14:38 -0300
  Re: integer divided by zero David Brown <david.brown@hesbynett.no> - 2025-04-25 19:50 +0200
    Re: integer divided by zero Thiago Adams <thiago.adams@gmail.com> - 2025-04-25 16:37 -0300
      Re: integer divided by zero Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2025-04-25 13:32 -0700
        Re: integer divided by zero Thiago Adams <thiago.adams@gmail.com> - 2025-04-26 12:30 -0300
          Re: integer divided by zero gazelle@shell.xmission.com (Kenny McCormack) - 2025-04-26 16:27 +0000
            Re: integer divided by zero Kaz Kylheku <643-408-1753@kylheku.com> - 2025-04-26 17:01 +0000
  Re: integer divided by zero Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2025-04-25 12:05 -0700
    Re: integer divided by zero Thiago Adams <thiago.adams@gmail.com> - 2025-04-25 16:32 -0300
      Re: integer divided by zero Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2025-04-25 13:31 -0700
        Re: integer divided by zero Kaz Kylheku <643-408-1753@kylheku.com> - 2025-04-25 21:36 +0000
          Re: integer divided by zero Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2025-04-25 16:09 -0700
            Re: integer divided by zero Tim Rentsch <tr.17687@z991.linuxsc.com> - 2025-05-14 01:09 -0700
        Re: integer divided by zero Thiago Adams <thiago.adams@gmail.com> - 2025-04-28 11:05 -0300
    Re: integer divided by zero Rosario19 <Ros@invalid.invalid> - 2025-04-30 12:41 +0200
      Re: integer divided by zero David Brown <david.brown@hesbynett.no> - 2025-04-30 13:35 +0200
  Re: integer divided by zero John McCue <jmccue@magnetar.jmcunx.com> - 2025-04-25 19:41 +0000
  Re: integer divided by zero antispam@fricas.org (Waldek Hebisch) - 2025-04-26 09:57 +0000
  Re: integer divided by zero Bonita Montero <Bonita.Montero@gmail.com> - 2025-04-27 15:33 +0200
    Re: integer divided by zero Bonita Montero <Bonita.Montero@gmail.com> - 2025-04-27 15:46 +0200
      Did you get confused again?  You seem eaily bewildered. (Was: integer divided by zero) gazelle@shell.xmission.com (Kenny McCormack) - 2025-04-27 14:43 +0000
        Re: Did you get confused again? You seem eaily bewildered. (Was: integer divided by zero) Bonita Montero <Bonita.Montero@gmail.com> - 2025-04-27 16:54 +0200
          Re: Did you get confused again? You seem eaily bewildered. (Was: integer divided by zero) scott@slp53.sl.home (Scott Lurndal) - 2025-04-27 16:16 +0000
            Re: Did you get confused again? You seem eaily bewildered. (Was: integer divided by zero) Kaz Kylheku <643-408-1753@kylheku.com> - 2025-04-27 16:23 +0000
              Re: Did you get confused again? You seem eaily bewildered. (Was: integer divided by zero) Bonita Montero <Bonita.Montero@gmail.com> - 2025-04-27 20:28 +0200
              Re: Did you get confused again? You seem eaily bewildered. (Was: integer divided by zero) scott@slp53.sl.home (Scott Lurndal) - 2025-04-27 22:52 +0000
                Re: Did you get confused again? You seem eaily bewildered. antispam@fricas.org (Waldek Hebisch) - 2025-04-28 10:41 +0000
            Re: Did you get confused again? You seem eaily bewildered. antispam@fricas.org (Waldek Hebisch) - 2025-04-28 10:12 +0000
              Re: Did you get confused again? You seem eaily bewildered. Rosario19 <Ros@invalid.invalid> - 2025-04-30 09:16 +0200
              Re: Did you get confused again? You seem eaily bewildered. Richard Heathfield <rjh@cpax.org.uk> - 2025-04-30 08:36 +0100
          Re: Did you get confused again? You seem eaily bewildered. antispam@fricas.org (Waldek Hebisch) - 2025-04-28 10:39 +0000
            Re: Did you get confused again? You seem eaily bewildered. Bonita Montero <Bonita.Montero@gmail.com> - 2025-04-28 12:50 +0200
    Re: integer divided by zero Richard Heathfield <rjh@cpax.org.uk> - 2025-04-28 08:54 +0100
      Re: integer divided by zero Bonita Montero <Bonita.Montero@gmail.com> - 2025-04-28 12:49 +0200
        Re: integer divided by zero Richard Heathfield <rjh@cpax.org.uk> - 2025-04-28 12:01 +0100
          Re: integer divided by zero Bonita Montero <Bonita.Montero@gmail.com> - 2025-04-28 13:27 +0200
    Re: integer divided by zero Richard Damon <richard@damon-family.org> - 2025-04-28 07:15 -0400
      Re: integer divided by zero Bonita Montero <Bonita.Montero@gmail.com> - 2025-04-28 13:28 +0200
        Re: integer divided by zero scott@slp53.sl.home (Scott Lurndal) - 2025-04-28 14:28 +0000
          Re: integer divided by zero Bonita Montero <Bonita.Montero@gmail.com> - 2025-04-28 16:51 +0200
          Re: integer divided by zero Michael S <already5chosen@yahoo.com> - 2025-04-28 18:03 +0300
            Re: integer divided by zero scott@slp53.sl.home (Scott Lurndal) - 2025-04-28 16:35 +0000
              Re: integer divided by zero Bonita Montero <Bonita.Montero@gmail.com> - 2025-04-28 18:38 +0200
            Re: integer divided by zero Muttley@DastardlyHQ.org - 2025-04-28 17:02 +0000
      Re: integer divided by zero antispam@fricas.org (Waldek Hebisch) - 2025-04-28 14:44 +0000
        Re: integer divided by zero Bonita Montero <Bonita.Montero@gmail.com> - 2025-04-28 16:52 +0200
  Re: integer divided by zero Richard Heathfield <rjh@cpax.org.uk> - 2025-04-28 14:52 +0100
    Re: integer divided by zero Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2025-04-28 13:44 -0700
  Re: integer divided by zero Vir Campestris <vir.campestris@invalid.invalid> - 2025-05-03 21:47 +0100
    Re: integer divided by zero Michael S <already5chosen@yahoo.com> - 2025-05-03 23:58 +0300
  Re: integer divided by zero goldside000@outlook.com - 2025-05-31 18:56 +0000
    Re: integer divided by zero Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2025-05-31 14:07 -0700
      Re: integer divided by zero scott@slp53.sl.home (Scott Lurndal) - 2025-05-31 21:53 +0000

csiph-web