Design.
C is an imperative (procedural) language. It was designed to be compiled using a relatively straightforward compiler, to provide low-level access to memory, to provide language constructs that map efficiently to machine instructions, and to require minimal run-time support. C was therefore useful for many applications that had formerly been coded in assembly language, such as in system programming. Despite its low-level capabilities, the language was designed to encourage cross-platform programming. A standards-compliant and portably written C program can be compiled for a very wide variety of computer platforms and operating systems with few changes to its source code. The language has become available on a very wide range of platforms, from embedded microcontrollers to supercomputers.
Overview.
Like most imperative languages in the ALGOL tradition, C has facilities for structured programming and allows lexical variable scope and recursion, while a static type system prevents many unintended operations. In C, all executable code is contained within subroutines, which are called "functions" (although not in the strict sense of functional programming). Function parameters are always passed by value. Pass-by-reference is simulated in C by explicitly passing pointer values. C program source text is free-format, using the semicolon as a statement terminator and curly braces for grouping blocks of statements.
The C language also exhibits the following characteristics:
- There is a small, fixed number of keywords, including a full set of flow of control primitives:
for
,if/else
,while
,switch
, anddo/while
. There is one namespace, and user-defined names are not distinguished from keywords by any kind of sigil. - There are a large number of arithmetical and logical operators, such as
+
,+=
,++
,&
,~
, etc. - More than one assignment may be performed in a single statement.
- Function return values can be ignored when not needed.
- Typing is static, but weakly enforced: all data has a type, but implicit conversions can be performed; for instance, characters can be used as integers.
- Declaration syntax mimics usage context. C has no "define" keyword; instead, a statement beginning with the name of a type is taken as a declaration. There is no "function" keyword; instead, a function is indicated by the parentheses of an argument list.
- User-defined (
typedef
) and compound types are possible. - Heterogeneous aggregate data types (
struct
) allow related data elements to be accessed and assigned as a unit. - Array indexing is a secondary notation, defined in terms of pointer arithmetic. Unlike structs, arrays are not first-class objects; they cannot be assigned or compared using single built-in operators. There is no "array" keyword, in use or definition; instead, square brackets indicate arrays syntactically, e.g.
month[11]
. - Enumerated types are possible with the
enum
keyword. They are not tagged, and are freely inter convertible with integers. - Strings are not a separate data type, but are conventionally implemented as null-terminated arrays of characters.
- Low-level access to computer memory is possible by converting machine addresses to typed pointers.
- Procedures (subroutines not returning values) are a special case of function, with an un-typed return type
void
. - Functions may not be defined within the lexical scope of other functions.
- Function and data pointers permit ad hoc run-time polymorphism.
- A pre processor performs macro definition, source code file inclusion, and conditional compilation.
- There is a basic form of modularity: files can be compiled separately and linked together, with control over which functions and data objects are visible to other files via
static
andextern
attributes. - Complex functionality such as I/O, string manipulation, and mathematical functions are consistently delegated to library routines.
- standard I/O library
long int
data typeunsigned int
data type- compound assignment operators of the form
=
op (such as=-
) were changed to the form op=
to remove the semantic ambiguity created by such constructs asi=-10
, which had been interpreted asi =- 10
(decrement i by 10) instead of the possibly intendedi = -10
(let i be -10) - Even after the publication of the 1989 ANSI standard, for many years 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 Standard C as well.
C does not include some features found in newer, more modern high-level languages, including object orientation and garbage collection.
Relations To Other Languages.
Many later languages have borrowed directly or indirectly from C, including C++, D, Go, Rust, Java, JavaScript, Limbo, LPC, C#, Objective-C, Perl, PHP, Python, Verilog (hardware description language), and Unix's C shell. These languages have drawn many of their control structures and other basic features from C. Most of them (with Python being the most dramatic exception) are also very syntactically similar to C in general, and they tend to combine the recognizable expression and statement syntax of C with underlying type systems, data models, and semantics that can be radically different.
History.
Early Developments.
The origin of C is closely tied to the development of the Unix operating system, originally implemented in assembly language on a PDP-7 by Ritchie and Thompson, incorporating several ideas from colleagues. Eventually they decided to port the operating system to a PDP-11. The original PDP-11 version of Unix was developed in assembly language. The developers were considering to rewrite the system using the B language, Thompson's simplified version of BCPL. However B's inability to take advantage of some of the PDP-11's features, notably byteaddressability, led to the development of C
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. At that year a great part of Unix was rewritten in C.[10] By 1973, with the addition of
struct
types, the C language had become powerful enough that most of the Unix kernel was now in C.
Unix 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.) Circa 1977, further changes to the language were made by Ritchie and Stephen C. Johnson to facilitate portability of the Unix operating system. Johnson's Portable C Compiler served as the basis for several implementations of C on new platforms.
.
K&R C.
In 1978, Brian Kernighan and Dennis Ritchie 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 several language features: