c - If C99 lifted "variable declaration at top of block" constraint, why doing so in a "for loop" showing error? -
i read site c99 lifted restriction variables in c must declared @ top of block. tested in program below , indeed true no errors. in th e same program if declare variable in first statement of for
loop, error:
'for' loop initial declarations allowed in c99 mode|
two things here. since indeed allowed declare variables in middle of program,as did i
, why not allowed in for
loop statement? , second, if compiler (codeblocks/gcc) not in c99 mode already, why didn't error when declared variables in middle instead of top?
#include <stdio.h> int main (void) { //proof initialization in middle works (for i) printf("enter\n"); char string[20]; scanf("%s", string); int i=20; printf("%s,%i", string, i); //proved works for(int j=0; j<10; j++) //this not allowed printf("%d\n", j); }
by default gcc compiles code own "dialect" extension of c89. idea new code , learning c nowadays pass more modern , standardized version of language. unfortunately implementation of c11 isn't yet complete, you'd have stick c99 moment using -std=c99
.
the gcc online documentation has information on different dialects of c implement.
an alternative compiler clang c99 default. experience better suited beginners because diagnostics bit more user friendly gcc's.
Comments
Post a Comment