473,581 Members | 2,314 Online
Bytes | Software Development & Data Engineering Community
+ 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 18142

"Ravi" <ra*********@gm ail.comschrieb im Newsbeitrag
news:11******** *************@q 4g2000prc.googl egroups.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
6459
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 C. It can cause very ugly errors,like this: epsilon=0 S=0 while epsilon<10: S=S+epsilon
2
6055
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
2658
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 before the variable is used? I'm trying to get an idea of whether the .NET compilers for VB.NET and C# will move all variable declaration to the...
8
3129
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
2391
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 loop wasn't good practice, something like: for(int i=0; i<size-1; ++i){ int aux = array; array=array; array=aux; }
21
2818
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 of x is: %d\n", x);
8
74556
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
4299
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
4127
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 ((int i = getint()) != 0) {
0
7862
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8301
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7894
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8169
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5670
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3803
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3820
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2300
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 we have to send another system
0
1132
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.