So I'm creating a simple program, and I usually use the GNU compiler.
However, this time I chose to use Visual C++ for developing in C.
I've set up my project correctly, changing the settings to make it compile in C. The code is very simple:
#include <stdlib.h>
#include <stdio.h>
int main(){
printf("Hey!");
int x = 9;
printf("%d",x);
return 0;
}
If I compiled this using Code::Blocks IDE and the GNU compiler, it would work, but for some reason it doesn't work in Visual C++. I keep getting these errors:
error C2143: syntax error : missing ';' before 'type'
error C2065: 'x' : undeclared identifier
How can I fix this?
VC++ 2010 only implements C89/C90, not the newer C standards that allow variable declarations after other statements inside of a function body. To fix it, move the declaration of x
to the beginning of main
:
#include <stdlib.h>
#include <stdio.h>
int main() {
int x = 9;
printf("Hey!");
printf("%d",x);
return 0;
}
Change the file extension to .cpp
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With