views:

547

answers:

6

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  A: 

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.

rmn
+1  A: 

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.

ZoogieZork
+4  A: 

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

AndreyT
I'm not aiming to conform to any specific standard, I just want to be confident that my code will compile on any mainstream c compiler
Graphics Noob
In that case the answer is: no, it won't compile on any mainsream C compiler. MS VIsual Studio compiler adheres to C89/90 and it will not compile such code.
AndreyT
+1  A: 

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.

JesperE
+1  A: 

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.

mcl
A: 

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.

Remo.D