Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS Objective-C Basics (Retired) Functional Programming in C Control Flow - For Loops

I got an error with this code. use option -std=c99 or std=gnuc99 to compile my code, please help.

I used the exact code and the error that I said above kept happening, can you tell me how to fix it please.

6 Answers

Can you post your code, please? Something's amiss!

Steve.

Ok here it is.

#include <stdio.h>

int main()
{
    int many[] = { 2, 4, 8};
    int sum = 0;
    int i;
    for (int i=0; i < 3; i++) {
        sum += many[i];
        printf("sum %d\n", sum);
    }
    return 0;
}

You've declared i as an int twice - could that be the problem?

I removed the int i = 0; and I got the same error so that isn't the problem.

The C99 standard provides support for declaring the loop counter inside the loop, as you have done. That's good practice as the scope of the variable is limite to the loop itself, rather than the whole code block.

If you're not using C99 compliant code, you need to declare the variable outside the loop, as you have also done. I'm not sure which declaration you left in.

I'm trying to replicate the problem in Xcode right now - I'm also seeing if there's a preference somewhere to handle coding conventions like this.

Are you using Xcode or something like CodeBlocks?

I am using eclipse IDE on ubuntu.