c programing

hi experts,
can we declare a variable inside for loops or others such.
eg. for(int i=0;i<=100;i++)

i m sandip.
sandipmurmuAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mlmccCommented:
What do you mean/

mlmcc
0
fl0ydCommented:
Nope, that doesn't work in c, but has been introduced in c++ and is generally referred to as on-the-fly variable declaration.

.f
0
mlmccCommented:
for(int i=0;i<=100;i++)  works just fine in C.

mlmcc
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

ozoCommented:
It doesn't work in strictly conforming ANSI Standard X3.159-1989 C, but because it works in C++ some C compilers will recognise it
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
burtdavCommented:
It is allowed in the newer 1999 ISO C standard (known as C99).
0
Mayank SAssociate Director - Product EngineeringCommented:
Strictly speaking, it won't work in C. It will work in C++, though. C++ allows you to declare variables anywhere in your program so that you don't have to go through the process of declaring everything right at the start of the function, but at your ease, you can just declare more and more variables anywhere whenever you need them.  

mlmcc said that it'll work on C, but probably he's not tried it in a pure C compiler. C++ compilers will allow it. However, I guess that if you save the file with a .C extension (instead of .CPP), then even a C++ compiler will report an error.

Mayank.
0
burtdavCommented:
Can I disagree, Mayank? As I just stated above, it is valid in C99, which is "pure C".
0
Mayank SAssociate Director - Product EngineeringCommented:
Checked it on http://home.tiscalinet.ch/t_wolf/tw/c/c9x_changes.html

You're right. Its allowed.

Mayank.
0
fl0ydCommented:
You will most likely have to set compiler switches though, to enable compilation conforming with c99 standard.

.f
0
CleanupPingCommented:
sandipmurmu:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0
mlmccCommented:
I believe I answered it.

mlmcc
0
burtdavCommented:
(Sorry mlmcc) I think fl0yd, ozo, myself (burtdav) and mayankeagle were on the right track. There's definitely an A to be given between us. I'd be happy to take the points if none of the other 3 want them.
0
fl0ydCommented:
As long as the points don't go to mlmcc, I'm happy with anything. My suggestion would be to split points between burtdav (first one to draw attention to the evolution of c) and mayankeagle for the helpful link.

.f
0
Mayank SAssociate Director - Product EngineeringCommented:
Thank you. My recommendation was also to split points between burtdav, ozo, fl0yd and mayankeagle.
0
burtdavCommented:
I think it'd be classy to split 20 points 4 ways.
0
fl0ydCommented:
Then again, points are nothing but a side effect. In essence choosing answers will make it easier for others searching through PAQ's finding an answer quickly.

.f
0
Mayank SAssociate Director - Product EngineeringCommented:
>> it'd be classy to split 20 points 4 ways

if its an A grade, then its still 20 points to each. Anyways, hardly matters!
0
burtdavCommented:
Well chosen, AnnieMod.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.