代码在GCC上编译良好,但在VC2010上出现错误

时间:2013-08-29 08:07:16

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

我用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;
}

3 个答案:

答案 0 :(得分:4)

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);

答案 1 :(得分:2)

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

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

srand(time(NULL));

在所有变量声明之后。


参考:

  

C89§3.6.2复合语句或块

     

语法

     

化合物语句

     

{declaration-list opt statement-list opt}

     

声明列表

     

声明

     

声明列表声明

     

语句列表

     

语句

     

陈述清单陈述

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

  

C99§6.8.2复合声明

     

语法

     

化合物语句

     

{block-item-list_opt}

     

块项目列表:

     

块项

     

block-item-list block-item

     

块项:

     

声明

     

语句

在C99中,声明和陈述可以混合使用。

答案 2 :(得分:0)

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

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;
    ...