473,325 Members | 2,671 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

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 18107

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

83
by: Alexander Zatvornitskiy | last post by:
Hello All! I'am novice in python, and I find one very bad thing (from my point of view) in language. There is no keyword or syntax to declare variable, like 'var' in Pascal, or special syntax in...
2
by: Christophe Grimault | last post by:
Hi all, look at the following code: ////////////////////////// for(int i = 0; i<10; i++) cout << i; for(int i = 0; i<20; i++) cout << i;
7
by: YGeek | last post by:
Is there any difference between declaring a variable at the top of a method versus in the code of the method? Is there a performance impact for either choice? What about if the method will return...
8
by: Alex Vinokur | last post by:
=========== Windows 2000 Intel C++ 8.0 =========== ------ foo.cpp ------ int main () { for (int i = 0; i < 10; i++); for (int i = 0; i < 10; i++);
39
by: Gaijinco | last post by:
I have always felt that you should only declared variables as needed, and implicitily it seems many authors to encourage it, but the other day a friend told me that declaring variables inside a...
21
by: Kannan | last post by:
Its been a while I have done pure C programming (I was coding in C++). Is the following function valid according to standard C? int main(int argc, char *argv) { int x; x = 9; printf("Value...
8
by: Pedro Pinto | last post by:
When compiling my program i got this error: Error: 'for' loop initial declaration used outside c99 mode What is it and how can i solve it? Thanks in advance! Regards
28
by: Larax | last post by:
Best explanation of my question will be an example, look below at this simple function: function SetEventHandler(element) { // some operations on element element.onclick = function(event) {
10
by: Zachary Turner | last post by:
Hello, This seems like an extremely basic question, and I'm a bit embarassed that I can't answer it myself. I just recently started using GCC and tried to type the following code: while...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.