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.

asked Nov 9, 2012 at 13:25
8
  • 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. Commented 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? Commented 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. Commented 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. Commented Nov 9, 2012 at 13:42
  • 2
    Visual Studio 2013 now supports mixed declarations and code. Commented May 14, 2014 at 12:55

3 Answers 3

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,...)
answered Nov 9, 2012 at 13:43
Sign up to request clarification or add additional context in comments.

2 Comments

I can't switch to C++, so using MinGW as compiler and VS as IDE seems a good solution. I'll try this one.
Note that this answer is no longer correct as of MSVC2013.
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

answered Aug 14, 2014 at 22:47

Comments

-5

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

answered Nov 9, 2012 at 13:31

11 Comments

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.
I would love to see some C code that does not compile under C++
@DjSol the classic is malloc casting... char* foo = malloc(1); vs. char* foo = reinterpret_cast< char* >( malloc(1) );
@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)
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.
|

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.