Justification for pre-C99 C without initial declarations for loops?

Why didn't the C source language support initial declarations for loop initialization?

Obviously, the original creators, and then the standards before the C99, did not indicate it that way. But I can not find any justification for why this design was made.

The closest thing to the answer, which seems to me to be finding this answer , explaining how mixing declarations and code was forbidden so that compilers could be single-pass when it was important. At first glance, it makes sense that declarations in a for for statement will have the same / similar problems as declarations mixed with code.

But pre-C99 C supported declarations at the beginning of blocks:

{ unsigned int i; for(i = 0; i < WHATEVER; i += 1) { /* ... */ } } 

I personally do not see how the compiler logic for this differs significantly from this:

 for(unsigned int i = 0; i < WHATEVER; i += 1) { /* ... */ } 

It seems to me that if the compiler can make the first one-pass, it can also do the last. This may require that the for statement always create a block of visibility (even if it was accompanied by only one statement and not a block of statements { ... } ), but I can’t think of a way for such semantics to break any other -C99 C code (for the for statement follows a block, in which case it is already "restricted" or one statement follows, and in this case a new declaration would not be allowed in this single statement).

So why was this feature syntax initially omitted? Am I mistaken in thinking that it would be trivial to support without violating the performance goals of the time? The famous language parser / compiler methods at that time made this more difficult? Was it simply omitted due to the minimalist design / mentality, since functionally it was possible to do the same (block the loop for the loop)? Or did he have a clear reason for language design (for example, how were Go exceptions initially ruled out because designers thought it was done for a better language)?

Where i looked

  • I tried to find the answer to this question here and through general web search-fu, with no luck: all the search terms that I thought seemed saturated with confusing questions about the initial C declarations for the loop are "used outside of the error message" C99 "etc. (except for the search term" justification ", which directed me to useful information, but nothing that specifically answered this).
  • I searched for this article by Dennis Ritchie on language development and didn’t notice anything.
  • I looked at my copy of the C programming language (2nd edition), first read the section to explain the loop, and then checked the index for other mentions of "for" / "for loop". I read a few other places that I thought I could mention about, but found nothing.
+5
source share
1 answer

I do not believe that there was any specific decision to exclude such functions, nor was a rationale established.

As romantic as it might seem that designers (Kernighan, Ritchie, etc.) thought about all the possibilities and excluded the possibilities only after a deep and meaningful consideration, the reality is that the first years of C design (like quite a few other programming languages) followed for a much more modest philosophy: "Start small, don’t sweat about adding features, unless programmers WANT to do something."

Functions, such as variable initialization in for loops, are convenient for programmers - their absence does not stop execution. Thus, even if someone begged or campaigned for such a function (which probably was not), it probably dropped in priority order.

As for how things evolved .....

Until 1999, variable declarations were at the beginning of blocks in C (the code started with { and ended at closing } ), and not inside other statements. This is how things originally worked in the preliminary standard (K & R) C and previous languages ​​such as B (which was actually an abbreviated derivative of previous languages).

Variable declaration / initialization in a for loop was introduced first in C ++. It appeared quite early (for example, in section 19 in ARM), and was eventually introduced into the first C ++ standard, which was ratified at the end of 1998.

There was some discussion in the C standard committee during the development of the C ++ standard (which took a decade) about adopting some C ++ functions in C. This discussion was often mainly in line with the “anything else in C break if we added this is?". A number of compiler providers have already implemented several of these functions in their C compilers as additional extensions (or their C compilers were actually C ++ compilers, with settings to disable C ++ functions incompatible with C), so the discussion of adding these functions was quite concise. Therefore, these functions, easily added to C from C ++, appeared in the 1999 C standard. Variable declaration / initialization in the for loop was one of these functions.

There is no evidence from this story of any particular decision or justification for the exclusion of such symptoms from early C - in short, they probably simply did not think about it.

+10
source

Source: https://habr.com/ru/post/1241913/


All Articles