简体   繁体   中英

Microsoft C/C++ compiler switch needed for local variable declaration placement

I'm compiling some C files using Microsoft C/C++ and it's complaining about declaring local variables inside a block. Declaring them at the beginning of a block, of course, is fine. What compiler switch can I use to suppress the errors that I'm getting?

Much appreciated,

kris

In C89 and earlier, all block-scope variable declarations must come before any statements. C99 changed this rule, so that declarations and statements may be intermixed as in C++.

Unfortunately, Microsoft has chosen not to support C99 in Visual Studio and has no plans to AFAIK.

In standard C, you can't declare variables anywhere but at the beginning. This is different from C++ where variables can be declared anywhere.

So you must compile the files as if they were C++ via /TP .

See this article for more details.

CL.exe /Tpfilename

It means "compile as C++ regardless of the extension".

Or, to put it another way, what you're doing is illegal in C, so of course it complains. Either change it to C++ or force it to be read as C++.

You can use /TP to force the compiler to compile them as C++ files. I'm not sure if this is what you want, however.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM