0

I have the following code snippet. This is a c file in visual studio 2010. If i try to compile this with the line: int hello = 10; commented out it will compile just fine. If I comment that line in it will not compile. Am I missing something or should I not be using Visual Studio 2010 to compile C code. If this is a Visual Studio problem can anyone recommend a easy to use IDE / Compiler that I can for C.

Thank You

int* x = (int*) calloc(1, sizeof(int));

*x = 5;

//int hello = 10;

printf("Hello World!  %i", *x);

getchar();
Corsen2000
  • 11
  • 1
  • 3

3 Answers3

17

You can't have declarations (like int hello = 10;) after non-declarations (like *x = 5;) in C89, unlike C99 or C++.

MSVC 2010 still does not support C99.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • 5
    I guess 11 years isn't quite enough time for a multi-billion dollar corporation to implement a standard. Seriously, what could be the reason for the lack of support? – Mark Rushakoff Jun 06 '10 at 22:29
1

you can still declare variables after coding. just change the "yourProject.c" file to "yourProject.cpp" and it will work fine.

user1
  • 61
  • 1
  • 7
0

typecasting a malloc return pointer in C is a bad practice and has undefined results.

Jay D
  • 3,263
  • 4
  • 32
  • 48