Visual Studio 2010在尝试调试时给出了语法错误,但没有语法错误!

时间:2011-06-17 03:16:32

标签: c visual-studio-2010 syntax syntax-error

我正在使用Visual Studio 2010,我正在尝试运行一个用C编写的简单程序,但是当我使用F5时,我遇到了大量的语法错误。几乎所有这些都是因为:

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

以下是我的主要功能:

#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;
}

您看到的第一个int值是第12行,VS2010的第一个错误是在第12行(char 1)报告的,这是该int的位置。根据报告我错过了“;”的消息(我显然不是这样),然后继续告诉我价值是未宣布的。

我该如何解决这个问题?我知道我的程序实际上没有语法(或任何)错误 - 这是以前编写和测试的,可以在UCCntu上使用GCC编译器。

有人可以帮忙吗?这非常令人沮丧:S

2 个答案:

答案 0 :(得分:5)

ANSI C不允许在块的中间定义变量 - 它们必须在块的顶部定义。后来的C99标准删除了这个限制(就像C ++一样),但Visual Studio不支持C99。

使用顶部的变量声明重写您的函数:

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作为扩展,允许在默认情况下在块的中间定义变量。如果指定-pedantic命令行选项(不含-std=c99),则会收到以下警告:

warning: ISO C90 forbids mixed declarations and code

答案 1 :(得分:0)

您是否尝试过重新启动新项目以确保项目未损坏。