C语言 错误 C2143:语法错误:缺少“;” 在“类型”之前

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15697190/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 05:52:22  来源:igfitidea点击:

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

cvisual-studio-2010

提问by eLg

I am new to programming C.. please tell me what is wrong with this program, and why I am getting this error: error C2143: syntax error : missing ';' before 'type'....

我是 C 编程新手.. 请告诉我这个程序有什么问题,以及为什么我会收到这个错误:错误 C2143:语法错误:缺少 ';' 在“类型”之前......

extern void func();

int main(int argc, char ** argv){
    func();
    int i=1;
    for(;i<=5; i++) {
        register int number = 7;
        printf("number is %d\n", number++);
    }
    getch();
}

回答by Ed S.

Visual Studio only supports C89. That means that all of your variables must be declared before anything else at the top of a function.

Visual Studio 仅支持 C89。这意味着必须在函数顶部的任何其他内容之前声明所有变量。

EDIT: @KeithThompson prodded me to add a more technically accurate description (and really just correct where mine is not in one regard). All declarations (of variables or of anything else) must precede all statements within a block.

编辑:@KeithThompson 催促我添加一个技术上更准确的描述(并且在我的不在某一方面的地方确实是正确的)。所有(变量或其他任何东西的)声明都必须在块中的所有语句之前。

回答by Randy Howard

I haven't used visual in at least 8 years, but it seems that Visual's limited C compiler support does not allow mixed code and variables. Is the line of the error on the declaration for int i=1;?? Try moving it above the call to func();

我至少有 8 年没有使用 Visual,但似乎 Visual 有限的 C 编译器支持不允许混合代码和变量。声明中的错误行是int i=1;?? 尝试将其移动到呼叫上方func();

Also, I would use extern void func(void);

另外,我会用 extern void func(void);

回答by lenik

this:

这个:

int i=1;
for(;i<=5; i++) {

should be idiomatically written as:

应该习惯性地写成:

for(int i=1; i<=5; i++) {

because there no point to declare forloop variable in the function scope.

因为for在函数范围内没有必要声明循环变量。