Path: csiph.com!news.mixmin.net!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail From: Tim Rentsch Newsgroups: comp.lang.c Subject: Re: for() syntax (Was: Christmas Quiz 2022) Date: Mon, 26 Dec 2022 06:48:57 -0800 Organization: A noiseless patient Spider Lines: 84 Message-ID: <86h6xiw78m.fsf@linuxsc.com> References: <20221220054654.91@kylheku.com> <87tu1quou7.fsf@bsb.me.uk> <87ili6unpn.fsf@bsb.me.uk> <87pmceq6k2.fsf@nosuchdomain.example.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: reader01.eternal-september.org; posting-host="b017513bf0c3c2565b748ac271e0e545"; logging-data="3468651"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19AF2aVv56dzEuWCsLKwoanR1gdkq4GHqA=" User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux) Cancel-Lock: sha1:7lAo72wm3+CfTOm4WQptIcnkqVg= sha1:dJOTr4R68lCFkyapq5HfpjpBdJQ= Xref: csiph.com comp.lang.c:168640 Lew Pitcher 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.