Yash Sharma
Yash Sharma

Reputation: 11

Receiving an error "Main.c:9:22: error: expected ';' in 'for' statement specifier" in C programming

I'm new to C Language and was trying to print the Fibonacci series but I constantly receive the error that is shown in the title. Anybody can help a newbie?

I've tried adding a semi colon after the "i++" and all I get is huge series of negative numbers that automatically terminate the program. I've tried compiling it using 2 different websites that offer c language execution and compilation.

#include<stdio.h>

int main()
{
    int a,b,i,sum;
    i = 1;
    a = 0;
    b = 1;
    for (i <= 10; i++)
    {
        sum = a + b;
        a = b;
        b = sum;
        printf("%d", sum);
    }
    return 0;
}

I expected the output to be 1 1 2 3 5 8 13 21 34 but I got no output at all

Upvotes: 0

Views: 413

Answers (1)

Blaze
Blaze

Reputation: 16876

Change this

for (i <= 10; i++)

To this:

for (; i <= 10; i++)

You have to do this because according to this, the syntax is

for ( init_clause ; cond_expression ; iteration_expression ) loop_statement

In your case there's no init_clause (it's optional), but you still need the ; after it.

You could also remove the i = 1; earlier in your code and instead put it into your loop, like this:

for (int i = 1; i <= 10; i++)

since you don't need i outside of the loop anyway.

Upvotes: 5

Related Questions