0

I've got this problem where I can only compile using the gcc -std=c99 but however, i need it to compile using c89 aka gcc -Wall. This is part of my code where i use the 'for' loop. Please see if you can help me out thank you in advance.

#include<stdio.h>
int main()
{
    int arr[100],i=0,ch;
    int n = 1, sum = 0;
    printf("Check out our selection! \n");
    printf("Airhead - 25 cents\n");
    printf("Fun Dip - 40 cents\n");
    printf("Gummi Bears - 20 cents\n");
    while (n != 0)
    {
        printf("Insert Coins: ");
        scanf("%d",&n);
        arr[i++] = n;
    }

    for(int j=0;j<i;j++)
    {   sum = sum + arr[j];
    }
......
4
  • 3
    Define the variable j at the start of the function, together with the other variables? Commented May 27, 2017 at 20:06
  • 2
    "i need it to compile using c89 aka gcc -Wall" <- these are different thinks. Why do you need this? Commented May 27, 2017 at 20:08
  • 2
    Possible duplicate of How do I fix "for loop initial declaration used outside C99 mode" GCC error? Commented May 27, 2017 at 20:09
  • 1
    gcc without a language option compiles for gnu11 (which is neither c89 nor c99 nor c11). See the gcc manual. For c89 you want gcc -std=c89 -pedantic ... Commented May 27, 2017 at 20:10

1 Answer 1

2

This is wrong:

for (int j = 0; j < i; j++) {
    sum = sum + arr[j];
}

You have to initialize j in beginning of function.

 int main() {
    int j;
    ...
    for (j = 0; j < i; j++) {
        sum = sum + arr[j];
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.