How to "simulate" C99 in Visual Studio for variables declaration
15,189
Solution 1
The choices I see:
- stick with MSVC and switch to C++
- stick with MSVC and use a precompiler that translates C99 to C90 (Comeau, c99-to-c89)
- switch to a toolchain that supports more recent revisions of the C language (Intel, MinGW, Clang, Pelles-C,...)
Solution 2
This seems like a dated thread, but having landed here first while I was searching for the same question I thought I should post an update:
As of VS13, the Visual C++ compiler supports C99 style variable declarations. More details here:
Author by
eang
Updated on June 03, 2022Comments
-
eang almost 2 years
I'm using Visual Studio 2012 to develop simple Win32 C programs. I know that the VS compiler only supports C89, but I'd like to know if there is a way to override this limitation.
In particular I'd like to declare variables anywhere in my code, instead of only at the beginning of scope blocks (as C89 requires).
Thanks in advance.
-
Benj over 11 yearsHow much do you care that the MSVC compiler is in C mode? If you set it to C++ mode you can still write C and you can use C99 style variable initialization.
-
eang over 11 yearsUsually I create a simple "Visual C++ Empty Project", then I add a .c source file to it. Do you mean that I should simply add .cpp files instead of .c?
-
Lundin over 11 years@Benj It is a bad idea to compile C programs in C++, there are many subtle differences: struct implementation, implicit pointer casts (for example the return value from malloc), different bool implementations, different NULL implementations and so on.
-
Benj over 11 years@ital Adding a
.c
file defaults the compiler to C++ (although you can override it) Adding a.cpp
file will default it to C++ mode. -
Benj over 11 years@Lundin Indeed, see my comment on DjSols post.
-
Lundin over 11 years@ital "I'm using Visual Studio 2012 to develop simple Win32 C programs". Simple as in no GUI, or maybe just raw Windows API? In that case the best solution might be to just use Visual Studio as IDE and compile the programs using a real C compiler like Mingw.
-
eang over 11 yearsI don't know if using C++ compiler could be a problem for my needings. I simply need to learn Win32 API for C programs, actually I'm using Visual Studio only for the integrated MSDN documentation.
-
Étienne about 10 yearsVisual Studio 2013 now supports mixed declarations and code.
-
-
Benj over 11 yearsThe reason I put this as a comment a while back and not as an answer is that a C compiler isn't the same as a C++ compiler. It's certainly possible to write C which won't compile as C++ and vice versa.
-
DjSol over 11 yearsI would love to see some C code that does not compile under C++
-
jheriko over 11 years@DjSol the classic is malloc casting... char* foo = malloc(1); vs. char* foo = reinterpret_cast< char* >( malloc(1) );
-
Benj over 11 years@DjSol
int *x = malloc(sizeof(int) * 10);
doesn't compile in C++ -
jheriko over 11 years(the implication of 'the classic' is that there is a well known set of problems like this)
-
eang over 11 yearsI can't switch to C++, so using MinGW as compiler and VS as IDE seems a good solution. I'll try this one.
-
DjSol over 11 years@Benj int
*x = (int*)malloc(sizeof(int) * 10);
compiles just fine under C and C++ -
Benj over 11 years@DjSol Yes, the point is that C++ requires a cast because
void*
behaves differently. -
DjSol over 11 years@Benj and my point is that C accepts the same cast and I often prefer explicit casting because everyone can see that a cast is being done.
-
Christoph over 11 years@Benj: another example would be the lack of support for designated initializers in C++, which sometimes cannot be worked around without an
#ifdef __cplusplus
(eg static initialization of unions) -
dpi over 11 yearsApart from the incompatibilities between C and C++, the problem with using a C++ compiler for C development is that you lose all helpful compiler diagnostics. Unless you have a very good knowledge of C and the subtle differences between C and C++, you may end up with invalid C code. A C++ compiler may be useful for getting some existing c code to run (with some changes), but it is a bad idea to use it for developing new code.
-
Jabberwocky about 10 years@Daniel: what kind of compiler diagnostics would you loose ?
-
ideasman42 over 8 yearsNote that this answer is no longer correct as of MSVC2013.