C99 declaration of for loop

R

Ravi

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.
 
J

Joachim Schmitz

Ravi said:
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
 
C

Chris Dollin

Ravi said:
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.
 
R

Rob Kendrick

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.
 
O

osmium

Ravi said:
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.
 
C

CBFalconer

Ravi said:
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.
 
M

Martin Ambuhl

Ravi said:
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);
}
 
R

Richard Heathfield

CBFalconer said:

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.
 
S

Stephen Sprunk

CBFalconer said:
By default gcc is in C90 mode. Read its info (or man) docs.

The documentation claims it's in "GNU89" mode by default. However, the
"-ansi -pedantic" options frequently suggested here will put it in C89/C90
mode. "-std=c99" is needed for C99 mode.

S
 
R

Richard

Stephen Sprunk said:
The documentation claims it's in "GNU89" mode by default. However,
the "-ansi -pedantic" options frequently suggested here will put it in
C89/C90 mode. "-std=c99" is needed for C99 mode.

This is quite an interesting thread considering the frequency with which
CBFalconer is correcting people's understanding of "standard C".

It's actual default is C90 with GnuCC extensions which as you rightly
point is GNU89 mode.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top