17

I have code that compiles on the GNUARM compiler, but Visual Studio 2010 issues errors. The issue involves declaring variables after the first statement in a C language file:

main.c

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
  int i = 6;
  i = i + 1;
  printf("Value of i is: %d\n", i);
  int j = i * 10; // <-- This is what Visual Studio 2010 complains about.
  printf("Value of j is: %d\n", j);
  return EXIT_SUCCESS;
}

The following code compiles without errors:

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
  int i = 6;
  int j;      // <-- Declaration is now here, valid according to K&R rules.
  i = i + 1;
  printf("Value of i is: %d\n", i);
  j = i * 10; // <-- Moved declaration of j to above.
  printf("Value of j is: %d\n", j);
  return EXIT_SUCCESS;
}

I'm using default settings for creating a Win32 console project. When I set "Compile as" property to "Compile as C++ (/TP)", I get compilation errors in some Visual Studio header files. (Right click on project, choose PropertiesConfiguration PropertiesC/C++Advanced).

How do I tell Visual Studio 2010 to allow variable declarations after the first statement, like C++ or the current C language standard?

Community
  • 1
  • 1
Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154
  • When you compile with `/TP`, what are the "compilation errors in some Visual Studio header files"? I get no errors when compiling your example with `/TP`. – James McNellis Sep 30 '11 at 18:23
  • 2
    Embarrassingly, you can't. http://stackoverflow.com/questions/146381/visual-studio-support-for-new-c-c-standards – James Greenhalgh Sep 30 '11 at 18:25
  • I rebuilt the {embedded} code as C++ `(/TP)` and also as C `(/TC)`. No errors in the Visual Studio header files. Weird. – Thomas Matthews Sep 30 '11 at 18:35

3 Answers3

9

You don't. Visual C++ does not support C99.

You'll need to compile as C++ (and update your code accordingly) or follow the rules of C89.

(I don't know what errors you get when compiling with /TP; I can compile your example successfully with /TP if I add #include <stdlib.h> for EXIT_SUCCESS; if you provide more details, I or someone else may be able to help.)

James McNellis
  • 348,265
  • 75
  • 913
  • 977
  • Perhaps this answer should be updated (see [HYildiz's answer](http://stackoverflow.com/questions/7614397/how-can-i-tell-visual-studio-microsofts-c-compiler-to-allow-variable-declaratio/25318845#25318845))? – Peter Mortensen May 16 '17 at 02:05
6

As of Visual Studio 2013, the Visual C++ compiler supports C99 style variable declarations. More details can be found in:

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
HYildiz
  • 224
  • 3
  • 2
  • I'm stuck with VS2010 at the shop and presently, they can't justify upgrading the tools to VS2013. I want them to upgrade because MS finally became C99 compatible in VS2013. All our other compilers are compliant. Sharing files between the compilers is frustrating. – Thomas Matthews Aug 14 '14 at 23:52
  • The link seems to be (effectively) broken. – Peter Mortensen May 15 '17 at 21:53
  • I couldn't edit the answer as the suggested edit queue is full. The blog post with the statement about C99 style variable declarations now can be found here: https://devblogs.microsoft.com/cppblog/c1114-stl-features-fixes-and-breaking-changes-in-vs-2013/#:~:text=C99%20variable%20declarations However it is unclear how that feature can be enabled. Without special options the VS 2013 compiler complains about C99 variable declarations. – stm Oct 28 '22 at 11:14
2

I made the same test with the default Visual Studio 2010 project, with the C file and /TP switch, and got the precompiled headers error. It can be removed by renaming stdafx.cpp to stdafx.c, or disabling precompiled headers for the whole project or for specific C files.

I didn't find any other problems. However, this effectively converts C language to C++, which is not your intention, I think. C allows, however, to define a variable in the beginning of every {} block.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Alex F
  • 42,307
  • 41
  • 144
  • 212
  • Yes, the C language allows add {} blocks to declare temporary variables. However, my legacy embedded C code uses the C99 language standard. – Thomas Matthews Sep 30 '11 at 18:38