问题
I've recently enabled -pedantic
option on gcc and now I've got about two or three pages of "ISO C90 forbids mixed declaration and code" warnings.
My goal with this project is to be able to deploy it on any mainstream system with a c compiler, so I realize it wouldn't be wise to assume that C99 will be supported everywhere, but is it even worth my time to address these warnings?
Are there still systems out there with c compilers that don't support mixed declaration and code?
回答1:
Well, was/is it your intent to write your code in C89/90 or in C99?
Since this is the only warning you seem to be concerned about, apparently your code was actually written in C89/90. If it is indeed so and you plan to stick with C89/90, then I'd stick to strict C89/90 and move all declarations to the beginning of the block.
If, on the other hand, you are willing and planning to switch to C99, then "misplaced" declarations are no longer an issue. Then your main concern becomes the platform/compiler support. MS Visual Studio C compilers are C89/90 compilers. Is this a problem? (Considering that GCC is available on Windows platforms).
回答2:
If you want to be -pedantic
about the C99 standard add the option -std=c99
.
Personally I like the older unmixed style because it makes it easier to audit visually what types of memory the function is using and decide what might need free()-ing before returning.
回答3:
The Visual Studio C-compiler does not allow mixing declarations and code. Microsoft will probably never add full C99 support to Visual Studio; C is not important enough in the Windows world.
回答4:
I'm not sure I would change it.
However, if you do decide you want to, it's pretty easy (so maybe you should); Just have all your declarations right after beginning a code block. If you absolutly must define them later, use another nested code block.
回答5:
IIRC, Visual C++ doesn't allow mixed declarations and code in C mode.
Visual C++ has been lagging behind in C99 support in general. The situation may have improved recently however, and it may be possible to compile your code in C++ mode anyway.
回答6:
I would eliminate that particular warning as intermixing code and declarations is not a widely supported feature.
Actually I have the same problem with a library I'm writing. I'm using variadic macros but those seem to be more supported.
To ensure my library will be really usable by others, I'm going to test it with as much compilers I can. I've already done it with Visual C++ Express and Pelles C and I'll try some other ( OpenWatcom, Mars C and lcc-Win32 at least).
Unfortunately SourceForge no longer provides a compilers farm as it used to do some years ago and I can't test it on commercial Unix compilers.
来源:https://stackoverflow.com/questions/1975866/mixed-declaration-and-code-warning-is-it-worth-addressing