4

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?

user2527666
  • 433
  • 5
  • 14
  • 1
    The first program fully (except for the `void main` bit) conforms to the current ANSI C standard which is the same as the current ISO c standard, commonly called C11. VS2015 doesn't support it. It only supports one *former* standard, C99, to which this program also conforms. If you want conformance to some other former standard like C89/90, you need to downgrade your toolset to something like v100 (VS2010), or use another compiler. There's nothing like `-std` flag in MSVC. – n. m. could be an AI Feb 04 '17 at 17:33

1 Answers1

4

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

Govind Parmar
  • 20,656
  • 7
  • 53
  • 85