The C Programming Language[1] (often referred to as K&R), the seminal book on C | |
Paradigm | Imperative (procedural), structured |
---|---|
Designed by | Dennis Ritchie |
Developer | Dennis Ritchie & Bell Labs (creators); ANSI X3J11 (ANSI C); ISO/IEC JTC1/SC22/WG14 (ISO C) |
First appeared | 1972; 47 years ago[2] |
Stable release | |
Typing discipline | Static, weak, manifest, nominal |
OS | Cross-platform |
Filename extensions | .c, .h |
Major implementations | |
K&R, GCC, Clang, Intel C,Microsoft Visual C++, Watcom C | |
Dialects | |
Cyclone, Unified Parallel C, Split-C, Cilk, C* | |
Influenced by | |
B (BCPL, CPL), ALGOL 68,[3]Assembly, PL/I, FORTRAN | |
Influenced | |
Numerous: AMPL, AWK, csh, C++, C--, C#, Objective-C, D, Go, Java, JavaScript, Julia, Limbo, LPC, Perl, PHP, Pike, Processing, Python, Ring,[4]Rust, Seed7, Vala, Verilog (HDL),[5]Nim | |
|
if/else
, for
, do/while
, while
, and switch
. User-defined names are not distinguished from keywords by any kind of sigil.+
, +=
, ++
, &
, ||
, etc.typedef
) and compound types are possible.struct
) allow related data elements to be accessed and assigned as a unit.month[11]
.enum
keyword. They are freely interconvertible with integers.void
.static
and extern
attributes.Year | C Standard[9] |
---|---|
1972 | Birth |
1978 | K&R C |
1989/1990 | ANSI C and ISO C |
1999 | C99 |
2011 | C11 |
2017/2018 | C18 |
struct
types.long int
data typeunsigned int
data type=op
(such as =-
) were changed to the form op=
(that is, -=
) to remove the semantic ambiguity created by constructs such as i=-10
, which had been interpreted as i =- 10
(decrement i
by 10) instead of the possibly intended i = -10
(let i
be -10).int
must be declared if used before the function definition; functions used without prior declaration were presumed to return type int
. Youtube video editor free for mac.int
type specifiers which are commented out could be omitted in K&R C, but are required in later standards.void
functions (i.e., functions with no return value)struct
or union
types (rather than pointers)struct
data typesvoid
pointers, support for international character sets and locales, and preprocessor enhancements. Although the syntax for parameter declarations was augmented to include the style used in C++, the K&R interface continued to be permitted, for compatibility with existing source code.__STDC__
macro can be used to split the code into Standard and K&R sections to prevent the use on a K&R C-based compiler of features available only in Standard C.long long int
and a complex
type to represent complex numbers), variable-length arrays and flexible array members, improved support for IEEE 754 floating point, support for variadic macros (macros of variable arity), and support for one-line comments beginning with //
, as in BCPL or C++. Many of these had already been implemented as extensions in several C compilers.int
implicitly assumed. A standard macro __STDC_VERSION__
is defined with value 199901L
to indicate that C99 support is available. GCC, Solaris Studio, and other C compilers now support many or all of the new features of C99. The C compiler in Microsoft Visual C++, however, implements the C89 standard and those parts of C99 that are required for compatibility with C++11.[17]__STDC_VERSION__
is defined as 201112L
to indicate that C11 support is available.__STDC_VERSION__
is defined as 201710L
./*
and */
, or (since C99) following //
until the end of the line. Comments delimited by /*
and */
do not nest, and these sequences of characters are not interpreted as comment delimiters if they appear inside string or character literals.[20]struct
, union
, and enum
, or assign types to and perhaps reserve storage for new variables, usually by writing the type followed by the variable name. Keywords such as char
and int
specify built-in types. Sections of code are enclosed in braces ({
and }
, sometimes called 'curly brackets') to limit the scope of declarations and to act as a single statement for control structures.if
(-else
) conditional execution and by do
-while
, while
, and for
iterative execution (looping). The for
statement has separate initialization, testing, and reinitialization expressions, any or all of which can be omitted. break
and continue
can be used to leave the innermost enclosing loop statement or skip to its reinitialization. There is also a non-structured goto
statement which branches directly to the designated label within the function. switch
selects a case
to be executed based on the value of an integer expression.&&
, ||
, ?:
and the comma operator). This permits a high degree of object code optimization by the compiler, but requires C programmers to take more care to obtain reliable results than is needed for other programming languages.a
–z
A
–Z
0
–9
! ' # % & ' ( ) * + , - . / : ; < = > ? [ ] ^ _ { | } ~
uXXXX
or UXXXXXXXX
encoding (where the X
denotes a hexadecimal character), although this feature is not yet widely implemented.
|
|
|
|
|
|
|
|
entry
, but this was seldom implemented, and has now been removed as a reserved word.[23]+
, -
, *
, /
, %
=
+=
, -=
, *=
, /=
, %=
, &=
, |=
, ^=
, <<=
, >>=
~
, &
, |
, ^
<<
, >>
!
, &&
, ||
? :
!=
( )
++
, --
.
, ->
sizeof
<
, <=
, >
, >=
&
, *
, [ ]
,
( )
(typename)
=
(used in mathematics to express equality) to indicate assignment, following the precedent of Fortran and PL/I, but unlike ALGOL and its derivatives. C uses the operator to test for equality. The similarity between these two operators (assignment and equality) may result in the accidental use of one in place of the other, and in many cases, the mistake does not produce an error message (although some compilers produce warnings). For example, the conditional expression if(ab+1)
might mistakenly be written as if(a=b+1)
, which will be evaluated as true if a
is not zero after the assignment.[24]&
(bitwise AND) and |
(bitwise OR) in expressions such as x & 1 0
, which must be written as (x & 1) 0
if that is the coder's intent.[25]#include
. This causes the compiler to replace that line with the entire text of the stdio.h
standard header, which contains declarations for standard input and output functions such as printf
and scanf
. The angle brackets surrounding stdio.h
indicate that stdio.h
is located using a search strategy that prefers headers provided with the compiler to other headers having the same name, as opposed to double quotes which typically include local or project-specific header files.main
is being defined. The main
function serves a special purpose in C programs; the run-time environment calls the main
function to begin program execution. The type specifier int
indicates that the value that is returned to the invoker (in this case the run-time environment) as a result of evaluating the main
function, is an integer. The keyword void
as a parameter list indicates that this function takes no arguments.[b]main
function.printf
, which in this case is supplied from a system library. In this call, the printf
function is passed (provided with) a single argument, the address of the first character in the string literal'hello, worldn'
. The string literal is an unnamed array with elements of type char
, set up automatically by the compiler with a final 0-valued character to mark the end of the array (printf
needs to know this). The n
is an escape sequence that C translates to a newline character, which on output signifies the end of the current line. The return value of the printf
function is of type int
, but it is silently discarded since it is not used. (A more careful program might test the return value to determine whether or not the printf
function succeeded.) The semicolon ;
terminates the statement.main
function. According to the C99 specification and newer, the main
function, unlike any other function, will implicitly return a value of 0
upon reaching the }
that terminates the function. (Formerly an explicit return 0;
statement was required.) This is interpreted by the run-time system as an exit code indicating successful execution.[27]enum
). Integer type char
is often used for single-byte characters. C99 added a boolean datatype. There are also derived types including arrays, pointers, records (struct
), and unions (union
).struct
objects linked together using pointers. Pointers to functions are useful for passing functions as arguments to higher-order functions (such as qsort or bsearch) or as callbacks to be invoked by event handlers.[27]0
, with or without explicit casting to a pointer type, or as the NULL
macro defined by several standard headers. In conditional contexts, null pointer values evaluate to false, while all other pointer values evaluate to true.void *
) point to objects of unspecified type, and can therefore be used as 'generic' data pointers. Since the size and type of the pointed-to object is not known, void pointers cannot be dereferenced, nor is pointer arithmetic on them allowed, although they can easily be (and in many contexts implicitly are) converted to and from any other object pointer type.[27]malloc
function, and treat it as an array. C's unification of arrays and pointers means that declared arrays and these dynamically allocated simulated arrays are virtually interchangeable.x[i]
(where x
designates a pointer) is syntactic sugar for *(x+i)
.[32] Taking advantage of the compiler's knowledge of the pointer type, the address that x + i
points to is not the base address (pointed to by x
) incremented by i
bytes, but rather is defined to be the base address incremented by i
multiplied by the size of an element that x
Free code editor for mac. points to. Thus, x[i]
designates the i+1
th element of the array.sizeof
), the name of an array is automatically converted to a pointer to the array's first element. This implies that an array is never copied as a whole when named as an argument to a function, but rather only the address of its first element is passed. Therefore, although function calls in C use pass-by-value semantics, arrays are in effect passed by reference.sizeof
to any dereferenced element of x
, as in n = sizeof *x
or n = sizeof x[0]
, and the number of elements in a declared array A
can be determined as sizeof A / sizeof A[0]
. The latter only applies to array names: variables declared with subscripts (int A[20]
). Due to the semantics of C, it is not possible to determine the entire size of arrays through pointers to arrays or those created by dynamic allocation (malloc
); code such as sizeof arr / sizeof arr[0]
(where arr
designates a pointer) will not work since the compiler assumes the size of the pointer itself is being requested.[33][34] Since array name arguments to sizeof
are not converted to pointers, they do not exhibit such ambiguity. However, arrays created by dynamic allocation are accessed by pointers rather than true array variables, so they suffer from the same sizeof
issues as array pointers.memcpy
function, or by accessing the individual elements.malloc
from a region of memory called the heap; these blocks persist until subsequently freed for reuse by calling the library function realloc
or free
malloc
for an example of dynamically allocated arrays.) Unlike automatic allocation, which can fail at run time with uncontrolled consequences, the dynamic allocation functions return an indication (in the form of a null pointer value) when the required storage cannot be allocated. (Static allocation that is too large is usually detected by the linker or loader, before the program can even begin execution.)free()
is called, then that memory cannot be recovered for later reuse and is essentially lost to the program, a phenomenon known as a memory leak. Conversely, it is possible for memory to be freed but continue to be referenced, leading to unpredictable results. Typically, the symptoms will appear in a portion of the program far removed from the actual error, making it difficult to track down the problem. (Such issues are ameliorated in languages with automatic garbage collection.)-lm
, shorthand for 'link the math library').[27]stdio.h
) specify the interfaces for these and other standard library facilities.stdio.h
). File handling is generally implemented through high-level I/O which works through streams. A stream is from this perspective a data flow that is independent of devices, while a file is a concrete device. The high level I/O is done through the association of a stream to a file. In the C standard library, a buffer (a memory area or queue) is temporarily used to store data before it's sent to the final destination. This reduces the time spent waiting for slower devices, for example a hard drive or solid state drive. Low-level I/O functions are not part of the standard C library but are generally part of 'bare metal' programming (programming that's independent of any operating system such as most but not all embedded programming). With few exceptions, implementations include low-level I/O.main
function actually has two arguments, int argc
and char *argv[]
, respectively, which can be used to handle command line arguments. The ISO C standard (section 5.1.2.2.1) requires both forms of main
to be supported, which is special treatment not afforded to any other function.1980s: ; Verilog first introduced ; Verilog inspired by the C programming languageCite uses deprecated parameter
|deadurl=
(help)|deadurl=
(help)