C has also been widely used to implement end-user applications, although as applications became larger much of that development shifted to other, higher-level languages.
One consequence of C's wide acceptance and efficiency is that the compilers, libraries, and interpreters of other higher-level languages are often implemented in C.
C is used as an intermediate language by some implementations of higher-level languages, which translate the input language to C source code, perhaps along with other object representations. The C source code is compiled by a C compiler to produce object code. This approach may be used to gain portability (C compilers exist for nearly all platforms) or for convenience (it avoids having to develop machine-specific code generators). Some programming languages which use C this way are Eiffel, Esterel, Gambit, the Glasgow Haskell Compiler, Lisp dialects, Lush, Sather, Squeak, and Vala.
Unfortunately, C was designed as a programming language, not as a compiler target language, and is thus less than ideal for use as an intermediate language. This has led to development of C-based intermediate languages such as C--.
Syntax
Main article: C syntax
Unlike languages such as FORTRAN 77, C source code is free-form which allows arbitrary use of whitespace to format code, rather than column-based or text-line-based restrictions. Comments may appear either between the delimiters /* and */, or (in C99) following // until the end of the line.
Each source file contains declarations and function definitions. Function definitions, in turn, contain declarations and statements. Declarations either define new types using keywords such as 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 }) to limit the scope of declarations and to act as a single statement for control structures.
As an imperative language, C uses statements to specify actions. The most common statement is an expression statement, consisting of an expression to be evaluated, followed by a semicolon; as a side effect of the evaluation, functions may be called and variables may be assigned new values. To modify the normal sequential execution of statements, C provides several control-flow statements identified by reserved keywords. Structured programming is supported by 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.
Expressions can use a variety of built-in operators (see below) and may contain function calls. The order in which operands to most operators, as well as the arguments to functions, are evaluated is unspecified; the evaluations may even be interleaved. However, all side effects (including storage to variables) will occur before the next "sequence point"; sequence points include the end of each expression statement and the entry to and return from each function call. This permits a high degree of object code optimization by the compiler, but requires C programmers to exert more care to obtain reliable results than is needed for other programming languages.
C Operators
Main article: Operators in C and C++
C supports a rich set of operators, which are symbols used within an expression to specify the manipulations to be performed while evaluating that expression. C has operators for:
arithmetic
equality testing
order relations
boolean logic
bitwise logic
assignment
increment and decrement
reference and dereference
conditional evaluation
member selection
type conversion
object size
function argument collection
sequencing
subexpression grouping
Operator precedence and associativity
What follows is the list of C operators sorted from highest to lowest priority (precedence). Operators of same priority are presented on the same line. (That the post-increment operator (++) has higher priority than the dereference operator (*) means that an expression *p++ is grouped as *(p++) and not (*p)++. That the subtraction operator (-) has left-to-right associativity means that an expression a-b-c is grouped as (a-b)-c and not a-(b-c).)
Class Associativity Operators
Grouping Nesting (expr)
Postfix Left-to-Right (args) [] -> . expr++ expr--
Unary Right-to-Left ! ~ + − * & (typecast) sizeof ++expr --expr
Multiplicative Left-to-Right * / %
Additive Left-to-Right + -
Shift Left-to-Right << >>
Relational Left-to-Right < <= > >=
Equality Left-to-Right == !=
Bitwise AND Left-to-Right &
Bitwise XOR Left-to-Right ^
Bitwise OR Left-to-Right
Logical AND Left-to-Right &&
Logical OR Left-to-Right
Conditional Right-to-Left ?:
Assignment Right-to-Left = += -= *= /= &= = ^= <<= >>=
Sequence Left-to-Right ,
"Hello, world" example
The following simple application appeared in the first edition of K&R, and has become the model for an introductory program in most programming textbooks, regardless of programming language. The program prints out "hello, world" to the standard output, which is usually a terminal or screen display. Standard output might also be a file or some other hardware device, depending on how standard output is mapped at the time the program is executed.
main()
{
printf("hello, world\n");
}
The above program will compile on most modern compilers that are not in compliance mode, but does not meet the requirements of either C89 or C99. Compiling this program in C99 compliance mode will result in warning or error messages.[8] A compliant version of the above program follows:
#include
int main(void)
{
printf("hello, world\n");
return 0;
}
#include
This first line of the program is a preprocessing directive, #include. This causes the preprocessor — the first tool to examine source code as it is compiled — to substitute the line with the entire text of the stdio.h file. The header file stdio.h contains declarations for standard input and output functions such as printf. The angle brackets surrounding stdio.h indicate that stdio.h can be found using an implementation-defined search strategy. Double quotes may also be used for headers, thus allowing the implementation to supply (up to) two strategies. Typically, angle brackets are reserved for headers supplied by the C compiler, and double quotes for local or installation-specific headers.
int main(void)
This next line indicates that a function named 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 return value, the value of evaluating the main function that is returned to its invoker (in this case the run-time environment), is an integer. The keyword void as a parameter list indicates that the main function takes no arguments.[9]
{
This opening curly brace indicates the beginning of the definition of the main function.
printf("hello, world\n");
This line calls (executes the code for) a function named printf, which is declared in the included header stdio.h and 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, world\n". 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 the 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 by the caller. (A more careful program might test the return value to determine whether or not the printf function succeeded.) The semicolon ; terminates the statement.
return 0;
This line terminates the execution of the main function and causes it to return the integer value 0, which is interpreted by the run-time system as an exit code (indicating successful execution).
}
This closing curly brace indicates the end of the code for the main function.
No comments:
Post a Comment