|
C99 Note: C99 is also the name of a C compiler for the Texas Instruments TI-99/4A home computer. Aside from being a C compiler, it is otherwise unrelated. 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:1999 in 1999. This standard is commonly referred to as "C99." It was adopted as an ANSI standard in March 2000. 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, including long long int, optional extended integer types, an explicit boolean data type, and a complex type to represent complex numbers · Variable-length arrays · Support for one-line comments beginning with //, as in BCPL or C++ · New library functions, such as snprintf · New header files, such as stdbool.h and inttypes.h · Type-generic math functions (tgmath.h) · Improved support for IEEE floating point · Designated initializers · Compound literals · Support for variadic macros (macros of variable arity) · restrict qualification 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 diagnose the omission but also assume int and continue translating the program. 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 __STDC_VERSION__ is defined with value 199901L to indicate that C99 support is available. As with the __STDC__ 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 ensures that inline is available in either case. #if __STDC_VERSION__ >= 199901L /* "inline" is a keyword */ #else # define inline /* nothing */ #endif Usage C's primary use is for "system programming", including implementing operating systems and embedded system applications, due to a combination of desirable characteristics such as code portability and efficiency, ability to access specific hardware addresses, ability to "pun" types to match externally imposed data access requirements, and low runtime demand on system resources. 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 higher-level languages. This is implemented in one of two ways, as languages which: · Emit C source code, and one or more other representations: machine code, object code, and/or bytecodes. Examples: some Lisp dialects (Lush), Squeak's C-subset Slang. · Emit C source code only, and no other representation. Examples: Eiffel, Sather; Esterel. C source code is then input to a C compiler, which then outputs finished machine or object code. This is done to gain portability (C compilers exist for nearly all platforms) and to avoid having to develop machine-specific code generators. 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
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, as well as the pointer-to symbol *, specify built-in types. Sections of code are enclosed in braces ({ and }) to indicate the extent to which declarations and control structures apply. As an imperative language, C depends on statements to do most of the work. Most statements are expression statements which simply evaluate an expression; as a side effect, variables may receive new values. Control-flow statements are also available for conditional or iterative execution, constructed with reserved keywords such as if, else, switch, do, while, and for. Arbitrary jumps are possible with goto. A variety of built-in operators perform primitive arithmetic, Boolean logical, comparative, bitwise logical, and array indexing operations and assignment. Expressions can also invoke functions, including a large number of standard library functions, for performing many common tasks. Operator precedence
What follows is the list of C operators sorted from highest to lowest priority. Operators of same priority are presented on the same line. Operators marked "R->L" are right-associative (operators with same priority are executed from the right to the left), others are left-associative. () [] -> . Unary (R->L): ! ~ - * & sizeof (type) ++ -- Binary arithmetical: * / % Binary arithmetical + - Shifts: << >> Comparisons: < <= > >= Comparisons: == != Bitwise: & Bitwise: ^ Bitwise: | Boolean: && Boolean: || Ternary: (R->L): ?: Assignments (R->L): = += -= *= /= &= |= ^= <<= >>= "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.A compliant version of the above program follows: #include <stdio.h> int main(void) { printf("hello, world\n"); return 0; }
What follows is a line-by-line analysis of the above program: #include <stdio.h>
This first line of the program is a preprocessing directive, #include. This causes the preprocessor — the first tool to examine source code when 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. in t main(void)
This next line indicates that a function named main is being defined. The main function serves a special purpose in C programs: When the program is executed, main is the function called by the run-time environment—otherwise it acts like any other function in the program. 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) in between the parentheses indicates that the main function takes no arguments. See also void {
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 beginning of the next 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. 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. If the above code were compiled and executed, it would do the following: · Print the string "hello, world" onto the standard output device (typically but not always a terminal), · Move the current position indicator to the beginning of the next line, and · Return a "successful" exit status to the calling process (such as a command shell or script). Data structures
C has a static weak typing type system that shares some similarities with that of other ALGOL descendants such as Pascal. There are built-in types for integers of various sizes, both signed and unsigned, floating-point numbers, characters, and enumerated types (enum). C99 added a Boolean datatype. There are also derived types including arrays, pointers, records (struct), and untagged unions (union). C is often used in low-level systems programming where "escapes" from the type system may be necessary. The compiler attempts to ensure type correctness of most expressions, but the programmer can override the checks in various ways, either by using a type cast to explicitly convert a value from one type to another, or by using pointers or unions to reinterpret the underlying bits of a value in some other way. (The use of type casts obviously sacrifices some of the safety normally provided by the type system.) |
