1

I read from a site that C99 lifted the restriction that variables in C must be declared at the top of a block. I tested in my program below and it is indeed true as I get no errors. But in th e same program if I declare a variable in the first statement of a for loop, I get the error:

'for' loop initial declarations are only allowed in C99 mode|

Two things here. Since it is indeed allowed to declare variables in the middle of the program,as I did for i, why then I am not allowed to do that in a for loop statement? And second, if my compiler (Codeblocks/gcc) is not in C99 mode already, why didn't I get an error when I declared variables in the middle instead of the top?

#include <stdio.h>

int main (void)
{
  //Proof that initialization in middle works (for i)
  printf("Enter\n");
  char string[20];
  scanf("%s", string);
  int i=20;
  printf("%s,%i", string, i);
  //Proved that it works

  for(int j=0; j<10; j++) //THIS IS NOT ALLOWED
    printf("%d\n", j);
}
10
  • 11
    Because the default mode for your compiler is not in C99 mode. Try adding -std=c99 flag to GCC. Commented May 21, 2013 at 7:22
  • 1
    @JoachimPileborg If so,why then I don't get an error while declaring variables in middle as I successfully did for i? Commented May 21, 2013 at 7:24
  • 1
    @JoachimPileborg A pre-C99 setting would show that as an error,shouldn't it? Commented May 21, 2013 at 7:24
  • 2
    If you give gcc the -pedantic flag it will warn you about mixed declarations and code (and the comments). And if you compile with -Wall it'll be angry with you for not explicitly returning a value. Commented May 21, 2013 at 7:28
  • 1
    That is okay. For example for (int j = 0; j < 10; j++) { int some_var = 5; use_some_var(some_var); }. Commented May 21, 2013 at 9:33

2 Answers 2

6

By default gcc compiles code with its own "dialect" which is an extension of C89. It is probably a good idea for new code and especially anybody learning C nowadays to pass to more modern and standardized version of the language. Unfortunately their implementation of C11 isn't yet complete, so you'd have to stick with C99 for the moment using -std=c99.

The gcc online documentation has information on the different dialects of C they implement.

An alternative compiler could be clang which is C99 by default. To my experience it is often better suited for beginners because the diagnostics are a bit more user friendly than gcc's.

Sign up to request clarification or add additional context in comments.

4 Comments

If in pre-C99 we can only declare variables at top of a block,what if inside that block,we declare variables but in a nested block,enclosed in {}.Will it violate the condition as though we might be following the rule by declaring it at top of nested block,we are still defying the rule by declaring variables in middle of the outer block!!
@Jugni, the rule was that you could declare a variable at the beginning of its block. (Otherwise this would have made not much sense.) But really, don't think too much about older versions of C and try to use the modern one with the features it provides.
Thanks for taking the time to answer this basic question.I am fussy about details and hence your answer really helped.
The specific GCC quirk causing confusion is in 2.1. C Language: "Some features that are part of the C99 standard are accepted as extensions in C90 mode, and some features that are part of the C11 standard are accepted as extensions in C90 and C99 modes." GCC allows some of the conveniences of modern C standards, even when compiling earlier versions of C. More details in 3.4. Options Controlling C Dialect.
3

You use gcc : That enable some gcc specific extensions by default. Try :

[igmar@devel ~]$ gcc -std=c89 -pedantic -o xxx xxx.c
xxx.c: In function 'main':
xxx.c:6: warning: ISO C90 forbids mixed declarations and code
xxx.c:8: warning: ISO C90 forbids mixed declarations and code

-pedantic disabled the non-standard gcc extensions.

1 Comment

Thanks for running the program and sharing the warnings.Seems like you too are new to StackOverflow like me!!+1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.