【问题标题】:Code compiles fine on GCC but loads of error on VC2010代码在 GCC 上编译良好,但在 VC2010 上出现大量错误
【发布时间】:2013-08-29 08:07:15
【问题描述】:

我用 C 语言做了一个数字猜谜游戏,它可以在 GCC 4.7.1 中正常编译。但它给VC2010带来了很多错误。主要是语法错误问题。我不知道为什么它会出现这些错误,因为我已经使用 VC2010 编译了许多其他 C 源代码而没有这些错误。

我用 VC(命令行)编译它,命令:cl guessgame.c

VC 给出以下错误:

Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 16.00.30319.01 for
Copyright (C) Microsoft Corporation.  All rights reserved.

guessgame.c
guessgame.c(8) : error C2143: syntax error : missing ';' before 'type'
guessgame.c(9) : error C2143: syntax error : missing ';' before 'type'
guessgame.c(10) : error C2143: syntax error : missing ';' before 'type'
guessgame.c(11) : error C2143: syntax error : missing ';' before 'type'
guessgame.c(12) : error C2143: syntax error : missing ';' before 'type'
guessgame.c(13) : error C2143: syntax error : missing ';' before 'type'
guessgame.c(14) : error C2143: syntax error : missing ';' before 'type'
guessgame.c(20) : error C2065: 'chances' : undeclared identifier
guessgame.c(21) : error C2065: 'randRangeX' : undeclared identifier
guessgame.c(21) : error C2065: 'randRangeY' : undeclared identifier
guessgame.c(23) : error C2065: 'i' : undeclared identifier
guessgame.c(23) : error C2065: 'chances' : undeclared identifier
guessgame.c(23) : error C2065: 'i' : undeclared identifier
guessgame.c(23) : error C2065: 'i' : undeclared identifier
guessgame.c(25) : error C2065: 'chances' : undeclared identifier
guessgame.c(25) : error C2065: 'i' : undeclared identifier
guessgame.c(26) : error C2065: 'userAns' : undeclared identifier
guessgame.c(28) : error C2065: 'userAns' : undeclared identifier
guessgame.c(28) : error C2065: 'randomNumber' : undeclared identifier
guessgame.c(30) : error C2065: 'chances' : undeclared identifier
guessgame.c(30) : error C2065: 'i' : undeclared identifier
guessgame.c(36) : error C2065: 'wrong_guesses' : undeclared identifier
guessgame.c(39) : error C2065: 'wrong_guesses' : undeclared identifier
guessgame.c(41) : error C2065: 'randomNumber' : undeclared identifier

代码如下:

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

int main()
{
    srand(time(NULL));
    int randomNumber = (rand() % 245)+5;
    int userAns;
    int randRangeX = randomNumber-((rand() % 3)+5);
    int randRangeY = randomNumber+((rand() % 3)+5);
    int chances = 3;
    int wrong_guesses = 0;
    int i;

    printf("Number guessing game v1.1.\n");
    printf("Copyright(c) 2013 - Ahnaf Tahmid.\n");
    printf("-----------------------------------\n\n");
    printf("Rules: 1) Guess the number.\n");
    printf("       2) You have %d chances.\n\n", chances);
    printf("The number is between %d and %d.", randRangeX, randRangeY);

    for(i = chances; i >= 1; i--)
    {
        printf("\nGuess %d: ", (chances-i)+1);
        scanf("%d", &userAns);
        while(getchar() != '\n');
        if(userAns == randomNumber)
        {
            printf("\nGood guess! You guessed it in %d turn(s).\n", (chances-i)+1);
            break;
        }
        else
        {
            printf("Aww, bad guess.\n");
            wrong_guesses += 1;
        }
    }
    if(wrong_guesses == 3)
    {
        printf("\nGame Over!\n\nThe answer was %d.\n", randomNumber);
    }
    getch();
    return 0;
}

【问题讨论】:

  • 源代码中的第 8 行是哪一行?
  • @verbose: int randomNumber = (rand() % 245)+5;

标签: c gcc compiler-errors visual-c++


【解决方案1】:

VC2010 支持 C89,它要求变量在其作用域块的开头声明。如果在变量声明之后移动 srand 调用并延迟初始化依赖于 rand() 的变量,则可以使用这两种编译器进行构建。

int main()
{
    int randomNumber;
    int userAns;
    int randRangeX;
    int randRangeY;
    int chances = 3;
    int wrong_guesses = 0;
    int i;
    srand(time(NULL));
    randomNumber = (rand() % 245)+5;
    randRangeX = randomNumber-((rand() % 3)+5);
    randRangeY = randomNumber+((rand() % 3)+5);

【讨论】:

  • 但这不会导致程序无法提供正确的随机数,因为应该在调用 rand() 之前声明随机种子?
  • 是的,我刚刚进行了编辑以解决这个问题。看起来我们的帖子重叠了。
【解决方案2】:

在 C89 中,所有变量都必须在块的开头声明。这已在 C99 中删除。

但是 VC2010 不支持 C99。所以你必须把

srand(time(NULL));

在所有变量声明之后。


参考:

C89 §3.6.2 复合语句或块

语法

复合语句

{ 声明列表选择 声明列表选择 }

声明列表

声明

声明列表声明

语句列表

声明

语句列表语句

请注意,在 C89 中,声明列表必须位于块中的语句列表之前。

C99 §6.8.2 复合语句

语法

复合语句

{ block-item-list_opt }

块项目列表:

方块物品

块项目列表块项目

方块物品:

声明

声明

在 C99 中,声明和语句可以混合使用。

【讨论】:

    【解决方案3】:

    解决其他答案指出的问题的另一种方法是打开一个新范围:

    int main()
    {
      srand(time(NULL));
    
      {
        int randomNumber = (rand() % 245)+5;
        int userAns;
        int randRangeX = randomNumber-((rand() % 3)+5);
        int randRangeY = randomNumber+((rand() % 3)+5);
        int chances = 3;
        int wrong_guesses = 0;
        int i;
        ...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-02
      • 1970-01-01
      • 2011-12-29
      • 1970-01-01
      相关资源
      最近更新 更多