c在Windows 7上从linux到visual studio 2010的编程问题

Ash*_*alz 2 c eclipse visual-studio-2010

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

下面的程序是我们第一个任务的一部分,我编译它并在课堂上在Linux上运行它没有任何问题,但是把它带回家我不能在我的生活中让它在Visual Studio 2010 ultimate中编译,或者使用MinGW编译器的eclipse IDE.

是否存在导致我的代码失败的两个操作系统之间切换的典型问题?还是我,作为我的新手,写了一些丑陋的代码,这些代码与VS 2010或Eclipse不同意?

尝试修复我从VS 2010获得的错误消息似乎是徒劳的,所以我倾向于从我的计算机中缺少必要的东西.我还设置VS 2010来编译C代码,所以我不认为这是问题.

来自VS2010的错误:

project1a.c(38):错误C2143:语法错误:缺少';' 在'
type'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;
}
Run Code Online (Sandbox Code Playgroud)

Mic*_*urr 6

问题是,在编译C代码时,MSVC不支持C99,只支持C90(除了少数库事物).您正在使用MSVC不支持的至少两个C99功能:

  • 最重要的是'可变长度阵列'.修复此问题通常需要对代码进行相当多的更改,如果您以任何重要方式使用它们.我稍后再回过头来看看.

  • 另一个是在"正常"语句之后发生的声明

C99允许在其他类型的语句之后在块中发生声明; C90不允许 - 所有声明都必须在块的开头发生.那么,你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;

    //...
Run Code Online (Sandbox Code Playgroud)

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

要解决该问题,请在块开始后移动声明:

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

    //...
Run Code Online (Sandbox Code Playgroud)

有时,这将要求您重新初始化,而不是.

要解决使用可变长度数组的问题,需要更多的工作.在这种情况下,我认为你可以声明获得通过userArray作为int*和使用它分配存储malloc():

int* userArray;

userArray = malloc( sizeof(int) * n);
Run Code Online (Sandbox Code Playgroud)

其他一些事情:

  • 因为total并且avg不在外面使用sumavg(),它们应该是局部变量(显式初始化为0)
  • 您可能希望n作为参数传递sumavg()而不是使用全局变量
  • 你声明sumavg()为返回int,但不返回任何东西.您应该将声明更改为void