Rahn
Rahn

Reputation: 5405

Can't declare variables after for loop in VS2012

Here is my code:

//main.c
int main()
{
    int i=0;
    for (i = 0; i < 10; i++)
    {
        //do nothing
    }
    int temp = 0;
    return 0;
}

The code could be compiled with my clang(602.0.49) but produces error C2143 in VS 2012:

error C2143: syntax error : missing ';' before 'type'

Everything is good after modifying the suffix to .cpp or deleting int temp = 0;

Does this mean that I can't declare a variable after for loop in C project?

Upvotes: 1

Views: 112

Answers (1)

ouah
ouah

Reputation: 145829

Visual Studio 2012 only supports the C89 dialect.

The C89 dialect forces you to put declarations before statements in the same block.

To fix the error, move the temp declaration right after the i declaration.

Upvotes: 5

Related Questions