10

Possible Duplicate:
How do I fix “for loop initial declaration used outside C99 mode” GCC error?

Why must I declare a loop variable outside of the for loop statement? I am getting a gcc (MacOSX) error which reads:

error: ‘for’ loop initial declaration used outside C99 mode

If I define my loop variable outside of the loop statement then gcc stops complaining.

4
  • Because, in ANSI C and C89, variables could be declared only at file scope or at the beginning of a block. C99 introduced declaration of variables inside a for loop clause. Commented May 4, 2011 at 4:49
  • Consider using Clang instead of GCC on Mac OS X. Apple won’t update GCC in the foreseeable future. Commented May 4, 2011 at 4:52
  • Exact duplicate of the question that Chuck linked to. That said, just set -std=c99 or -std=gnu99 and you're all set. Commented May 4, 2011 at 6:35
  • I did search for older questions but missed them for some reason. Next time I will do a deep search. Commented May 5, 2011 at 4:11

2 Answers 2

15

As the error suggests, this is because declaring a variable inside the condition of a for-loop wasn't allowed until C99, and you are using an older language standard. If you're compiling directly, use the -std=c99 flag. In Xcode, go to the "Compiler - Language" options for your target and set the Language Standard to either C99 or GNU99.

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

Comments

3

You need to compile with the option -std=c99.

For example:

$ gcc -std=c99 code.c

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.