从 Linux 到 Windows 7 上的 Visual Studio 2010 的 C 编程问题

标签 c eclipse visual-studio-2010

基本上,我一直在上 C 编程类(class),在该类(class)中我们使用 Linux 机器来编写和编译我们的代码。

下面的程序是我们第一个作业的一部分,我在类里面编译了它并在 Linux 上运行它,没有出现任何问题,但是把它带回家后我一辈子都无法在 Visual Studio 2010 Ultimate 中编译它,或带有 MinGW 编译器的 eclipse IDE。

在两个操作系统之间切换是否存在导致我的代码失败的典型问题?或者,作为一个菜鸟,我是否编写了一些与 VS 2010 或 Eclipse 不兼容的丑陋代码?

尝试修复我从 VS 2010 收到的错误消息似乎是徒劳的,所以我倾向于寻找我的计算机中缺少的一些基本内容。我还设置了 VS 2010 来编译 C 代码,所以我认为这不是问题。

VS2010 的错误:

project1a.c(38):错误 C2143:语法错误:缺少“;”在“输入”之前
project1a.c(41): 错误 C2065: 'i' : 未声明的标识符
project1a.c(44):错误 C2065:'userArray':未声明的标识符
project1a.c(44):错误 C2065:'i':未声明的标识符
project1a.c(44): 错误 C2109: 下标需要数组或指针类型
project1a.c(51):错误 C2065:'userArray':未声明的标识符

这些错误之间有多个“i”实例:未声明的标识符错误

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

int n;
float total, avg;

int sumavg(void);

int main(void)
{
    //First time scan for the value to be assigned to n.
    printf("Hey, Enter a number or 999 to exit:> ");
    scanf("%d", &n);

    //if n == 999 then exit the program
    while(n != 999)
    {   
        //enter the sumavg function.
        sumavg();

        //Try to run the program again.
        printf("Hey, Enter a number or 999 to exit:> ");
        scanf("%d", &n);
    }

    //exit program. 
    return EXIT_SUCCESS;    
}

int sumavg(void)
{


    //Define a number that will be used for the array size.
    printf("Hey, now enter %d more numbers:>\n", n);

    //Define the size of array using the number assigned to the variable "n".
    int userArray[n], i;

    //Construct the array, one position at a time using the for loop.
    for (i = 0; i < n; i++)
    {
        //Assign a value to userArray[i] while i < n(the size of the array).
        scanf("%d", &userArray[i]);
    }

    //Calculate the sum by looping through each position in the userArray[i].
    for (i = 0; i < n; i++)
    {
        //Take the current position in the array and add it to the variable: "total"
        total += userArray[i];
    }

    //Calculate the average
    avg = total / n;

    //Print the sum followed by the average
    printf("Sum is: %.1lf\n", total);
    printf("The average is: %.1lf\n", avg);

    //reset total and avg in case future iterations are performed.
    total = 0;
    avg = 0;
}

最佳答案

问题是,在编译 C 代码时,MSVC 不支持 C99,仅支持 C90(可能除了一些库之外)。您正在使用至少两个 MSVC 不支持的 C99 功能:

  • 最重要的是“可变长度数组”。如果您以任何重要方式使用它们,修复此问题通常需要对代码进行大量更改。我稍后会再讨论这个问题。

  • 另一种是在“正常”语句之后发生的声明

C99 允许声明出现在其他类型的语句之后的 block 中; C90 不允许这样做 - 所有声明都必须出现在 block 的开头。所以,然后你声明 userArray例如:

int sumavg(void)
{
    //Define a number that will be used for the array size.
    printf("Hey, now enter %d more numbers:>\n", n);

    //Define the size of array using the number assigned to the variable "n".
    int userArray[n], i;

    //...

这在 C90 中是不允许的,并且在 C 模式下编译时 MSVC 会提示它(如果编译 C++ 则不会,因为 C++ 支持这种事情)。

要解决该问题,请将声明移到 block 开始之后:

int sumavg(void)
{
    //Define the size of array using the number assigned to the variable "n".
    int userArray[n], i;

    //Define a number that will be used for the array size.
    printf("Hey, now enter %d more numbers:>\n", n);

    //...

有时这需要您重新调整初始化等等。

要解决使用可变长度数组的问题需要更多工作。在这种情况下,我认为你可以声明 userArray作为int*并使用 malloc() 为其分配存储空间:

int* userArray;

userArray = malloc( sizeof(int) * n);

其他一些事情:

  • totalavg不在 sumavg() 之外使用,它们应该是局部变量(显式初始化为 0)
  • 您可能想通过 n作为 sumavg() 的参数而不是使用全局变量
  • 您声明 sumavg()返回 int ,但不返回任何内容。您可能应该将声明更改为 void

关于从 Linux 到 Windows 7 上的 Visual Studio 2010 的 C 编程问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9780967/

相关文章:

c# - 使用 Crystal Report 在表/网格中显示记录详细信息

c - 使用 read() 逐字节读取文件

java - 当程序在 Debug模式下运行时,有什么方法可以查看 java 程序中数组的内容吗?

c++ - 使用 CreateFile 函数创建文件会导致写保护文件

java - 如何编辑/加载 Eclipse 插件

java - 打开 pom.xml 时 Eclipse 编辑器不会打开

asp.net - 如何在 Visual Studio 2010 中设置 Umbraco cms?

c - 如何判断文件是否在FAT系统上(看是否真的可执行)

c - 如何修复 gcc 错误 : expected while before void

在 C 编程中,无法在主函数内调用 void 用户定义函数