Dif­fer­ent C Stan­dards: The Story of C

This ar­ti­cle cov­ers the var­i­ous stan­dards of the C lan­guage, from K&R C through ANSI C, C99, C11 and Em­bed­ded C.

OpenSource For You - - Contents - By: Deepu Benson The au­thor cur­rently works as an as­sis­tant pro­fes­sor in Amal Jyothi Col­lege of En­gi­neer­ing, Kan­ji­rap­pally. He main­tains a tech­ni­cal blog at www.com­put­ing­for­be­gin­ners.blogspot.in and can be reached at deep­[email protected]­mail.com.

Ihave al­ways wanted to write about the dif­fer­ent stan­dards of C but re­frained from do­ing so for two rea­sons. The first is that though as an aca­demi­cian I re­spect and am de­voted to C, I thought the in­dus­try hardly cared about it. The other more com­pelling rea­son is that not all the dif­fer­ent stan­dards of C are taken se­ri­ously. So, if no­body cares, read­ers may won­der why am I both­er­ing to write this ar­ti­cle.

Two in­ci­dents made me change my mind. To write an ar­ti­cle about pop­u­lar pro­gram­ming lan­guages in OSFY, I did a de­tailed study of pro­gram­ming lan­guages and that in­cluded C. I found out that C is still very pop­u­lar in all the rank­ings, with sup­port­ers in both the aca­demic world and the in­dus­try. The other rea­son is that in a tech­ni­cal in­ter­view I had a few months ago, I faced a ques­tion based on the C11 stan­dard of C. Even though I ab­so­lutely fum­bled with that ques­tion, I was quite happy and ex­cited. Fi­nally, the times have changed and peo­ple are now ex­pected to know the fea­tures of C11 and not just ANSI C. So, I be­lieve this is the right time to start preach­ing about the lat­est stan­dards of C.

The ANSI C stan­dard was adopted in 1989. In the last 28 years, C has pro­gressed quite a bit and has had three more stan­dards since ANSI C. But re­mem­ber, ANSI C is not even the first C stan­dard — K&R C holds that dis­tinc­tion. So, there were stan­dards be­fore and af­ter ANSI C. Let’s con­tinue with a dis­cus­sion of all the five dif­fer­ent stan­dards of C — K&R C, ANSI C, C99, C11 and Em­bed­ded C. For the pur­poses of our dis­cus­sion, the com­piler used is the gcc C com­piler from the GNU Com­piler Col­lec­tion (GCC).

If there are five dif­fer­ent stan­dards for C, which one is the de­fault stan­dard of gcc? The answer is: none of the above. The com­mand info gcc will tell you about the cur­rent de­fault stan­dard of gcc, which can also be ob­tained with the op­tion -std=gnu90. This stan­dard has the whole of ANSI C with some ad­di­tional GNU-spe­cific fea­tures. But there’s no need to panic — gcc gives you the op­tion to spec­ify a par­tic­u­lar stan­dard dur­ing com­pi­la­tion, ex­cept in the case of K&R C. I will men­tion these op­tions while dis­cussing each stan­dard.

But if there are so many stan­dards for C, who is re­spon­si­ble for cre­at­ing a new stan­dard? Can a small group of de­vel­op­ers pro­pose a new stan­dard for C? The answer is an em­phatic ‘No!’. The C stan­dards com­mit­tee re­spon­si­ble for mak­ing the de­ci­sions re­gard­ing C stan­dard­i­s­a­tion is called ISO/IEC JTC 1/SC 22/WG 14. It is a stan­dard­i­s­a­tion sub­com­mit­tee of the Joint Tech­ni­cal Com­mit­tee ISO/IEC JTC 1 of the In­ter­na­tional Or­ga­ni­za­tion for Stan­dard­iza­tion (ISO) and the In­ter­na­tional Elec­trotech­ni­cal Com­mis­sion (IEC).

K&R C

A lot has been writ­ten about the his­tory of C and I am not go­ing to re­peat it. All I want to say is that C was de­vel­oped by Den­nis Ritchie be­tween 1969 and 1973 at Bell Labs. It was

widely ac­cepted by al­most all the pro­fes­sional pro­gram­mers im­me­di­ately, and they started mak­ing their own ad­di­tions to it. Af­ter a few years, there were a lot of C vari­ants avail­able for a pro­gram­mer to choose from. My C was dif­fer­ent from yours, and no­body could claim that theirs was the real one. So, there was a need for stan­dard­i­s­a­tion and the masters them­selves have un­der­taken the task.

The stan­dard called K&R C was ac­tu­ally an in­for­mal spec­i­fi­ca­tion based on the first edi­tion of the ex­tremely pop­u­lar book, ‘The C Pro­gram­ming Lan­guage’, pub­lished in 1978 by Brian Kernighan and Den­nis Ritchie; hence, the name K&R af­ter Kernighan and Ritchie. But do re­mem­ber that the sec­ond edi­tion of the book that is avail­able nowa­days cov­ers mostly ANSI C. So, if you want to learn the de­tails of K&R C from the masters them­selves, then you have to buy a used copy of the first edi­tion of this book on­line.

K&R C not only acted as an in­for­mal stan­dards spec­i­fi­ca­tion for C but also added lan­guage fea­tures like the new data types long int and un­signed int and the com­pound as­sign­ment oper­a­tor. A stan­dard­ised I/O li­brary was also pro­posed by K&R C. Since the most pop­u­lar C stan­dard fol­lowed by the academia in In­dia is still ANSI C, I will men­tion a few dif­fer­ences be­tween K&R C and ANSI C. “There are 32 key­words in C,” is one of the clichés I ut­ter in many of my C pro­gram­ming classes. But I of­ten for­get to men­tion the fact that this was not al­ways true. Ac­cord­ing to K&R C, there are only 28 key­words in C. One key­word was called en­try, which was nei­ther im­ple­mented at that point in time by any of the com­pil­ers nor ac­cepted into the list of key­words in ANSI C.

An­other ma­jor dif­fer­ence is re­gard­ing the func­tion def­i­ni­tion. A func­tion def­i­ni­tion in K&R C has the pa­ram­e­ter types de­clared on sep­a­rate lines. Con­sider the fol­low­ing lines of code show­ing a func­tion def­i­ni­tion in K&R C:

The func­tion fun( ) ac­cepts an in­te­ger and a float­ing-point vari­able, and re­turns the prod­uct. You can clearly see that the two pa­ram­e­ters are de­clared on sep­a­rate lines be­low the name of the func­tion. This is not the style for func­tion def­i­ni­tion in ANSI C. The first line of the same func­tion def­i­ni­tion will be float fun( int a, float b ) in ANSI C. As men­tioned ear­lier, gcc does not have an op­tion to spec­ify com­pi­la­tion in the K&R C stan­dard. But pro­grams writ­ten in K&R C will also be com­piled with­out any er­rors, be­cause gcc com­piler is back­ward com­pat­i­ble with K&R C.

ANSI C

Even though K&R C was ac­cepted by many pro­gram­mers as the de facto stan­dard of C, it was not the de jure stan­dard, and no­body could have been coaxed into ac­cept­ing it as the of­fi­cial stan­dard of C. So, it was ab­so­lutely es­sen­tial for some stan­dards or­gan­i­sa­tion to ac­cept the chal­lenge of com­ing up with an of­fi­cial stan­dard for C. The Amer­i­can Na­tional Stan­dards In­sti­tute (ANSI) ad­dressed this is­sue in 1983 by form­ing a com­mit­tee, and the fi­nal draft of the stan­dards it for­mu­lated was re­leased in 1989. This is the rea­son why ANSI C is also called C89.

ANSI C is de­pen­dent on the POSIX stan­dard. POSIX is the Por­ta­ble Op­er­at­ing Sys­tem In­ter­face, a fam­ily of stan­dards spec­i­fied by the IEEE Com­puter So­ci­ety for main­tain­ing com­pat­i­bil­ity be­tween op­er­at­ing sys­tems. The same stan­dard pro­posed by ANSI was adopted by ISO of­fi­cially as ISO/IEC 9899:1990 in 1990. This is the rea­son why ANSI C is also called as ISO C and C90. One stan­dard and four dif­fer­ent names — I be­lieve this the rea­son why this par­tic­u­lar stan­dard of C is still very pop­u­lar. The fol­low­ing five key­words const, enum, signed, void and volatile were added, and the key­word en­try was dropped in ANSI C. The op­tion -ansi from gcc com­piler will com­pile pro­grams in the ANSI C stan­dard. The op­tions -std=c89 and -std=c90 will also com­pile pro­grams in ANSI C. Since gcc is a back­ward com­pat­i­ble com­piler, the above given op­tions will re­sult in suc­cess­ful com­pi­la­tion of pro­grams with K&R C fea­tures. Al­most all the pop­u­lar C com­pil­ers sup­port ANSI C fea­tures. These in­clude com­pil­ers like gcc from GCC, Por­ta­ble C Com­piler (PCC), Clang from LLVM, etc. For ex­am­ples of ANSI C code, open any text­book on C and you will find a lot of them.

C99

C99 is the in­for­mal name given to the ISO/IEC 9899:1999 stan­dards spec­i­fi­ca­tion for C that was adopted in 1999. The C99 stan­dard added five more key­words to ANSI C, and the to­tal num­ber of key­words be­came 37. The key­words added in C99 are _Bool, _Com­plex, _I­mag­i­nary, in­line and re­strict. The key­word _Bool is used to de­clare a new in­te­ger type ca­pa­ble of stor­ing 0 and 1. The key­words _Com­plex and _I­mag­i­nary are used to de­clare com­plex and imag­i­nary float­ing point type vari­ables to han­dle com­plex num­bers. The key­word in­line is used to de­clare in­line func­tions in C, sim­i­lar to C++ in­line func­tions. The key­word re­strict is used to tell the com­piler that for the life­time of the pointer, only the pointer it­self or a value di­rectly de­rived from it will be used to ac­cess the ob­ject to which it points. New header files like <std­bool.h>, <com­plex.h>, <tg­math.h>, <int­types.h>, etc, were also added in C99. A new in­te­ger data type called long long int with a min­i­mum size of 8 bytes was added in C99.

In gcc com­piler long long int usu­ally takes 8 bytes. The C99 pro­gram named fact.c given be­low finds the fac­to­rial of 20. A pro­gram us­ing the data type long int with the usual

min­i­mum size of 4 bytes would have given the answer as -2102132736 due to over­flow.

The pro­gram can be com­piled by ex­e­cut­ing the com­mand gcc ­std=c99 fact.c or the com­mand gcc fact.c on the ter­mi­nal. The sec­ond com­mand works be­cause, by de­fault, gcc com­piler sup­ports long long int. The out­put of the C99 pro­gram is shown in Fig­ure 1.

Fea­tures like vari­able length ar­rays, bet­ter sup­port for IEEE float­ing point stan­dard, sup­port for C++ style one line com­ments (//), macros with vari­able num­ber of ar­gu­ments, etc, were also added in C99. The of­fi­cial doc­u­men­ta­tion of gcc has this to say: ‘ISO C99. This stan­dard is sub­stan­tially com­pletely sup­ported’. The le­gal term ‘sub­stan­tially com­plete’ is slightly con­fus­ing but I be­lieve it means that the gcc com­piler sup­ports al­most all the fea­tures pro­posed by the stan­dards doc­u­men­ta­tion of C99. As men­tioned ear­lier, the op­tion -std=c99 of gcc will com­pile pro­grams in the C99 stan­dard.

C11

C11 is the cur­rent and lat­est stan­dard of the C pro­gram­ming lan­guage and, as the name sug­gests, this stan­dard was adopted in 2011. The for­mal doc­u­ment de­scrib­ing the C11 stan­dard is called ISO/IEC 9899:2011. With C11, seven more key­words were added to the C pro­gram­ming lan­guage, thereby mak­ing the to­tal num­ber of key­words, 44. The seven key­words added to C99 are _Alig­nas,_Alig­nof, _A­tomic, _Generic, _Nore­turn, _Stat­ic_assert and _Thread­_lo­cal. Con­sider the C11 pro­gram nore­turn.c shown be­low, which uses the key­word _Nore­turn.

Fig­ure 2 shows the out­put of the pro­gram nore­turn.c. There are a few warn­ings be­cause the main( ) has one more line of code af­ter the com­ple­tion of the func­tion fun( ). So why was the last printf( ) in main( ) not printed? This is due to the dif­fer­ence be­tween a func­tion re­turn­ing void and a func­tion with the _Nore­turn at­tribute. The key­word void only means that the func­tion does not re­turn any val­ues to the callee func­tion, but when the called func­tion ter­mi­nates the pro­gram, the pro­gram counter regis­ter makes sure that the ex­e­cu­tion con­tin­ues with the callee func­tion. But in case of a func­tion with the _Nore­turn at­tribute, the whole pro­gram ter­mi­nates af­ter the com­ple­tion of the called func­tion. This is the rea­son why the state­ments af­ter the func­tion call of fun( ) didn’t get ex­e­cuted.

The func­tion gets( ) caused a lot of may­hem and so was dep­re­cated in C99, and com­pletely re­moved in C11. Header files like <std­nore­turn.h> are also added in C11. Sup­port for con­cur­rent pro­gram­ming is an­other im­por­tant fea­ture added by the C11 stan­dard. Anony­mous struc­tures and unions are also sup­ported in C11. But un­like C99, where the im­ple­men­ta­tion of vari­able length ar­rays was manda­tory, in C11, this is an op­tional fea­ture. So even C11-com­pat­i­ble com­pil­ers may not have this fea­ture. The of­fi­cial doc­u­men­ta­tion of gcc again says that ‘ISO C11, the 2011 re­vi­sion of the ISO C stan­dard. This stan­dard is sub­stan­tially com­pletely sup­ported’. So, let us safely as­sume that the gcc com­piler sup­ports most of the fea­tures pro­posed by the C11 stan­dard doc­u­men­ta­tion. The op­tion -std=c11 of gcc will com­pile pro­grams in C11 stan­dard. The C Stan­dards Com­mit­tee has no im­me­di­ate plans to come up with a new stan­dard for C. So, I be­lieve we will be us­ing C11 for some time.

Em­bed­ded C

The stan­dard known as Em­bed­ded C is slightly dif­fer­ent from all the oth­ers. C from K&R C to C11 de­picts the changes of a pro­gram­ming lan­guage over time, based on user re­quire­ments. But the Em­bed­ded C stan­dard was pro­posed to cus­tomise the C lan­guage in such a way that it can cater to the needs of em­bed­ded sys­tem pro­gram­mers. While the other stan­dards of C are im­prove­ments over pre­vi­ous stan­dards, Em­bed­ded C is a stan­dard that is be­ing de­vel­oped in par­al­lel. A lot of non-stan­dard fea­tures were used in C pro­grams writ­ten for em­bed­ded sys­tems. Fi­nally, in 2008, the C Stan­dards com­mit­tee came up with a stan­dard­i­s­a­tion doc­u­ment that ev­ery­one has to ad­here to. Em­bed­ded C mostly has the syn­tax and se­man­tics of nor­mal C with ad­di­tional fea­tures like fixed-point arith­metic, named ad­dress spaces, and ba­sic I/O hard­ware ad­dress­ing. Two groups of fixed-point data types called the fract types and the ac­cum types were added to the C lan­guage to sup­port fixed-point arith­metic. The key­words _Fract and _Ac­cum are used for this pur­pose. New header files like <iohw.h> have also been pro­posed by the Em­bed­ded C stan­dards com­mit­tee. The main ad­van­tage of the Em­bed­ded C stan­dard is that it is sim­pler and eas­ier to learn than tra­di­tional C.

If you want to learn more about any of these stan­dards, just search the In­ter­net, par­tic­u­larly the web­site http://www.open-std.org/. I will be very happy if this ar­ti­cle mo­ti­vates some­body to mi­grate from

ANSI C to C11, which was re­leased in 2011. Six years have al­ready elapsed, so it is high time to adopt the

C11 stan­dard. As an aside, the lat­est stan­dard of C++, in­for­mally known as C++17, will be fi­nalised in 2017.

So, we have the chance to be the early adopters of a new stan­dard of a very pop­u­lar pro­gram­ming lan­guage, rather than be one among the late ma­jor­ity, as in the past.

Be­fore wind­ing up, I want to men­tion the Turbo C com­piler from Bor­land, which is out­dated yet used by a lot of peo­ple. Which is the stan­dard sup­ported by the Turbo C com­piler? Well, just like for the de­fault stan­dard sup­ported by gcc, the answer is also ‘None’. The de­fault stan­dard is the whole of ANSI C and some Turbo C spe­cific ex­ten­sions. Re­mem­ber, these ex­ten­sions are not avail­able with gcc and this is the rea­son why header files like <co­nio.h> are not avail­able in Linux. Fi­nally, it took me some time to come up with this pun, “Read this ar­ti­cle care­fully and you will see that you are not all at sea about C!”

Fig­ure 1: Fac­to­rial of 20 in C99

Fig­ure 2: _Nore­turn key­word in C11

Newspapers in English

Newspapers from India

© PressReader. All rights reserved.