12

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.

eang
  • 1,615
  • 7
  • 21
  • 41
  • 1
    How 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 Nov 09 '12 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 Nov 09 '12 at 13:35
  • 4
    @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 Nov 09 '12 at 13:36
  • @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 Nov 09 '12 at 13:36
  • @Lundin Indeed, see my comment on DjSols post. – Benj Nov 09 '12 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 Nov 09 '12 at 13:42
  • I 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. – eang Nov 09 '12 at 13:43
  • 2
    Visual Studio 2013 now supports mixed declarations and code. – Étienne May 14 '14 at 12:55

3 Answers3

13

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,...)
Christoph
  • 164,997
  • 36
  • 182
  • 240
9

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:

http://blogs.msdn.com/b/vcblog/archive/2013/06/28/c-11-14-stl-features-fixes-and-breaking-changes-in-vs-2013.aspx

HYildiz
  • 224
  • 3
  • 2
-5

Build your app using the C++ compiler. This is the easiest way. You can still write C code just name the file *.cpp

DjSol
  • 208
  • 1
  • 11
  • 2
    The 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. – Benj Nov 09 '12 at 13:32
  • I would love to see some C code that does not compile under C++ – DjSol Nov 09 '12 at 13:56
  • 3
    @DjSol the classic is malloc casting... char* foo = malloc(1); vs. char* foo = reinterpret_cast< char* >( malloc(1) ); – jheriko Nov 09 '12 at 13:57
  • @DjSol `int *x = malloc(sizeof(int) * 10);` doesn't compile in C++ – Benj Nov 09 '12 at 13:58
  • (the implication of 'the classic' is that there is a well known set of problems like this) – jheriko Nov 09 '12 at 13:58
  • @Benj int `*x = (int*)malloc(sizeof(int) * 10);` compiles just fine under C and C++ – DjSol Nov 09 '12 at 14:03
  • @DjSol Yes, the point is that C++ requires a cast because `void*` behaves differently. – Benj Nov 09 '12 at 14:03
  • @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. – DjSol Nov 09 '12 at 14:05
  • 4
    @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) – Christoph Nov 09 '12 at 14:05
  • 3
    Apart 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. – dpi Nov 09 '12 at 15:14
  • @Daniel: what kind of compiler diagnostics would you loose ? – Jabberwocky Mar 31 '14 at 11:31