Basically, I’ve been taking a class on C programming, within that class we use Linux machines to write and compile our code.
The program below is a part of our first assignment, I compiled it and ran it on Linux during the class with no issues, but having taken it home I cannot for the life of me get it to compile in Visual studio 2010 ultimate, or the eclipse IDE with the MinGW compiler.
Is there some typical issue with switching between the two operating systems that is causing my code to fail? or have I, being the rookie I am, written some ugly code that just won’t agree with VS 2010 or Eclipse?
Attempts to fix error messages that I have been getting from VS 2010 are it seems futile, so I’m leaning toward something essential missing from my computer. I have also set VS 2010 to compile C code so I dont think that is the issue.
errors from VS2010:
project1a.c(38): error C2143: syntax error : missing ‘;’ before ‘type’
project1a.c(41): error C2065: ‘i’ : undeclared identifier
project1a.c(44): error C2065: ‘userArray’ : undeclared identifier
project1a.c(44): error C2065: ‘i’ : undeclared identifier
project1a.c(44): error C2109: subscript requires array or pointer type
project1a.c(51): error C2065: ‘userArray’ : undeclared identifier
There are multiple instances of the ‘i’: undeclared identifier error inbetween these errors
#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;
}
The problem is that when compiling C code, MSVC does not support C99, only C90 (except maybe for a few library things). You’re using at least two C99 features that MSVC doesn’t support:
the big one is ‘variable length arrays’. Fixing this will generally require quite a bit of change to your code if you use them in any significant way. I’ll get back to this later.
the other one is declarations that occur after ‘normal’ statements
C99 permits declarations to occur in a block after other kinds of statements; C90 doesn’t permit that – all declarations have to occur at the beginning of a block. So, then you declare
userArrayfor example:That is not permitted in C90 and MSVC complains about it when compiling in C mode (it won’t if compiling C++, since this kind of thing is supported in C++).
To fix that problem move your declarations just after the start of a block:
Sometimes that will require you to rejigger initializations and what not.
To fix the problem of using variable length arrays requires more work. In this case I think you can get by with declaring
userArrayas anint*and allocating the storage for it usingmalloc():A few other things:
totalandavgaren’t used outside ofsumavg(), they should be local variables (initialized explicitly to 0)nas an argument tosumavg()instead of using a global variablesumavg()as returningint, but don’t return anything. You should probably change the declaration tovoid