C language knowledge (2) -- History and Version （C语言 基本知识2）
By williamxue on Jun 03, 2007
C language knowledge (2) -- History and Version
C (programming language)
From Wikipedia, the free encyclopedia
The initial development of C occurred at AT&T Bell Labs between 1969 and 1973; according to Ritchie, the most creative period occurred in 1972. It was named "C" because many of its features were derived from an earlier language called "B," which according to Ken Thompson was a stripped down version of the BCPL programming language.
There are many legends as to the origin of C and the closely related Unix operating system, including these:
- The development of Unix was the result of programmers' desire to play the Space Travel computer game. They had been playing it on their company's mainframe, but as it was underpowered and had to support about 100 users, Thompson and Ritchie found they did not have sufficient control over the spaceship to avoid collisions with the wandering space rocks. This led to the decision to port the game to an idle PDP-7 in the office. As this machine lacked an operating system, the two set out to develop one, based on several ideas from colleagues. Eventually it was decided to port the operating system to the office's PDP-11, but faced with the daunting task of translating a large body of custom-written assembly language code, the programmers began considering using a portable, high-level language so that the OS could be ported easily from one computer to another. They looked at using B, but it lacked functionality to take advantage of some of the PDP-11's advanced features. This led to the development of an early version of the C programming language.
- The justification for obtaining the original computer to be used in developing the Unix operating system was to create a system to automate the filing of patents. The original version of the Unix system was developed in assembly language. Later, nearly all of the operating system was rewritten in C, an unprecedented move at a time when nearly all operating systems were written in assembly.
By 1973, the C language had become powerful enough that most of the Unix kernel, originally written in PDP-11 assembly language, was rewritten in C. This was one of the first operating system kernels implemented in a language other than assembly. (Earlier instances include the Multics system (written in PL/I), and MCP (Master Control Program) for the Burroughs B5000 written in ALGOL in 1961.)
ANSI C和ISO C
In 1978, Dennis Ritchie and Brian Kernighan published the first edition of The C Programming Language. This book, known to C programmers as "K&R", served for many years as an informal specification of the language. The version of C that it describes is commonly referred to as "K&R C". The second edition of the book covers the later ANSI C standard.
K&R introduced several language features:
long intdata type
unsigned intdata type
=-operator was changed to
-=to remove the semantic ambiguity created by the construct
i=-10, which could be interpreted as either
i =- 10or
i = -10
For many years after the introduction of ANSI C, K&R C was still considered the "lowest common denominator" to which C programmers restricted themselves when maximum portability was desired, since many older compilers were still in use, and because carefully written K&R C code can be legal ANSI C as well.
In early versions of C, only functions that returned a non-integer value needed to be declared if used before the function definition; a function used without any previous declaration was assumed to return an integer.
long int SomeFunction();
long int test1;
test1 = SomeFunction();
if (test1 > 0)
test2 = 0;
test2 = OtherFunction();
In the example, both
OtherFunction were declared before use. In K&R,
declaration could be omitted.
Since K&R function declarations did not include any information about function arguments, function parameter type checks were not performed, although some compilers would issue a warning message if a local function was called with the wrong number of arguments, or if multiple calls to an external function used different numbers of arguments. Separate tools such as Unix's lint utility were developed that (among other things) could check for consistency of function use across multiple source files.
In the years following the publication of K&R C, several unofficial features were added to the language (since there was no standard), supported by compilers from AT&T and some other vendors. These included:
- Functions returning
uniontypes (rather than pointers)
- Assignment for
constqualifier to make an object read-only
- Enumerated types
The large number of extensions and lack of a standard library, together with the language popularity and the fact that not even the Unix compilers precisely implemented the K&R specification, led to the necessity of standardization.
ANSI C and ISO C
During the late 1970s, C began to replace BASIC as the leading microcomputer programming language. During the 1980s, it was adopted for use with the IBM PC, and its popularity began to increase significantly. At the same time, Bjarne Stroustrup and others at Bell Labs began work on adding object-oriented programming language constructs to C, resulting in the language now called C++.
In 1983, the American National Standards Institute (ANSI) formed a committee, X3J11, to establish a standard specification of C. In 1989, the standard was ratified as ANSI X3.159-1989 "Programming Language C." This version of the language is often referred to as ANSI C, Standard C, or sometimes C89.
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 C standardization process
was to produce a superset of K&R C, incorporating many of the unofficial
features subsequently introduced. However, the standards committee also
included several new features, such as function prototypes (borrowed from C++),
pointers, support for international character sets and locales, and a more
capable preprocessor. The syntax for parameter declarations was also augmented
to include the C++ style:
int main(int argc, char \*\*argv)
although the K&R interface
int main(argc, argv)
continued to be permitted, for compatibility with existing source code.
C89 is supported by current C compilers, and most C code being written nowadays is based on it. Any program written only in Standard C and without any hardware-dependent assumptions will run correctly on any platform with a conforming C implementation, within its resource limits. Without such precautions, 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 a reliance on compiler- or platform-specific attributes such as the exact size of data types and byte endianness.
In cases where code must be compilable by either
standard-conforming or K&R C-based compilers, the
macro can be used to split the code into Standard and K&R sections, in
order to take advantage of features available only in Standard C.
extern int getopt(int,char \* const \*,const char \*);
extern int getopt();
In the above example, a compiler which has defined
__STDC__ macro (as mandated by the C standard) only interprets
the line following the
ifdef command. In other, nonstandard
compilers which don't define the macro, only the line following the
command is interpreted.
After the ANSI standardization process, the C
language specification remained relatively static for some time, whereas C++ continued to
evolve, largely during its own standardization effort. Normative Amendment 1
created a new standard for the C language in 1995, but only to correct some
details of the C89 standard and to add more extensive support for international
character sets. However, the standard underwent further revision in the late
1990s, leading to the publication of ISO 9899:
C99 introduced several new features, many of which had already been implemented as extensions in several compilers:
- Inline functions
- Variables can be declared anywhere (as in C++), rather than only after another declaration or the start of a compound statement
- Several new data types,
long long int, optional extended integer types, an explicit boolean data type, and a
complextype to represent complex numbers
- Variable-length arrays
- Support for one-line
comments beginning with
//, as in BCPL or C++
- New library functions,
- New header
files, such as
- Type-generic math
- Improved support for IEEE floating point
- Designated initializers
- Compound literals
- Support for variadic macros (macros of variable arity)
restrictqualification to allow more aggressive code optimization
C99 is for the most part upward-compatible with
C90, but is stricter in some ways; in particular, a declaration that lacks a
type specifier no longer has
int implicitly assumed. The C
standards committee decided that it was of more value for compilers to diagnose
inadvertent omission of the type specifier than to silently process legacy code
that relied on implicit
int. In practice, compilers are likely to
generate a warning.
Support by major compilers
GCC and other C compilers now support many of the new features of C99. However, there has been less support from vendors such as Microsoft and Borland that have mainly focused on C++, since C++ provides similar functionality improvement.
GCC, despite its extensive C99 support, is still not a completely compliant implementation; several key features are missing or don't work correctly.
A standard macro
defined with value
macro for C90,
__STDC_VERSION__ can be used to write code that
will compile differently for C90 and C99 compilers, as in this example that
inline is available in either case.
#if __STDC_VERSION__ >=
/\* "inline" is a keyword \*/
# define inline /\* nothing \*/
(中文)Chinese C wiki page: http://zh.wikipedia.org/wiki/C%E8%AF%AD%E8%A8%80