tags:

views:

404

answers:

6

I'm used to old-style C and and have just recently started to explore c99 features. I've just one question: Will my program compile successfully if I use c99 in my program, the c99 flag with gcc and link it with prior c99 libraries?

So, should I stick to old C89 or evolve?

Thanks.

+7  A: 

I believe that they are compatible in that respect. That is as long as the stuff that you are compiling against doesn't step on any of the new goodies. For instance, if the old code contains enum bool { false, true }; then you are in trouble. As a similar dinosaur, I am slowly embracing the wonderful new world of C99. After all, it has only been out there lurking for about 10 years now ;)

D.Shawley
Actually, as long as you don't include `<stdbool.h>`, you are free to use the identifier `bool` in C99 code. C99 defines the `_Bool`, `_True`, and `_False` keywords as part of the language, and the header `<stdbool>` aliases these to `bool`, `true`, and `false`.
Adam Rosenfield
Technically, the implicit `int` return type is allowed in C89 but not C99, but if your code still uses those then you have much bigger problems and you should switch to C99 just to clean them up. (Note that there may be a few other C89/C99 problems along these lines, but they all fall under the same category more or less.)
Chris Lutz
+1  A: 

It's intended to be backwards compatible. It formalizes extensions that many vendors have already implemented. It's possible, maybe even probable, that a well written program won't have any issues when compiling with C99.

In my experience, recompiling some modules and not others to save time... wastes a lot of time. Usually there is some easily overlooked detail that needs the new compiler to make it all compatible.

wallyk
+2  A: 

Respectfully: Try it and find out. :-)

Though, keep in mind that even if you need to fix a few minior compiling differences, moving up is probably worth it.

Frank V
+2  A: 

If you don't violate the explicit C99 features,a c90 code will work fine c99 flag with another prior c99 libraries.

But there are some dos based libraries in C89 like ,, that will certainly not work.

C99 is much flexible so feel free to migrate :-)

nthrgeek
+1  A: 

The calling conventions between C libraries hasn't changed in ages, and in fact, I'm not sure it ever has.

Operating systems at this point rely heavily on the C calling conventions since the C APIs tend to be the glue between the pieces of the OS.

So, basically the answer is "Yes, the binaries will be backwards compatible. No, naturally, code using C99 features can't later be compiled with a non-C99 compiler."

scotchi
+4  A: 

You should evolve. Thanks for listening :-)

Actually, I'll expand on that.

You're right that C99 has been around for quite a while. You should (in my opinion) be using that standard for anything other than legacy code (where you just fix bugs rather than add new features). It's probably not worth it for legacy code but you should (as with all business decisions) do your own cost/benefit analysis.

I'm already ensuring my new code is compatible with C1x - while I'm not using any of the new features yet, I try to make sure it won't break.

As to what code to look out for, the authors of the standards take backward compatibility very seriously. Their job was not ever to design a new language, it was to codify existing practices.

The phase they're in at the moment allows them some more latitude in updating the language but they still follow the Hippocratic oath in terms of their output: "first of all, do no harm".

Generally, if your code is broken with a new standard, the compiler is forced to tell you. So simply compiling your code base will be an excellent start. However, if you read the C99 rationale document, you'll see the phrase "quiet change" appear - this is what you need to watch out for.

These are behavioral changes in the compiler that you don't need to be informed about and may be the source of much angst and gnashing of teeth if your application starts acting strange. Don't worry about the "quiet change in c89" bits - if they were a problerm, you would have already been bitten by them.

That document, by the way, is an excellent read to understand why the actual standard says what it says.

paxdiablo
Do you have a pointer to what new stuff is coming in C1x? It looks like the 'safe function' TRs aren't being incorporated (at least they don't seem to be in the March 2009 draft). I'd rather not have to read through the whole draft document to try to spot out the new stuff.
Michael Burr
I'm using n1362 (March draft). Things can be pulled out right up to the time when the standard is officially voted in (though, of course, it gets less likely as the voting draws near). Despite the fact that I think the "safe" functions are a crutch for people who have no business coding in C in the first place :-), I'm still acting as if they'll be there, since WG14 is still working on them as at May '09 (though I notice they're now calling them the more apt "bounds-checking interfaces" - they were *never* safe). I'm not using them, just making sure *my* code isn't broken by them.
paxdiablo
Actually, I don't hate the bounded functions that much since they do reduce effort. I just hated the fact that they were called safe. They're only safe if you use them properly and that's true of *all* C functions.
paxdiablo
In general I like the 'bounds-checking' functions because they force people to think about buffer size issues. I generally call them 'safe' even if it's not entirely accurate out out habit or something. I do think they encourage safer use of output buffers. Anyway, I briefly looked at n1362, but it seems like it would take some serious study to suss out the new stuff, so I was hoping you might have a pointer to what's new (Google didn't seem to help much - at least at first go).
Michael Burr