Recently I had to modify a legacy code that was compiled with a very old version of GCC (somewhere around version 2.3). Within a function, variable had to be declared before being used. I believe this is done C89 standard. This limitation is later removed.
My question is: Back then, why did they enforce this ruling? Was there any concern that could jeopardise the integrity of the software?
Variables still have to be declared before being used — and they’ve never had to be declared just at the top of a function.
The C89 requirement is that a block consists of an opening
{, followed by zero or more declarations, followed by zero or more statements, followed by the closing}.For example, this is legal C89 (and, without the
void, even K&R C, going back to 1978 or earlier):C99 loosened this, allowing declarations and statements to be mixed within a block:
As for the reason for the original restriction, I think it goes back to C’s ancestor languages: B, BCPL, and even Algol. It probably did make the compiler’s job a bit easier. (I was thinking that it would make parsing easier, but I don’t think it does; it still has to be able to distinguish whether something is a declaration or a statement without knowing in advance from the context.)