C (programming language)
C is a programming language developed by Ken Thompson and Dennis Ritchie, in the early 1970s, for use on the UNIX operating system. It is now used on practically every operating system, and is the most popular language for writing system software, though it is also used for writing applications. It is also commonly used in computer science education. The popular C++ programming language is based on C.
Features
C is a relatively minimalist programming language. It is significantly lower-level than most other programming languages. Even though it is sometimes referred to as a "high level language", it is only really higher-level than the various assembly languages.
C has two important advantages over assembly. Firstly, code is generally easier to read and much less burdensome to write, especially for lengthy programs. Secondly, assembly code is usually applicable only to a specific computer architecture, whereas a C program can be ported to any architecture on which a C compiler and certain required libraries exist. (C code is almost always compiled, rather than interpreted.) On the other hand, the efficiency of C code is somewhat dependent on the ability of the compiler to optimize the resulting machine language, which is largely out of the programmer's control. In contrast, the efficiency of assembly code is precisely determined, since assembly is just human-readable notation for a machine language. For this reason, programs such as operating system kernels, though mostly written in C, may contain "hand-tuned" fragments of assembly language where performance is especially crucial.
Similar advantages and disadvantages distinguish C from higher-level languages: the efficiency of C code can be more closely controlled, at the cost of being generally more troublesome to read and write. Note, however, that C is at least as portable as higher-level languages, because nowadays most computer architectures are equipped with a C compiler and libraries; in fact, the compilers, libraries, and interpreters of higher-level languages are often implemented in C!
One of the most notable features of C is that it is up to the programmer to manage the contents of computer memory. Standard C provides no facilities for array bounds checking or automatic garbage collection. In contrast, the Java and C# languages, both descendants of C, provide automatic memory management, including garbage collection. While manual memory management provides the programmer with greater leeway in tuning the performance of a program, it also makes it easy to produce bugs involving erroneous memory operations, such as buffer overflows. Bugs of these sort have gained notoriety for their effects on computer insecurity. Some tools have been created to help C programmers avoid memory errors, including libraries for performing array bounds checking and automatic garbage collection, and automated source code checkers such as Lint.
Some of the specific features of C are:
- An extremely simple core language, with non-essential functionality provided by a standardized set of library routines.
- Focus on the procedural programming paradigm, with facilities for programming in a structured style.
- Use of a preprocessor language, the C preprocessor, for tasks such as defining macros and including multiple source code files.
- O(1) performance for all operators.
- Low-level access to computer memory via the use of pointers.
- Parameters are always passed to functions by value, never by reference.
- Lexical variable scoping (but no support for closures or functions defined within functions).
History
Early developments
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". Accounts differ regarding the origins of the name "B": Ken Thompson credits the BCPL programming language, but he had also created a language called Bon in honor of his wife Bonnie.
By 1973, the C language had become powerful enough that most of the UNIX kernel, originally written in PDP-11/20 assembly language, was rewritten in C. This was one of the first operating system kernels implemented in a language other than assembly, earlier instances being the Multics system (written in PL/I) and TRIPOS (written in BCPL.)
K&R C
In 1978, 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, described below.)
K&R introduced the following features to the language:
struct
data typeslong int
data typeunsigned int
data type- The
=+
operator was changed to+=
, and so forth (=+
was confusing the C compiler's lexical analyzer).
K&R C is often considered the most basic part of the language that is necessary for a C compiler to support. For many years, even after the introduction of ANSI C, it was considered the "lowest common denominator" that C programmers stuck to when maximum portability was desired, since not all compilers were updated to fully support ANSI C, and reasonably well-written K&R C code is also legal ANSI C.
In the years following the publication of K&R C, several "unofficial" features were added to the language, supported by compilers from AT&T and some other vendors. These included:
void
functions andvoid *
data type- functions returning
struct
orunion
types struct
field names in a separate name space for each struct type- assignment for
struct
data types const
qualifier to make an object read-only- a standard library incorporating most of the functionality implemented by various vendors
- enumerations
- the single-precision
float
type
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. The language they produced, called C++, is now the most common application programming language on the Microsoft Windows operating system; C remains more popular in the Unix world.
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. In 1990, the ANSI C standard (with a few minor modifications) was adopted by the International Standards Organization (ISO) as ISO/IEC 9899:1990.
One of the aims of the ANSI 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++), and a more capable preprocessor.
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 is guaranteed to perform correctly on any platform with a conforming C implementation. However, many programs have been written that will only compile on a certain platform, or with a certain compiler, due to (i) the use of non-standard libraries, e.g. for graphical displays, and (ii) some compilers not adhering to the ANSI C standard, or its successor, in their default mode.
C99
After the ANSI standardization process, the C language specification remained relatively static for some time, whereas C++ continued to evolve. (Normative Amendment 1 created a new version of the C language in 1995, but this version is rarely acknowledged.) However, the standard underwent revision in the late 1990s, leading to the publication of ISO 9899:1999 in 1999. This standard is commonly referred to as "C99". It was adopted as an ANSI standard in March 2000.
The new features in C99 include:
- inline functions
- freeing of restrictions on the location of variable declarations (as in C++)
- addition of several new data types, including
long long int
(to reduce the pain of the looming 32-bit to 64-bit transition), an explicit boolean data type, and acomplex
type representing complex numbers - variable-length arrays
- official support for one-line comments beginning with
//
, borrowed from C++ - several new library functions, such as
snprintf()
- several new header files, such as
stdint.h
Interest in supporting the new C99 features appears to be mixed. Whereas GCC and several other compilers now support most of the new features of C99, the compilers maintained by Microsoft and Borland do not, and these two companies do not seem to be interested in adding such support.
"Hello, World!" in C
The following simple application prints out "Hello, World!" to the standard output file (which is usually the screen, but might be a file or some other hardware device). A version of this program appeared for the first time in K&R.
#include <stdio.h> int main(void) { printf("Hello, World!\n"); return 0; }
Links and references
References
- The C Programming Language, by Brian Kernighan and Dennis Ritchie. Also known as K&R. The original book on C.
- 1st, Prentice-Hall 1978; ISBN 0-131-10163-3. Pre-ANSI C.
- 2nd, Prentice-Hall 1988; ISBN 0-131-10362-8. ANSI C.
- The C Standard, edited by the British Standard Institute. The official ISO standard (C99) in book form.
- Wiley, 2003; ISBN 0-470-84573-2.
- C: A Reference Manual, by Samuel P. Harbison and Guy L. Steele. This book is excellent as a definitive reference manual, and for those working on C compiler and processors. The book contains a BNF grammar for C.
- 4th, Prentice-Hall 1994; ISBN 0-133-26224-3.
- 5th, Prentice-Hall 2002; ISBN 0-130-89592-X.
See also
- Anatomy of C program
- C library
- C standard library (ANSI C standard library)
- GNU Compiler Collection
- make
External links
- The Development of the C Language by Dennis M. Ritchie
- Programming in C: A Tutorial by Brian W. Kernighan
- Lysator collection of C language resources
- comp.lang.c Answers to Frequently Asked Questions (FAQ List) by Steve Summit
- Splint - Secure Programming Lint - First Aid for Programmers
An early version of this article contained material from FOLDOC, used with permission.