Re: Error: 'for' loop initial declaration used outside c99 mode




james of tucson escreveu:
Pedro Pinto wrote:
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?

You're coming to C from a Java background, aren't you?

This is illegal in most C dialects; it is a legal C++ declaration, and
so may be accepted if you are compiling C with a C++ compiler:

for( int k=0; k<10; k++){}

I do like that syntax since it's such a common idiom to have a loop
iterator only in the scope of the loop.


Hi again!

Yes, the issue was that i was declaring the int i inside the for! =)

And yes, i first started programming in Java, thus my dificulties in
understanding certain apects of C! Thanks a million

.



Relevant Pages

  • Re: "Sorting" assignment
    ...     If clause-1 is a declaration, the scope of any variables it declares ... in that direction lies COBOL. ... >I don't declare anything inside of a loop is why. ...
    (comp.programming)
  • Re: About String
    ... Ada, where declarations are just as executable as statements. ... I think it does make sense because it clearly defines the scope ... anywhere outside the declaration part. ... for I in A'Range loop ...
    (comp.lang.ada)
  • Re: Newbies question
    ... for I in Elements'Range loop ... I ruled out the declaration of a local array within the block statement Copy_List, because I thought that the compiler would mandate a return statement to be at the top level of the function's body, which I wouldn't be able to do, since the declared array would be local to the block statement. ...
    (comp.lang.ada)
  • Re: static or not static? That is the question!
    ... anything else but a normal declaration that can accept ANY ... This loop would be run only ONCE in the program's lifetime. ... What is strange is to FORBID it, ... I can't imagine wanting to declare a static object in a for statement. ...
    (comp.std.c)
  • Re: Which scope for variables being used in a loop?
    ... can't be GC'd until the scope they are declared in exist. ... inside of the loop. ... overlay local allocations in another loop. ... declaration itself, such that the pointer-value of the variable is ...
    (comp.lang.java.programmer)