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:

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

Share:
15,189
eang
Author by

eang

Updated on June 03, 2022

Comments

  • eang
    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
      Benj over 11 years
      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.
    • eang
      eang over 11 years
      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?
    • Lundin
      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
      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
      Benj over 11 years
      @Lundin Indeed, see my comment on DjSols post.
    • Lundin
      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
      eang over 11 years
      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.
    • Étienne
      Étienne about 10 years
      Visual Studio 2013 now supports mixed declarations and code.
  • Benj
    Benj over 11 years
    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.
  • DjSol
    DjSol over 11 years
    I would love to see some C code that does not compile under C++
  • jheriko
    jheriko over 11 years
    @DjSol the classic is malloc casting... char* foo = malloc(1); vs. char* foo = reinterpret_cast< char* >( malloc(1) );
  • Benj
    Benj over 11 years
    @DjSol int *x = malloc(sizeof(int) * 10); doesn't compile in C++
  • jheriko
    jheriko over 11 years
    (the implication of 'the classic' is that there is a well known set of problems like this)
  • eang
    eang over 11 years
    I can't switch to C++, so using MinGW as compiler and VS as IDE seems a good solution. I'll try this one.
  • DjSol
    DjSol over 11 years
    @Benj int *x = (int*)malloc(sizeof(int) * 10); compiles just fine under C and C++
  • Benj
    Benj over 11 years
    @DjSol Yes, the point is that C++ requires a cast because void* behaves differently.
  • DjSol
    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
    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
    dpi over 11 years
    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.
  • Jabberwocky
    Jabberwocky about 10 years
    @Daniel: what kind of compiler diagnostics would you loose ?
  • ideasman42
    ideasman42 over 8 years
    Note that this answer is no longer correct as of MSVC2013.