How can I tell Visual Studio/Microsoft's C compiler to allow variable declarations after the first statement?

二次信任 提交于 2019-12-01 15:05:46

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.)

HYildiz

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

Alex F

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!