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.
-
1How 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.Benj– Benj2012年11月09日 13:28:35 +00:00Commented Nov 9, 2012 at 13:28
-
Usually 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?eang– eang2012年11月09日 13:35:33 +00:00Commented Nov 9, 2012 at 13:35
-
5@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.Lundin– Lundin2012年11月09日 13:36:17 +00:00Commented Nov 9, 2012 at 13:36
-
1@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.Lundin– Lundin2012年11月09日 13:42:37 +00:00Commented Nov 9, 2012 at 13:42
-
2Visual Studio 2013 now supports mixed declarations and code.Étienne– Étienne2014年05月14日 12:55:12 +00:00Commented May 14, 2014 at 12:55
3 Answers 3
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,...)
2 Comments
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:
Comments
Build your app using the C++ compiler. This is the easiest way. You can still write C code just name the file *.cpp
11 Comments
#ifdef __cplusplus (eg static initialization of unions)