Shop OBEX P1 Docs P2 Docs Learn Events
Why can't I use for(int i = 0; i < 10; i++) ? — Parallax Forums

Why can't I use for(int i = 0; i < 10; i++) ?

BeanBean Posts: 8,129
edited 2012-07-06 13:58 in Propeller 1
When I try to declare a variable inside a "for" statement I get an error

test.c:12:3: error: 'for' loop initial declarations are only allowed in C99 mode

How do I enable C99 mode ?

Bean

Comments

  • Brian FairchildBrian Fairchild Posts: 549
    edited 2012-06-20 10:53
    you need '-std=c99' on your command line or in the 'Other Compiler Options' box on the compiler tab in SimpleIDE.
  • Dave HeinDave Hein Posts: 6,347
    edited 2012-06-20 11:05
    Oh wow, I learned something new. I always thought that was a C++ thing. The inline declaration should be avoided if you want your code to be ANSI compliant.
  • Daniel HarrisDaniel Harris Posts: 207
    edited 2012-06-20 11:32
    I remember struggling with that a while back when I was in school trying to write programs for a class. I had quite a time trying to figure out why the compiler was complaining. In the end, once I moved the declarations up to the top, everything worked fine. I think that rule enforcement even helped me learn how to write cleaner code.
  • KyeKye Posts: 2,200
    edited 2012-06-20 13:30
    In OOP languages its considered cleaner to put the variable in the smallest scope as possible. Putting the 'i' variable in the loop makes the 'i' variable only visible in the loop.
  • pedwardpedward Posts: 1,642
    edited 2012-06-20 17:13
    My recollection is that ANSI C allows variable declarations inside any set of braces. This means you can add another set of braces and declare another scope, like in an IF statement.
    #include <stdio.h>
    
    void main(void)
    {
        int foo;
    
        for (foo = 0; foo < 10; foo++) {
            int bar = 1;
    
            printf("foo: %d\n",foo);
            printf("bar: %d\n",bar);
        }
    
        {
            int bar = 2;
            printf("bar: %d\n",bar);
        }
    
        printf("foo: %d\n",foo);
    //    printf("bar: %d\n",bar);
    
    }
    
    
    

    The above compiles and runs, if you uncomment the bottom printf you will get a compile error.
  • TorTor Posts: 2,010
    edited 2012-07-06 06:47
    All correct about the scope, just as you demonstrated.However, you have a couple of non-ANSI constructs in your example :)
    - '//' , aka C++ comments isn't allowed in ANSI (although it is in C99. Personally I find them unreadable)
    - 'main' shall be 'int', not 'void', so you should use 'int main(void)' (on some architectures it _does_ make a difference - the exit handling doesn't work correctly unless main is 'int').
    - Due to the above there needs to be a 'return (0);' at the end.

    -Tor
  • jazzedjazzed Posts: 11,803
    edited 2012-07-06 13:58
    Tor wrote: »
    All correct about the scope, just as you demonstrated.However, you have a couple of non-ANSI constructs in your example :)
    - '//' , aka C++ comments isn't allowed in ANSI (although it is in C99. Personally I find them unreadable)
    - 'main' shall be 'int', not 'void', so you should use 'int main(void)' (on some architectures it _does_ make a difference - the exit handling doesn't work correctly unless main is 'int').
    - Due to the above there needs to be a 'return (0);' at the end.

    -Tor

    GCC can be used with strict c89 mode for folks who need it.

    $ propeller-elf-gcc -pedantic -std=c89 foo.c
Sign In or Register to comment.