Enforce ANSI C Standard in Visual Studio 2015

主宰稳场 提交于 2019-12-13 14:17:21

问题


I am trying to get Visual Studio to enforce the ANSI C standard when compiling a project, but I can't get it to work. Any tips? I have read all the tutorials, I enabled the /Za option, and named my file as .c (not .cpp). However, the following program still builds successfully:

#include <stdio.h>
void main(void)
{
    for (int i = 0; i < 10; i++)
    {
    }
    int f = 0;
}

But it shouldn't. It would have to be like this to respect the ANSI C standard:

#include <stdio.h>
void main(void)
{
    int i;
    int f = 0;
    for (i = 0; i < 10; i++)
    {
    }
}

I would like the equivalent of the GCC options "-ansi" and "-Wpedantic". Is this even possible in VS?


回答1:


From this page, MSVC 2015 seems to only support C99:

C99 Conformance Visual Studio 2015 fully implements the C99 Standard Library, with the exception of any library features that depend on compiler features not yet supported by the Visual C++ compiler (for example, <tgmath.h> is not implemented).

There is no mention of C89 compatibility anywhere on that page.

The /Za switch only disables Microsoft specific extensions:

The Visual C++ compiler offers a number of features beyond those specified in either the ANSI C89, ISO C99, or ISO C++ standards. These features are known collectively as Microsoft extensions to C and C++. These extensions are available by default, and not available when the /Za option is specified. For more information about specific extensions, see Microsoft Extensions to C and C++.

It will not disable non-Microsoft specific extensions if they are part of an official C standard that it supports (like C99).



来源:https://stackoverflow.com/questions/42043078/enforce-ansi-c-standard-in-visual-studio-2015

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