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


Groups > comp.lang.c > #168640

Re: for() syntax (Was: Christmas Quiz 2022)

From Tim Rentsch <tr.17687@z991.linuxsc.com>
Newsgroups comp.lang.c
Subject Re: for() syntax (Was: Christmas Quiz 2022)
Date 2022-12-26 06:48 -0800
Organization A noiseless patient Spider
Message-ID <86h6xiw78m.fsf@linuxsc.com> (permalink)
References (4 earlier) <87ili6unpn.fsf@bsb.me.uk> <tnsis8$mh7i$2@dont-email.me> <tnsl3s$mh7i$3@dont-email.me> <87pmceq6k2.fsf@nosuchdomain.example.com> <tnt0d6$mh7i$5@dont-email.me>

Show all headers | View raw


Lew Pitcher <lew.pitcher@digitalfreehold.ca> writes:

> [...concerning the syntax of for() statements...]
>
> Consider (from N1570)
>
>   6.7 Declarations
>     Syntax
>       declaration:
>         declaration-specifiers init-declarator-list[opt] ;
>         static_assert-declaration
>
>       declaration-specifiers:
>         storage-class-specifier declaration-specifiers[opt]
>         type-specifier declaration-specifiers[opt]
>         type-qualifier declaration-specifiers[opt]
>         function-specifier declaration-specifiers[opt]
>         alignment-specifier declaration-specifiers[opt]
>
>       init-declarator-list:
>         init-declarator
>         init-declarator-list , init-declarator
>
>       init-declarator:
>         declarator
>         declarator = initializer
>
> And
>   6.8.5 Iteration statements
>     Syntax
>       iteration-statement:
>         while ( expression ) statement
>         do statement while ( expression ) ;
>         for ( expression[opt] ; expression[opt] ; expression[opt] ) statement
>         for ( declaration expression[opt] ; expression[opt] ) statement
>
> (The square brackets here denotes a subscript)
>
> Does not the second form of the for() statement
>     for ( declaration expression[opt] ; expression[opt] ) statement
> permit the declaration part to /not/ have an initializer?  As in
>     for ( int i; i>0; ++i) continue;

Yes, it does, and clearly that is deliberate.

> While it looks "legal" to me, it certainly seems suspect.  I would
> have expected the standard to /require/ that the "declaration" part
> of the for() statement include an initializer.

Consider the following for() statement, which is allowed by the C
standard:

    for(  int i = 0, j, k;  i < N;  i++  )  ...

Would you insist that 'j' and 'k' also have initializers?

It seems obvious that the syntax for for() statements simply
reused the syntax for _declaration_ so as not to complicate the
language.  Whether declaring a non-initialized variable is useful
or not (and there are cases where it can be useful), that is not
sufficiently important to complicate the language by adding more
syntax (or a constraint).

Remember Flon's Law:  there has never been, nor will there ever
be, a programming language in which it is the least bit difficult
to write bad programs.

What I think is funnier is that the for() statement below is also
allowed (as of C11):

    for(
        _Static_assert( 1 > 0, "fooled you!" );
        c = getchar(),  c != EOF;
        /* nothing */
    ){
        ...
    }

Here again, the negative consequence of allowing a _Static_assert
in a for() statement is outweighed by the cost of expanding the
syntax.  Special cases are anathema to ease of remembering and
ease of understanding.  Natural languages with lots of irregular
verbs are harder to learn than those with few irregular verbs.
The same idea applies to programming languages.

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


Thread

Christmas Quiz 2022 Roman P <invalid@example.com> - 2022-12-18 20:00 +0000
  Re: Christmas Quiz 2022 Tony Oliver <guinness.tony@gmail.com> - 2022-12-18 13:18 -0800
    Re: Christmas Quiz 2022 Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2022-12-18 16:48 -0800
    Re: Christmas Quiz 2022 Tim Rentsch <tr.17687@z991.linuxsc.com> - 2022-12-18 17:18 -0800
      Re: Christmas Quiz 2022 "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2022-12-19 19:39 -0800
    Re: Christmas Quiz 2022 Niles Rogoff <eternal-september@niles.xyz> - 2022-12-27 01:22 -0800
      Re: Christmas Quiz 2022 Öö Tiib <ootiib@hot.ee> - 2022-12-27 01:58 -0800
        Re: Christmas Quiz 2022 Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2022-12-27 10:25 -0800
      Re: Christmas Quiz 2022 James Kuyper <jameskuyper@alumni.caltech.edu> - 2022-12-27 05:07 -0500
        Re: Christmas Quiz 2022 scott@slp53.sl.home (Scott Lurndal) - 2022-12-27 16:06 +0000
          Re: Christmas Quiz 2022 scott@slp53.sl.home (Scott Lurndal) - 2022-12-27 17:42 +0000
          Re: Christmas Quiz 2022 Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2022-12-27 13:53 -0800
    Re: Christmas Quiz 2022 Niles Rogoff <eternal-september@niles.xyz> - 2022-12-27 01:24 -0800
      Re: Christmas Quiz 2022 scott@slp53.sl.home (Scott Lurndal) - 2022-12-27 16:07 +0000
  Re: Christmas Quiz 2022 Ben Bacarisse <ben.usenet@bsb.me.uk> - 2022-12-18 21:22 +0000
    Re: Christmas Quiz 2022 Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2022-12-18 21:40 +0000
    Re: Christmas Quiz 2022 Tim Rentsch <tr.17687@z991.linuxsc.com> - 2022-12-18 17:02 -0800
    Re: Christmas Quiz 2022 Tim Rentsch <tr.17687@z991.linuxsc.com> - 2022-12-18 17:20 -0800
  Re: Christmas Quiz 2022 red floyd <no.spam.here@its.invalid> - 2022-12-18 13:36 -0800
  Re: Christmas Quiz 2022 "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2022-12-18 13:48 -0800
    Re: Christmas Quiz 2022 Siri Cruise <chine.bleu@yahoo.com> - 2022-12-18 14:57 -0800
      Re: Christmas Quiz 2022 "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2022-12-18 15:43 -0800
        Re: Christmas Quiz 2022 Lynn McGuire <lynnmcguire5@gmail.com> - 2022-12-19 23:40 -0600
      Re: Christmas Quiz 2022 "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2022-12-20 14:01 -0800
  Re: Christmas Quiz 2022 Kaz Kylheku <864-117-4973@kylheku.com> - 2022-12-20 13:49 +0000
    Re: Christmas Quiz 2022 Ben Bacarisse <ben.usenet@bsb.me.uk> - 2022-12-20 14:33 +0000
      Re: Christmas Quiz 2022 Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2022-12-20 14:44 +0000
        Re: Christmas Quiz 2022 Ben Bacarisse <ben.usenet@bsb.me.uk> - 2022-12-20 14:57 +0000
          Re: Christmas Quiz 2022 Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2022-12-20 15:03 +0000
            Re: Christmas Quiz 2022 David Brown <david.brown@hesbynett.no> - 2022-12-20 16:32 +0100
            Re: Christmas Quiz 2022 Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2022-12-20 15:41 +0000
              Re: Christmas Quiz 2022 Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2022-12-20 10:22 -0800
                Re: for() syntax (Was: Christmas Quiz 2022) Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2022-12-20 18:54 +0000
                Re: for() syntax Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2022-12-20 12:08 -0800
                Re: for() syntax (Was: Christmas Quiz 2022) Tim Rentsch <tr.17687@z991.linuxsc.com> - 2022-12-26 06:48 -0800
                Re: Christmas Quiz 2022 Tim Rentsch <tr.17687@z991.linuxsc.com> - 2022-12-26 06:25 -0800
              Re: Christmas Quiz 2022 Tim Rentsch <tr.17687@z991.linuxsc.com> - 2022-12-26 06:24 -0800
                Re: Christmas Quiz 2022 Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2022-12-26 13:05 -0800
  Re: Christmas Quiz 2022 Andrey Tarasevich <andreytarasevich@hotmail.com> - 2022-12-20 12:46 -0800
    Re: Christmas Quiz 2022 Paavo Helde <eesnimi@osa.pri.ee> - 2022-12-20 23:49 +0200
      Re: Christmas Quiz 2022 Andrey Tarasevich <andreytarasevich@hotmail.com> - 2022-12-20 14:00 -0800
        Re: Christmas Quiz 2022 Paavo Helde <eesnimi@osa.pri.ee> - 2022-12-21 00:13 +0200
          Re: Christmas Quiz 2022 Andrey Tarasevich <andreytarasevich@hotmail.com> - 2022-12-20 14:38 -0800
    Re: Christmas Quiz 2022 Andrey Tarasevich <andreytarasevich@hotmail.com> - 2022-12-20 21:31 -0800
      Re: Christmas Quiz 2022 David Brown <david.brown@hesbynett.no> - 2022-12-21 12:02 +0100

csiph-web