ANSI C

From Wikipedia, the free encyclopedia

The C Programming Language, 2nd edition, is a widely used reference on ANSI C.
Enlarge
The C Programming Language, 2nd edition, is a widely used reference on ANSI C.

ANSI C is the standard published by the American National Standards Institute (ANSI) for the C programming language. Software developers writing in C are encouraged to conform to the requirements in the document, as it encourages easily portable code.

[edit] History of ANSI C and ISO C

The first standard for C was published by ANSI. Although this document was subsequently adopted by International Organization for Standardization (ISO) and subsequent revisions published by ISO have been adopted by ANSI, the name ANSI C (rather than ISO C) is still more widely used. While some software developers use the term ISO C, others are standards body neutral and use Standard C.

In 1983, the American National Standards Institute (ANSI) formed a committee, X3J11, to establish a standard specification of C. After a long and arduous process, the standard was completed in 1989 and ratified as ANSI X3.159-1989 "Programming Language C." This version of the language is often referred to as ANSI C, or sometimes C89 (to distinguish it from C99).

In 1990, the ANSI C standard (with a few minor modifications) was adopted by the International Organization for Standardization (ISO) as ISO/IEC 9899:1990. This version is sometimes called C90. Therefore, the terms "C89" and "C90" refer to essentially the same language.

One of the aims of the ANSI C standardization process was to produce a superset of K&R C (the first published standard), incorporating many of the unofficial features subsequently introduced. However, the standards committee also included several new features, such as function prototypes (borrowed from the C++ programming language), and a more capable preprocessor. The syntax for parameter declarations was also changed to reflect the C++ style.

ANSI C is now supported by almost all the widely used compilers. Most of the C code being written nowadays is based on ANSI C. Any program written only in standard C and without any hardware dependent assumptions is virtually guaranteed to run correctly on any platform with a conforming C implementation. Without such precautions, most programs may compile only on a certain platform or with a particular compiler, due, for example, to the use of non-standard libraries, such as GUI libraries, or to the reliance on compiler- or platform-specific attributes such as the exact size of certain data types and byte endianness.

To mitigate the differences between K&R C and the ANSI C standard, the __STDC__ macro can be used to split code into ANSI and K&R sections.

#if __STDC__
extern int getopt(int,char * const *,const char *);
#else
extern int getopt();
#endif

Some suggest using "#if __STDC__" as above rather than "#ifdef __STDC__" because some compilers set __STDC__ to zero to indicate non-ANSI compliance.

In other languages