473,324 Members | 2,370 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,324 software developers and data experts.

variable declaration in for()

I recently sent some code to a friend to go over, and I was told that
c99 doesn't allow variable declarations in for()

in other words you can't do this:

for (int i; i<9; i++);

I can't see what the reason for this not being allowed.
Any comments on this?

Cheers, Matt

Apr 3 '06 #1
5 1248
ballpointpenthief wrote:

I recently sent some code to a friend to go over, and I was told that
c99 doesn't allow variable declarations in for()

in other words you can't do this:

for (int i; i<9; i++);

I can't see what the reason for this not being allowed.
Any comments on this?


(i) needs an initializer.

--
pete
Apr 3 '06 #2

pete wrote:
ballpointpenthief wrote:

in other words you can't do this:

for (int i; i<9; i++);

I can't see what the reason for this not being allowed.
Any comments on this?


(i) needs an initializer.

--
pete


sorry, schoolboy error
for (int i=0; i<9; i++);

Apr 3 '06 #3
ballpointpenthief wrote:

pete wrote:
ballpointpenthief wrote:
in other words you can't do this:

for (int i; i<9; i++);

I can't see what the reason for this not being allowed.
Any comments on this?


(i) needs an initializer.

sorry, schoolboy error
for (int i=0; i<9; i++);


Looks like valid C99 to me.
Code like that, is usually the first clue that I notice
which indicates that I'm looking at C99 code.

--
pete
Apr 3 '06 #4

ballpointpenthief wrote:
I recently sent some code to a friend to go over, and I was told that
c99 doesn't allow variable declarations in for()

in other words you can't do this:

for (int i; i<9; i++);

I can't see what the reason for this not being allowed.
Any comments on this?


With an initialiser, it's perfectly legal - the scope of 'i' ending
with the statement/compund-statement following the for(), e.g.,

for(int i = 0; i < 10; ++i)
{
// use i.
}

// the i above is out of scope here.

Apr 3 '06 #5
"ballpointpenthief" <Ma*************@gmail.com> writes:
I recently sent some code to a friend to go over, and I was told that
c99 doesn't allow variable declarations in for()


Your friend may be thinking of C89. This feature is new in C99.
--
"...what folly I commit, I dedicate to you."
--William Shakespeare, _Troilus and Cressida_
Apr 3 '06 #6

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...
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...
2
by: Thomas Matthews | last post by:
Hi, I'm getting linking errors when I declare a variable in the global scope, but not inside a function. The declarations are the same (only the names have been changed...). class Book {...
134
by: James A. Donald | last post by:
I am contemplating getting into Python, which is used by engineers I admire - google and Bram Cohen, but was horrified to read "no variable or argument declarations are necessary." Surely that...
4
by: Ray | last post by:
Hello, I think I've had JavaScript variable scope figured out, can you please see if I've got it correctly? * Variables can be local or global * When a variable is declared outside any...
14
by: subramanian100in | last post by:
Consider the following program: #include <iostream> using namespace std; int main() { int i;
2
by: Shraddha | last post by:
Can we declare extern variable as static? What will be the scope of the variable then? What if we change the value of the variable in some other function? Also can someone tell me that if we can...
1
NeoPa
by: NeoPa | last post by:
Problem Description : In VBA there is an option to enforce declaration of variables in your code. With this set, any reference to a variable that has not been previously declared (Dim; Private;...
112
by: istillshine | last post by:
When I control if I print messages, I usually use a global variable "int silent". When I set "-silent" flag in my command line parameters, I set silent = 1 in my main.c. I have many functions...
11
by: whirlwindkevin | last post by:
I saw a program source code in which a variable is defined in a header file and that header file is included in 2 different C files.When i compile and link the files no error is being thrown.How is...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
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...
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: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
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: 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: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.