17

I just read: C Wikipedia entry. As far as I know there are 3 different versions of C that are widely used: C89, C99 and C11. My question concerns the compatibility of source code of different versions. Suppose I am going to write a program (in C11 since it is the latest version) and import a library written in C89. Are these two versions going to work together properly when compiling all files according to the C11 specification?

Question 1: Are the newer versions of C i.e. C99, C11 supersets of older C versions? By superset I mean, that old code will compile without errors and the same meaning when compiled according to newer C specifications.

I just read, that the // has different meanings in C89 and C99. Apart from this feature, are C99 and C11 supersets of C89?

If the answer to Question 1 is no, then I got another 2 questions.

  1. How to 'port' old code to the new versions? Is there a document which explains this procedure?

  2. Is it better to use C89 or C99 or C11?

Thanks for your help in advance.

EDIT: changed ISO C to C89.

jxh
  • 69,070
  • 8
  • 110
  • 193
Michael S
  • 466
  • 1
  • 4
  • 12
  • 2
    You keep saying ISO C, but I think you mean ANSI C (or C89 or C90; all three of those names refer to the same language). ISO C by itself doesn't really mean anything (and it's important to note that both C99 and C11 are defined by ISO language bodies). – Cornstalks Jan 08 '17 at 18:04
  • 1
    @Cornstalks Technically every ISO C standard supersedes its predecessors and ANSI adopts the ISO C language standards, so "ANSI C" would mean C11, too. (But most people don't use it like that.) – melpomene Jan 08 '17 at 18:07
  • 1
    https://github.com/mauke/poly.poly/blob/master/poly.poly can distinguish between C89 and C99 (by compiling with different semantics in each), so the answer to Q1 is "no". – melpomene Jan 08 '17 at 18:09
  • @melpomene: You're totally right, but colloquially "ANSI C" means C89. I personally dislike the term ANSI C (precisely for the reason you describe), and wish people would just call it C89 (or C90... I'm fine with that name too). – Cornstalks Jan 08 '17 at 18:11
  • thanks, I am going to change ISO C to C89. – Michael S Jan 08 '17 at 18:12
  • No, ANSI won't agree. I believe if you email ANSI they would say currently ANSI C is ISO-9899:2011. – user3528438 Jan 08 '17 at 18:17
  • 1
    (assuming you're responding to me) @user3528438: [I said *colloquially*](https://www.google.com/search?q=define+colloquially). Of course ANSI and ISO officially designate the most recent standard as being the official meaning of the C language. – Cornstalks Jan 08 '17 at 18:31
  • Regarding "ANSI C" see [What is the difference between C, C99, ANSI C and GNU C? A general confusion regarding the various versions of C](http://stackoverflow.com/questions/17206568/what-is-the-difference-between-c-c99-ansi-c-and-gnu-c-a-general-confusion-reg). – Lundin Jan 09 '17 at 10:06
  • And no, there is no such thing as ANSI/ISO C any longer. In USA, and only there, the current standard is named `INCITS/ISO/IEC 9899-2012`. This is irrelevant knowledge to anyone living outside USA. For example in Sweden it called `SS-ISO 9899`. Nobody cares about the national standard institute designation. Simply refer to the standard as `ISO/IEC 9899:2012`. – Lundin Jan 09 '17 at 10:08
  • 1
    As for what "ISO C" means, it means the current version, C11. C99 and C90 are withdrawn by ISO and shouldn't be used, as far as ISO is concerned. – Lundin Jan 09 '17 at 10:16

4 Answers4

16

Are the newer versions of C i.e. C99, C11 supersets of older C versions?

There are many differences, big and subtle both. Most changes were adding of new features and libraries. C99 and C11 are not supersets of C90, although there was a great deal of effort made to ensure backwards-compatibility. C11 is however mostly a superset of C99.

Still older code could break when porting from C90, in case the code was written poorly. Particularly various forms of "implicit int" and implicit function declarations were banned from the language with C99. C11 banned the gets function.

A complete list of changes can be found in the C11 draft page 13 of the pdf, where "the third edition" refers to C11 and "the second edition" refers to C99.

How to 'port' old code to the new versions? Is there a document which explains this procedure?

I'm not aware about any such document. If you have good code, porting is easy. If you have rotten code, porting will be painful. As for the actual porting procedure, it will be easy if you know the basics of C99 and C11, so the best bet is to find a reliable source of learning which addresses C99/C11.

Porting from C99 to C11 should be effortless.

Is it better to use C89 or C99 or C11?

It is best to use C11 as that is the current standard. C99 and C11 both contained various "language bug fixes" and introduced new, useful features.

Lundin
  • 195,001
  • 40
  • 254
  • 396
  • 1
    Thanks for your help. I got another question: You said that it was better to use C11. Then I read this: http://stackoverflow.com/questions/20600497/which-c-version-is-used-in-the-linux-kernel#20664429 Why does the Linux kernel use the GNU dialect of C89? – Michael S Jan 09 '17 at 16:30
  • 3
    @MichaelS I would assume that would be because Linux was made in the late 90s when neither C99 nor C11 was around. That being said, the Linux kernel is an incredibly messy code base, filled to the brim with non-standard gcc extensions, to the point where that code is forever stuck with gcc. Why they do things in a certain way, I have no idea, but I would look elsewhere for inspiration, as the Linux kernel code base is about as far as canon as you get. Similarly, the Linux kernel code style guide is not a professional coding standard. – Lundin Jan 09 '17 at 21:27
  • Thank you for your answer and help! I am sorry but I can't upvote your comment and answer since I haven't got enough reputation. – Michael S Jan 13 '17 at 11:45
1

In most ways, the later versions are supersets of earlier versions. While C89 code which tries to use restrict as an identifier will be broken by C99's addition of a reserved word with the same spelling, and while there are some situations in which code which is contrived to exploit some corner cases with a parser will be treated differently in the two languages, most of those are unlikely to be important.

A more important issue, however, has to do with memory aliasing. C89 include rules which restrict the types of pointers that can be used to access certain objects. Since the rules would have made functions like malloc() useless if they applied, as written, to the objects created thereby, most programmers and compiler writers alike treated the rules as applying only in limited cases (I doubt C89 would have been widely accepted if people didn't believe the rules applied only narrowly). C99 claimed to "clarify" the rules, but its new rules are much more expansive in effect than contemporaneous interpretations of the old ones, breaking a lot of code that would have had defined behavior under those common interpretations of C89, and even some code which would have been unambiguously defined under C89 has no practical C99 equivalent.

In C89, for example, memcpy could be used to copy the bit pattern associated with an object of any type to an object of any other type with the same size, in any cases where that bit pattern would represent a valid value in the destination type. C99 added language which allows compilers to behave in arbitrary fashion if memcpy is used to copy an object of some type T to storage with no declared type (e.g. storage returned from malloc), and that storage is then read as object of a type that isn't alias-compatible with T--even if the bit pattern of the original object would have a valid meaning in the new type. Further, the rules that apply to memcpy also apply in cases where an object is copied as an array of character type--without clarifying exactly what that means--so it's not clear exactly what code would need to do to achieve behavior matching the C89 memcpy.

On many compilers such issues can be resolved by adding a -fno-strict-aliasing option to the command line. Note that specifying C89 mode may not be sufficient, since compilers writers often use the same memory semantics regardless of which standard they're supposed to be implementing.

supercat
  • 77,689
  • 9
  • 166
  • 211
0

In general, newer versions of the standard are backward compatible.

If not, you can compile different .c files to different .o files, using different standards, and link them together. That does work.

Generally you should use the newest standard available for new code and, if it's easy, fix code that the new standard does break instead of using the hacky solution above.

EDIT: Unless you're dealing with potentially undefined behavior.

Paul Stelian
  • 1,381
  • 9
  • 27
  • 1
    There are some semantic changes in the standards which cause some constructs that used have defined behavior (e.g. certain uses of `memcpy` or `memmove` for type punning in cases where the destination has no declared type, but will be read using a type with no trap representations) to instead invoke Undefined Behavior. Specification of different standard versions when compiling may not suffice to achieve correct behavior, however, if compilers pretend that such behaviors were never defined despite the lack of anything in the older standards to justify such belief. – supercat Jan 10 '17 at 21:35
0

The newer versions of C are definitely not strict super-sets of the older versions.

Generally speaking, this sort of problem only arises when upgrading the compiler or switching compiler vendors. You must plan for a lot of minor touches of the code to deal with this event. A lot of the time, the new compiler will catch issues that were left undiagnosed by the old compiler, in addition to minor incompatibilities that may have occurred by enforcing the newer C standard.

If it is possible to determine, the best standard to use is the one that the compiler supports the best.

The C11 wikipedia article has a lengthy description of how it differs from C99.

jxh
  • 69,070
  • 8
  • 110
  • 193