jayjyli
jayjyli

Reputation: 821

Visual Studio 2010 gives me syntax errors when trying to debug, but there are no syntax errors!

I'm using Visual Studio 2010, and I'm trying to run a simple program written in C, but I get tons of syntax errors when I F5 it. Almost all of them are because of this:

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

Here's how my main function looks:

#include <stdio.h>
#include <stdlib.h>

int main()
{
    printf("Enter base 10 num: ");
    int value; scanf("%d", &value);
    printf("Enter base floor (min 2): ");
    int min; scanf("%d", &min);
    printf("Enter base ceiling (max 10): ");
    int max; scanf("%d", &max);
    base_convert(value, min, max);
    return 0;
}

The first int value you see is line 12, and VS2010's first error is reported at line 12, char 1, which is the location of that int. Following the message that reports I'm missing a ";" (which I'm clearly not), it then goes on to tell me that value is undeclared.

How do I fix this? I know that my program doesn't actually have syntax (or any) errors - this was previously written and tested to work on Ubuntu with GCC compiler.

Can someone help? This is extremely frustrating :S

Upvotes: 1

Views: 1344

Answers (2)

Adam Rosenfield
Adam Rosenfield

Reputation: 400384

ANSI C does not allow variables to be defined in the middle of a block—they must be defined at the top of the block. The later C99 standard removes this restriction (like C++ does), but Visual Studio does not support C99.

Rewrite your function with the variable declarations at the top:

int main()
{
    int value, min, max;
    printf("Enter base 10 num: ");
    scanf("%d", &value);
    printf("Enter base floor (min 2): ");
    scanf("%d", &min);
    printf("Enter base ceiling (max 10): ");
    scanf("%d", &max);
    base_convert(value, min, max);
    return 0;
}

GCC, as an extension, allows variables to be defined in the middle of the block by default. If you specify the -pedantic command line option (without -std=c99), you will get the following warning:

warning: ISO C90 forbids mixed declarations and code

Upvotes: 5

Craig T
Craig T

Reputation: 2742

Have you tried restarting with a new project to ensure that your project is not corrupted.

Upvotes: 0

Related Questions