Compatibility Of C89/C90, C99 And C11 - Stack Overflow

Just browsing Stack Overflow? Help us improve your experience. Sign up for research
    1. Home
    2. Questions
    3. Tags
    4. Users
    5. Companies
    6. Labs
    7. Jobs
    8. Discussions
    9. Collectives
    10. Communities for your favorite technologies. Explore all Collectives

  1. Teams

    Ask questions, find answers and collaborate at work with Stack Overflow for Teams.

    Try Teams for free Explore Teams
  2. Teams
  3. Ask questions, find answers and collaborate at work with Stack Overflow for Teams. Explore Teams

Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

Get early access and see previews of new features.

Learn more about Labs Compatibility of C89/C90, C99 and C11 Ask Question Asked 7 years, 10 months ago Modified 7 years, 10 months ago Viewed 18k times 18

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.

Share Improve this question Follow edited Jan 8, 2017 at 19:00 jxh's user avatar jxh 70.3k8 gold badges113 silver badges198 bronze badges asked Jan 8, 2017 at 18:00 Michael S's user avatar Michael SMichael S 4761 gold badge4 silver badges12 bronze badges 10
  • 3 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 Commented Jan 8, 2017 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 Commented Jan 8, 2017 at 18:07
  • 1 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 Commented Jan 8, 2017 at 18:09
  • 1 (assuming you're responding to me) @user3528438: I said colloquially. Of course ANSI and ISO officially designate the most recent standard as being the official meaning of the C language. – Cornstalks Commented Jan 8, 2017 at 18:31
  • 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 Commented Jan 9, 2017 at 10:16
| Show 5 more comments

4 Answers 4

Sorted by: Reset to default Highest score (default) Trending (recent votes count more) Date modified (newest first) Date created (oldest first) 17

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.

Share Improve this answer Follow answered Jan 9, 2017 at 10:02 Lundin's user avatar LundinLundin 212k44 gold badges270 silver badges426 bronze badges 3
  • 1 Thanks for your help. I got another question: You said that it was better to use C11. Then I read this: stackoverflow.com/questions/20600497/… Why does the Linux kernel use the GNU dialect of C89? – Michael S Commented Jan 9, 2017 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 Commented Jan 9, 2017 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 Commented Jan 13, 2017 at 11:45
Add a comment | 2

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.

Share Improve this answer Follow answered Jan 9, 2017 at 21:50 supercat's user avatar supercatsupercat 80.7k9 gold badges174 silver badges220 bronze badges Add a comment | 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.

Share Improve this answer Follow answered Jan 8, 2017 at 19:00 jxh's user avatar jxhjxh 70.3k8 gold badges113 silver badges198 bronze badges Add a comment | 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.

Share Improve this answer Follow edited Jan 11, 2017 at 6:54 answered Jan 8, 2017 at 18:03 Paul Stelian's user avatar Paul StelianPaul Stelian 1,37910 silver badges27 bronze badges 1
  • 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 Commented Jan 10, 2017 at 21:35
Add a comment |

Your Answer

Reminder: Answers generated by artificial intelligence tools are not allowed on Stack Overflow. Learn more

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid …

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.

Draft saved Draft discarded

Sign up or log in

Sign up using Google Sign up using Email and Password Submit

Post as a guest

Name Email

Required, but never shown

Post Your Answer Discard

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.

  • Featured on Meta
  • We’re (finally!) going to the cloud!
  • More network sites to see advertising test [updated with phase 2]

Linked

178 What is the difference between C, C99, ANSI C and GNU C? 50 Which C version is used in the Linux kernel? 0 What is the behavior when a new C programming language standard is official, the old standard is ALWAYS compatible? 47 How universally is C99 supported? 17 Is C99 backward compatible with C89? 5 Why do new C books not adhere to the C99 standard? 36 Latest changes in C11 3 how to understand C99 standard syntax 3 C90 compound literals 15 GCC options for strict C90 code? 2 What hardware specific defines does the c89 standard require the implementation to provide? 1 How does gcc's -Wc90-c99-compat flag works? 2 Are C versions backwards compatible?

Hot Network Questions

  • Can you see through a cannonball packing?
  • Why do I have to reboot to activate a kernel upgrade even though live kernel updates are enabled?
  • Non-reflexive use of laisser without a direct object in « The Stranger » ?
  • How to Prove This Elegant Integral Identity Involving Trigonometric and Square Root Terms
  • When my modem places a signal on coax, is that signal still considered Ethernet?
  • Why is the chi-square test giving unintuitive results?
  • Looking for short story about detectives investigating a murder in the future
  • What are some ways I can get an LM5176 buck-boost converter to operate down to 2.5V input voltage without a separate power supply?
  • Is there any reported instance of a member of the U.S. military disobeying an order on the grounds that it was unlawful?
  • Who are Catalina and Teresa in Gabriela Mistral's poem Vieja?
  • A fantasy movie with two races, "Big Ones" (=us) and smaller ones, about saving a newborn baby from a cruel queen
  • Why does water vapor sometimes start to twirl above a pot of boiling water?
  • Conditional Definition of a Mathematical Operator
  • Implicit function theorem without manifolds (Steve Smale article)?
  • Why is Ukraine's conscription age (still) so high (25)?
  • How would the Aboriginal Australians interact with and utilize a Sapient Octopus Species?
  • Use of “12 m.” for noon and “12 p.m.” for midnight
  • When and how were nets and filters first shown to be equivalent?
  • A roulette wheel? An AC Milan kit? Darth Maul's face?
  • Hearing the cry of a baby - abandoning practice for action?
  • How do you write a page-centered abstract in a two column document
  • Why does Japanese not have a native "tu" sound?
  • Is the Heter for music on Shabbat universal
  • Why did Satan take Jesus to the Temple to jump down from?
more hot questions Question feed Subscribe to RSS Question feed

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

lang-c

Từ khóa » C99 Vs C11 Vs C90