Amazon.com Widgets 'for' loop outside c99 mode
Welcome, Guest. Please login or register.
Did you miss your activation email?
July 30, 2014, 08:18:52 PM
Home Help Search chat Login Register   
News: Read this please.The Great Kangaroo Escape Looking for reviews of the 4th ed on Amazon!   Twitter:  @skochan
                     

+  Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
|-+  Old Stuff
| |-+  Chapter Study
| | |-+  Chapter 5 - Program Looping
| | | |-+  'for' loop outside c99 mode
Pages: [1]   Go Down
Print
Author Topic: 'for' loop outside c99 mode  (Read 1686 times)
edward.h
Newbie
*
Posts: 12






« on: July 09, 2011, 02:18:42 AM »

Hello everyone, Mr.Kochan,

Not really a big problem, but I want to understand it. Code examples from others clearly show this is possible.

When I declare i inside the for loop, I get the error
Code: (Objective-C)
// for ( int i = 1; i < 11; ++i ) {
main.m:18: error: ‘for’ loop initial declaration used outside C99 mode

Code: (Objective-C)
    // int i; // intializing it here wont give any errors.
   
    printf("  n    Squared\n");
    printf("----  ---------\n");
   
    for ( int i = 1; i < 11; ++i ) { // but it does here
        printf(" %2i    %i\n", i, i*i);
   }

I replaced all the printf() with NSLog(), but I will still get the error.

I know a little tiny bit of C, so i tried to use printf() here after reading it can be done.

Thank you!
Logged
skochan
Administrator
Hero Member
*****
Posts: 3114







« Reply #1 on: July 09, 2011, 11:18:38 AM »

How are you compiling your program?  From the command line?

Cheers,

Steve
Logged
edward.h
Newbie
*
Posts: 12






« Reply #2 on: July 09, 2011, 08:54:08 PM »

Yes I used command line to compile. Seeing your question, I tried doing it in xcode and have no problem at all. I understand now after some digging that it is the gcc compiler being in c89 mode (but how does it know about c99 then, weird!!), which doesn't support the inline declaration that was used.

Same error in C too, I tried a similar program in C and got the exact same error message.

typing > gcc -v
The gcc version is 4.2.1 on my Terminal, on Wikipedia the c99 page says GCC will have full C99 features in version 4.6 in June 2011. Maybe Lion will have 4.6 for the Terminal, Lion just a few days away! ^_^ (Edit: Upon further reading, its probably not as simple as that.)

I'm using terminal because I didn't want to create a new project for each question and xcode is intimidating, is that how everyone is doing it,  a project for each question?

Mr.Kochan I'm totally enjoying your book (3rd ed), I find it easy to digest, can't wait to finish it and love this forum, it helps a lot.
« Last Edit: July 09, 2011, 11:40:57 PM by edward.h » Logged
skochan
Administrator
Hero Member
*****
Posts: 3114







« Reply #3 on: July 10, 2011, 11:30:45 AM »

Use the option -std=c99 on the command line.

Cheers,

Steve
Logged
edward.h
Newbie
*
Posts: 12






« Reply #4 on: July 11, 2011, 05:06:27 AM »

That totally worked. Smiley Thank you!
Code: (Objective-C)
> gcc -std=c99 -framework Foundation main.m -o output
Logged
Pages: [1]   Go Up
Print
Jump to:  



Login with username, password and session length

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Entire forum contents (c) 2009 classroomM.com. All rights reserved.