Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading