Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Undeclared Identifier C Programming in Visual C++

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?

like image 587
turnt Avatar asked Dec 21 '22 10:12

turnt


2 Answers

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;
}
like image 72
ildjarn Avatar answered Dec 24 '22 01:12

ildjarn


Change the file extension to .cpp

like image 36
Ricardo Ortega Magaña Avatar answered Dec 24 '22 00:12

Ricardo Ortega Magaña