469,342 Members | 5,623 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,342 developers. It's quick & easy.

C99 declaration of for loop

I read from the book "The Complete Reference - C" that C99 allows us
to declare for loop variables as

for(int i=0; i < 10; i++)
printf("%d",i);

however, my compiler(gcc) gives me the error:
" 'for' loop initial declaration used outside C99 mode "

Please explain.

Aug 25 '07 #1
7 17391

"Ravi" <ra*********@gmail.comschrieb im Newsbeitrag
news:11*********************@q4g2000prc.googlegrou ps.com...
>I read from the book "The Complete Reference - C" that C99 allows us
to declare for loop variables as

for(int i=0; i < 10; i++)
printf("%d",i);

however, my compiler(gcc) gives me the error:
" 'for' loop initial declaration used outside C99 mode "

Please explain.
What's there top explain, the error says it all

In pre C99 this was not possible and if you don't tell your compiler to run
in c99 mode, he rightfully complains.

Bye, Jojo
Aug 25 '07 #2
Ravi wrote:
I read from the book "The Complete Reference - C" that C99 allows us
/C99/ allows us.
to declare for loop variables as

for(int i=0; i < 10; i++)
printf("%d",i);

however, my compiler(gcc) gives me the error:
" 'for' loop initial declaration used outside C99 mode "
"outside C99 mode": not using C99.
Please explain.
Not using C99 =C99 features not available =observed behaviour.

--
BOOM /Tomorrow/ Hedgehog
Nit-picking is best done among friends.

Aug 25 '07 #3
On Sat, 25 Aug 2007 06:37:39 -0700, Ravi wrote:
however, my compiler(gcc) gives me the error:
" 'for' loop initial declaration used outside C99 mode "

Please explain.
GCC defaults to C89. (Or rather, GNU's version.) You need to add
-std=c99 or similar to your compiler's command line.

B.
Aug 25 '07 #4
"Ravi" writes:
>I read from the book "The Complete Reference - C" that C99 allows us
to declare for loop variables as

for(int i=0; i < 10; i++)
printf("%d",i);

however, my compiler(gcc) gives me the error:
" 'for' loop initial declaration used outside C99 mode "
I think it wants you to move the *declaration* of i to the head of the
block containing the for statement.
Aug 25 '07 #5
Ravi wrote:
I read from the book "The Complete Reference - C" that C99 allows us
to declare for loop variables as

for(int i=0; i < 10; i++)
printf("%d",i);

however, my compiler(gcc) gives me the error:
" 'for' loop initial declaration used outside C99 mode "

Please explain.
The diagnostic is clear: you are not in C99 mode and attempting to do
something allowed in C99 but not with earlier standards.

Note that for all versions of C you can do much the same thing just by
introducing a new block:

{
int i;
for(i=0; i < 10; i++)
printf("%d",i);
}
Aug 25 '07 #6
Ravi wrote:
>
I read from the book "The Complete Reference - C" that C99 allows us
to declare for loop variables as

for(int i=0; i < 10; i++)
printf("%d",i);

however, my compiler(gcc) gives me the error:
" 'for' loop initial declaration used outside C99 mode "

Please explain.
By default gcc is in C90 mode. Read its info (or man) docs.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>
--
Posted via a free Usenet account from http://www.teranews.com

Aug 26 '07 #7
CBFalconer said:

<snip>
By default gcc is in C90 mode.
No, it isn't. By default, gcc is in gcc mode. You have to kick it
moderately hard to get it into C90 mode.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Aug 26 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

83 posts views Thread by Alexander Zatvornitskiy | last post: by
2 posts views Thread by Christophe Grimault | last post: by
8 posts views Thread by Alex Vinokur | last post: by
39 posts views Thread by Gaijinco | last post: by
10 posts views Thread by Zachary Turner | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.