Tryer
Tryer

Reputation: 4110

Compiler warning for statement on same line as #endif

Consider code:

#include <stdio.h>

int main() {
    int a = 4;
#if 1
    printf("Hello world\n");
#endif a++;
    printf("a is %d\n", a);
}

Inadvertently, statement a++; is on the same line as a #endif and is not evaluated. As a result, the final output is:

Hello world
a is 4

On x86-64 clang 12, this is captured as a warning with using option -Wextra-tokens. See here.

I tried compiling this on Visual Studio 2019 MSVC, with command line options:

/JMC /permissive- /ifcOutput "Debug\" /GS /analyze- /W3 /Zc:wchar_t /I"../include/" /ZI /Gm- /Od /sdl /Fd"Debug\vc142.pdb" /Zc:inline /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_UNICODE" /D "UNICODE" /errorReport:prompt /WX- /Zc:forScope /RTC1 /Gd /Oy- /MDd /FC /Fa"Debug\" /EHsc /nologo /Fo"Debug\" /Fp"Debug\windows.pch" /diagnostics:column 

There is no warning of any sort on compilation. Is there a setting that can be passed to the compiler in MSVC that detects extra tokens?


Edited to add:

As answered by user Nathan Pierson, it was indeed option /Za that worked. It does not seem to be on by default. I was also unable to use the Visual Studio Project Properties dialog to find out the option to set. Yet, one can feed in extra options manually thus:

enter image description here


ETA: Use of /Za is not recommended

Upvotes: 6

Views: 210

Answers (1)

Nathan Pierson
Nathan Pierson

Reputation: 5585

There's compiler warning C4067. It looks like you need to set the flag /Za for it to apply to #endif directives.

In the Visual Studio properties page, this flag is controlled by the setting "Disable Language Extensions" in the Language subsection of the C/C++ section.

Upvotes: 5

Related Questions