Looking at the early (6th edition Unix, 1975) C manual from Dennis Ritchie's home page, in that version function-local variables could only be declared at the beginning of a function:
The function-statement is just a compound statement which may have declarations at the start.
function-statement: { declaration-listopt statement-list }
declaration-list is not defined (an omission), but can be readily assumed to have grammar:
declaration-list: declaration declaration-listopt.
No other compound statement is allowed to contain variable (or indeed any) declarations.
This obviously simplifies the implementation; in the early compiler source code c02.c
the function header function blkhed()
only needs to sum the stack space used by auto
variable declarations, at the same time recording their stack offset, and emit code to bump the stack pointer by the appropriate amount. On function exit (by return
or falling off the end) the implementation just needs to restore the saved stack pointer.
The fact that K&R feels necessary to state that "declarations of variables (including initializations) may follow the left brace that introduces any compound statement, not just the one that begins a function" is a hint that at that point it was a relatively recent feature. It also indicates that combined declaration-initialization syntax was also a recent feature, and indeed in the 1975 manual declarators cannot have initializers.
The 1975 manual in section 11.1 specifically states that:
C is not a block-structured language; this may fairly be considered a defect.
Block-statement and initialized declarations (K&R) address that defect, and mixed declarations and code (C99) are the logical continuation.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…