Sunday, February 19, 2012

C++'11: C99 compatibility

When I was first learning how to program, I stumbled across the now defunct website programing.com [sic] where someone posed the question "What is the difference between C and C++?" The first response to the question said that "The difference is in C you say printf, but in C++ you say cout." I thought it was so funny that I showed all my software friends.

C compatibility has been one of the greatest advantages of the C++ language. Without it the language would have been (as Bjarne put it in D&E) stillborn. It has also been one of the biggest sources of problems in the language. I frequently deal with these kind of issues when integrating COTS C code with C++. The ISO C standard was updated for the first time in 11 years in 1999, a year after C++'98 was released. It was updated again in 2011 around the same time as C++'11. The two committees coordinated many of their changes to continue their symbiosis.

For an interesting discussion on incompatibility between the languages see appendix C of the standards document. The appendix is informative and not part of the actual C++ language definition but it is still worth your time if you work much with C and C++ together.

long long
long long was officially added to define an integer that is at least 64 bits long. Along with that comes unsigned long long and the literal constants LL/ll and ULL/ull. printf also has the ll length specifier. long long has been the de facto standard for awhile so it will probably be a surprise to some that it was not officially in the language before now. In was actually proposed back in 1995 but at the time was rejected because C had not yet officially adopted it yet. Here is a little code example:
 long long myNumber = -1234567LL;
unsigned long long uMyNumber = 8446744073709551616ULL; //That is a large number
printf("%lld and %llu\n", myNumber, uMyNumber);
See for [n1811] for more information.

Extended Integer Types
The C++ language did not adopt actual Extended Integer types. i.e. 128-bit integers or larger. They did however define a way for them to behave is an implementation decided to provide them. In a nutshell, they will behave basically how you would expect them to when it comes to things like implicit conversions. For the most part a normal developer should not need to worry about it or even know about this language extension because any use of it would not be portable. It should be avoided unless there is a good reason to use it. The C language, as well as the ECMA C++ Binding for CLI standard (that is the European Computer Manufactures Association) have already adopted it, so for the sake of not creating a C++ dialect the committee picked it up too. See [1988] for more information.

Other Stuff...
A bunch of other misc. things were carried over. Maybe the biggest thing was updates to the C libraries, a quick scan of [n1568] will show you more detail than you probably care to know about some of the compatibility library updates. I am choosing to gloss over changes like the updates to the preprocessor and C style strings in hopes that they will be touched on in another post. One important difference to point out though, is C++ did not adopt the Variable Length Arrays. Sorry, I guess you will just have to continue using new() for a variable length - or better yet use the new array class in the Standard Template Library.


No comments:

Post a Comment