• Home
  • InfoBase
  • Dictionaries
  • Member
  • News
  • 中文网站
     Advanced Search
    Read the latest Blogs from IT professionals in the field. Read and write community created documents. Need IT help? Ask our staff. Connect with your peers. Check our Tech Shop for posters, books and software tools. Home

    C Programming Language

    The C programming language (often, just "C") is a general-purpose, procedural, imperative computer programming language developed in the early 1970s by Dennis Ritchie for use on the Unix operating system. The first major program written in C was the UNIX operating system. It has since spread to many other operating systems. Although originally designed as a systems programming language, C has proved to be a powerful and flexible language that can be used for a variety of applications, from business programs to engineering. C is a particularly popular language for personal computer programmers because it is relatively small -- it requires less memory than other languages.

    K&R C
    In 1978, Dennis 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.

    K&R introduced several language features:

    struct data types
    long int data type
    unsigned int data type
    The =- operator was changed to -= to remove the semantic ambiguity created by the construct i=-10, which could be interpreted as either i =- 10 or i = -10
    For many years after the introduction of ANSI C, 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 ANSI C as well.

    In early versions of C, only functions that returned a non-integer value needed to be declared if used before the function definition; a function used without any previous declaration was assumed to return an integer.

    For example:

    long int SomeFunction();
    int OtherFunction();

    int CallingFunction()
    {
    long int test1;
    int test2;

    test1 = SomeFunction();
    if (test1 > 0) test2 = 0;
    else test2 = OtherFunction();

    return test2;
    }
    In the example, both SomeFunction and OtherFunction were declared before use. In K&R, OtherFunction declaration could be omitted.

    Since K&R function declarations did not include any information about function arguments, function parameter type checks were not performed, although some compilers would issue a warning message if a local function was called with the wrong number of arguments, or if multiple calls to an external function used different numbers of arguments. Separate tools such as Unix's lint utility were developed that (among other things) could check for consistency of function use across multiple source files.

    In the years following the publication of K&R C, several unofficial features were added to the language (since there was no standard), supported by compilers from AT&T and some other vendors. These included:

    void functions
    Functions returning struct or union types (rather than pointers)
    Assignment for struct data types
    const qualifier to make an object read-only
    Enumerated types
    The large number of extensions and lack of a standard library, together with the language popularity and the fact that not even the Unix compilers precisely implemented the K&R specification, led to the necessity of standardization.

    [edit]
    ANSI C and ISO C
    It has been suggested that ANSI C be merged into this article or section. (Discuss)
    The C Programming Language, 2nd edition, is a widely used reference on ANSI 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, resulting in the language now called C++.

    In 1983, the American National Standards Institute (ANSI) formed a committee, X3J11, to establish a standard specification of C. In 1989, the standard was ratified as ANSI X3.159-1989 "Programming Language C." This version of the language is often referred to as ANSI C, Standard C, or sometimes C89.

    In 1990, the ANSI C standard (with a few minor modifications) was adopted by the International Organization for Standardization (ISO) as ISO/IEC 9899:1990. This version is sometimes called C90. Therefore, the terms "C89" and "C90" refer to essentially the same language.

    One of the aims of the 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++), void pointers, support for international character sets and locales, and a more capable preprocessor. The syntax for parameter declarations was also augmented to include the C++ style:

    int main(int argc, char **argv)
    {
    ...
    }
    although the K&R interface

    int main(argc, argv)
    int argc;
    char **argv;
    {
    ...
    }
    continued to be permitted, for compatibility with existing source code.

    C89 is supported by current C compilers, and most C code being written nowadays is based on it. Any program written only in Standard C and without any hardware-dependent assumptions will run correctly on any platform with a conforming C implementation, within its resource limits. Without such precautions, programs may compile only on a certain platform or with a particular compiler, due, for example, to the use of non-standard libraries, such as GUI libraries, or to a reliance on compiler- or platform-specific attributes such as the exact size of data types and byte endianness.

    In cases where code must be compilable by either standard-conforming or K&R C-based compilers, the __STDC__ macro can be used to split the code into Standard and K&R sections, in order to take advantage of features available only in Standard C.

    #ifdef __STDC__
    extern int getopt(int,char * const *,const char *);
    #else
    extern int getopt();
    #endif
    In the above example, a compiler which has defined the __STDC__ macro (as mandated by the C standard) only interprets the line following the ifdef command. In other, nonstandard compilers which don't define the macro, only the line following the else command is interpreted.

    [edit]
    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, 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 in sometimes incompatible ways (e.g., the complex template class). Microsoft's Brandon Bray said "In general, we have seen little demand for many C99 features. Some features have more demand than others, and we will consider them in future releases provided they are compatible with C++." [1]

    GCC, despite its extensive C99 support, is still not a completely compliant implementation; several key features are missing or don't work correctly.[2]

    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