473,466 Members | 4,855 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 18126

"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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.