|
|
C Programming Language#REDIRECT C programming language C programming language[[Image:K&R_C.jpg|thumb|right|''The C Programming Language (book)'', Brian Kernighan and Dennis Ritchie, the original edition that served for many years as an informal specification of the language]] The C programming language is a standardized programming language developed in the early 1970s by Ken Thompson and Dennis Ritchie for use on the Unix operating system. It has since spread to many other operating systems, and is one of the most widely used programming languages. C is prized for its efficiency, and is the most popular programming language for writing system software, though it is also used for writing Application softwares. It is also commonly used in computer science education, despite not being designed for novices. == Features == === Overview === C is a relatively minimalist programming language that operates close to the hardware, and is more similar to assembly language than most High-level programming language are. Indeed, C is sometimes referred to as "portable assembly," reflecting its important difference from Low-level programming language such as assembly languages: C code can be compiled to run on almost any computer, more than any other language in existence, while any given assembly language runs on at most a few very specific models of computer. For these reasons C has been called a Medium-level programming language. C was created with one important goal in mind: to make it easier to write large programs with fewer errors in the procedural programming paradigm, but without putting a burden on the writer of the C compiler, who is encumbered by complex language features. To this end, C has the following important features: * A simple core language, with important functionality such as math functions or file handling provided by sets of Library (computer science) instead * Focus on the procedural programming paradigm, with facilities for programming in a Structured programming * A simple type system which prevents many operations that are not meaningful * Use of a preprocessor language, the C preprocessor, for tasks such as defining macros and including multiple source code files * Low-level unchecked access to computer memory via the use of pointers * A minimalistic set of keywords * Parameter (computer science)s that are always passed to functions by value, never by reference * Function pointers, which allow for a rudimentary form of Closure (computer science) and Polymorphism (computer science) * Lexical variable scoping * Record (computer science)s, or user-defined aggregate datatypes ( structs) which allow related data to be combined and manipulated as a whole
Some features that C lacks that are found in other languages include:
* Type safety
* Garbage collection (computer science)
* Class (computer science)es or object (computer science)s with behavior (see object-oriented programming)
* An advanced type system
* Closure (computer science)
* Nested functions
* Generic programming
* Overloading and operator overloading
* Native support for multithreading and computer networks
* List processing
Although the list of useful features C lacks is long, this has in a way been important to its acceptance, because it allows new compilers to be written quickly for it on new platforms, and because it keeps the programmer in close control of what the program is doing. This is what often allows C code to run more efficiently than many other languages. Typically only hand-tuned assembly language code runs more quickly, since it has complete control of the machine, but advances in compilers along with new complexity in modern processors have quickly narrowed this gap.
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.
=== "hello, world" example ===
The following simple application appeared in the first edition of The C Programming Language (book), and has become a standard introductory program in most programming textbooks, regardless of language. The program prints out "Hello world program" to standard output, which is usually a terminal or screen display. However, it might be a file or some other hardware device, including the bit bucket, depending on how standard output is mapped at the time the program is executed.
The above program will compile correctly on most modern compilers that are not in compliance mode. However, it produces several warning messages when compiled with a compiler that conforms to the ANSI C standard. Additionally, the code will not compile if the compiler strictly conforms to the C99 standard, as a return value of type int will no longer be inferred if the source code has not specified otherwise. These messages can be eliminated with a few minor modifications to the original program:
What follows is a line-by-line analysis of the above program: #includeThis 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 for that line the entire text of the file or other entity to which it refers. In this case, the header file stdio.h—which contains the definitions of standard input and output functions—will replace that line. The angle brackets surrounding stdio.h indicate that the file can be found within one of the locations given to the preprocessor through the ''search path'' for header files.
int main(void)This next line indicates that a function named main is being defined. The main function (programming) function serves a special purpose in C programs. When they are executed, main() is the first function called. The portion of the code that reads int indicates that the ''return value''—the value to which the main function will evaluate—is an integer. The portion that reads (void) indicates that the main function will take no arguments from whatever calls it. See also Void (computer science).
{
This opening curly brace indicates the beginning of the definition of the main function.
printf("hello, world\n");
This line ''calls''—looks up and then executes the code for—a function named printf, which was declared in the included header file stdio.h. In this call, the printf function is ''passed''—provided with—a single argument, the string literal "hello, world\n". The sequence that reads \n is an ''escape sequence'' that is translated to the EOL—or end-of-line—character, which is intended to move the terminal cursor to the beginning of the next line. The return value of the printf function is of type int, but no use was made of it so it will be quietly discarded.
return 0;This line terminates the execution of the main function and causes it to return the integral value 0.
}This closing curly brace indicates the end of the code for the main function.
===Types===
C has a type system similar to that of other ALGOL descendants such as Pascal programming language, although different in a number of ways. There are types for integers of various sizes, both signed and unsigned, floating-point numbers, characters, enumerated types (enum), record (computer science) (struct), and untagged union (computer science)s (union).
C makes extensive use of pointers, a very simple type of reference (computer science) that stores the address of a memory location. Pointers can be ''dereferenced'' to retrieve the data stored at that address. The address can be manipulated with regular assignment and pointer arithmetic. At runtime, a pointer represents a memory address. At compile-time, it is a complex type that represents both the address and the type of the data. This allows expressions including pointers to be type-checked. Pointers are used for many different purposes in C. Text strings are commonly represented with a pointer to an array of characters. Dynamic memory allocation, which is described below, is performed using pointers.
A ''null pointer'' has a reserved value indicating that it points to no valid location. These are useful for indicating special cases such as the ''next'' pointer in the final node of a linked list. Dereferencing a null pointer causes unpredictable behavior. Pointers to type void also exist, and point to objects of unknown type. These are particularly useful for generic programming. Since the size and type of the objects they point to is not known they cannot be dereferenced, but they can be converted to other types of pointers.
Array types in C are of a fixed, static size known at compile-time; this isn't too much of a hindrance in practice, since one can allocate blocks of memory at runtime using the standard library and treat them like arrays. Unlike many other languages, C represents arrays just as it does pointers: as a memory address and a data type. Therefore, index values can exceed the actual size of an array.
C also supplies multi-dimensional arrays. The index values of the arrays are assigned in row-major order. Semantically these arrays function like arrays of arrays, but physically they are stored as a single one-dimensional array with computed offsets.
C is often used in low-level systems programming, where it may be necessary to treat an integer as a memory address, a double-precision value as an integer, or one type of pointer as another. For such cases C provides ''casting'', which forces the explicit conversion of a value from one type to another. The use of casts sacrifices some of the safety normally provided by the type system.
=== Data storage ===
One of the most important functions of a programming language is to provide facilities for managing computer memory and the objects that are stored in memory. C provides three distinct ways of allocating memory for objects:
* Static memory allocation: space for the object is provided in the binary at compile-time; these objects have a lifetime (computer science) as long as the binary which contains them exists
* Automatic memory allocation: temporary objects can be stored on the stack (computing), and this space is automatically freed and reusable after the block they are declared in is left
* Dynamic memory allocation: blocks of memory of any desired size can be requested at run-time using the library functions malloc, malloc, and malloc from a region of memory called the dynamic memory allocation; these blocks are reused after malloc is called on them
These three approaches are appropriate in different situations and have various tradeoffs. For example, static memory allocation has no allocation overhead, automatic allocation has a small amount of overhead during initialization, and dynamic memory allocation can potentially have a great deal of overhead for both allocation and deallocation. On the other hand, stack space is typically much more limited than either static memory or heap space, and only dynamic memory allocation allows allocation of objects whose size is only known at run-time. Most C programs make extensive use of all three.
Where possible, automatic or static allocation is usually preferred because the storage is managed by the compiler, freeing the programmer of the error-prone hassle of manually allocating and releasing storage. Unfortunately, many data structures can grow in size at runtime; since automatic and static allocations must have a fixed size at compile-time, there are many situations in which dynamic allocation must be used. Variable-sized arrays are a common example of this (see "malloc" for an example of dynamically allocated arrays).
=== Syntax ===
''Main article: C syntax''
Unlike languages like Fortran 77, C is free-form, allowing the programmer to use whitespace to organize their code. Comments can be included either by wrapping a comment in /* and */, or following // until the end of that 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 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 *, represent built-in types. Sections of code are enclosed in braces ({ and }) to indicate the extent to which declarations and control structures apply.
Statements perform imperative actions, such as modifying the value of a variable or outputting text. Control structures are available for conditional or iterative execution, constructed with reserved keywords such as if, else, switch, do, while, and for, and arbitrary jumps are possible with goto. A variety of built-in operators perform primitive arithmetic, logical, comparative, bitwise, and array indexing operations and assignment. Statements can also call functions, including a large number of standard library functions, for performing many common tasks.
== Problems ==
A popular saying, repeated by such notable language designers as Bjarne Stroustrup, is that "C makes it easy to shoot yourself in the foot." In other words, C permits many operations that are generally not desirable, and thus many simple errors made by a programmer are not detected by the compiler or even when they occur at runtime. This leads to programs with unpredictable behavior and security holes.
Part of the reason for this is to avoid compile and runtime checks that were too expensive when C was originally designed. Rather than placing these checks in the compiler, additional tools, such as lint, were used. Another reason is the desire to keep C as efficient and flexible as possible; the more powerful a language, the more difficult it is to prove things about programs written in it. Today many tools are available to allow a C programmer to detect or correct various common problems, but others are impossible to reliably detect due to the lack of constraints on C programs.
One problem is that automatically and dynamically allocated objects are not initialized; they initially have whatever value is present in the memory space they are assigned. This value is highly unpredictable, and can vary between two machines, two program runs, or even two calls to the same function. If the program attempts to use such an uninitialized value, the results are usually unpredictable. Most modern compilers detect and warn about this problem in some restricted cases.
Pointers are one primary source of danger; because they are unchecked, a pointer can be made to point to any object of any type, including code, and then written to, causing unpredictable effects. Although most pointers point to safe places, they can be moved to unsafe places using pointer arithmetic, the memory they point to may be deallocated and reused (dangling pointers), they may be uninitialized (wild pointers), or they may be directly assigned any value using a cast or through another corrupt pointer. Another problem with pointers is that C freely allows conversion between any two pointer types. Other languages attempt to address these problems by using more restrictive reference (computer science) types.
Although C has native support for static arrays, it does not verify that array indexes are valid (bounds checking). For example, one can write to the sixth element of an array with five elements, yielding generally undesirable results. This is called a ''buffer overflow''. This has been notorious as the source of a number of security problems in C-based programs.
Multidimensional arrays are necessary in numerical algorithms (mainly from applied linear algebra) to store matrices (the representations of linear mappings). The structure of the C-array is neither well adapted nor fit for this particular task. This problem is discussed in the book Numerical Recipes in C, Chap. 1.2, page 20 ff ([http://www.library.cornell.edu/nr/bookcpdf/c1-2.pdf online readable!]). You can find there also a good solution which is used throughout this book. It is at the same time a weak point as a strength of C, that the problem does exist and has a working solution too within the language.
Another common problem is that heap memory cannot be reused until it is explicitly released by the programmer with free(). The result is that if the programmer accidentally forgets to free memory, but continues to allocate it, more and more memory will be consumed over time. This is called a ''memory leak''. Conversely, it is possible to release memory too soon, and then continue to use it. Because the allocation system can reuse the memory at any time for unrelated reasons, this results in insidiously unpredictable behavior. These issues in particular are ameliorated in languages with garbage collection (computer science).
Yet another common problem are variadic functions, which take a variable number of arguments. Unlike other prototyped C functions, checking the arguments of variadic functions at compile-time is not mandated by the standard, and is impossible in general without additional information. If the wrong type of data is passed, the effect is unpredictable, and often fatal. Variadic functions also handle null pointer constants in an unexpected way. For example, the printf family of functions supplied by the standard library, used to generate formatted text output, is notorious for its error-prone variadic interface, which relies on a format string to specify the number and type of trailing arguments. Type-checking of variadic functions from the standard library is a quality of implementation issue, however, and many modern compilers do in particular type-check printf calls, producing warnings if the argument list is inconsistent with the format string. It should be noted that not all printf calls can be checked statically (this is difficult as soon as the format string itself comes from somewhere hard to trace), and other variadic functions typically remain unchecked.
Tools have been created to help C programmers avoid many of these errors in many cases. Automated source code checking and auditing is fruitful in any language, and for C many such tools exist, such as lint programming tool. A common practice is to use Lint to detect questionable code when a program is first written. Once a program passes Lint, it is then compiled using the C compiler. There are also libraries for performing array bounds checking and a limited form of garbage collection (computer science), but they are not a standard part of C.
There are other problems in C that don't directly result in errors, but do inhibit the ability of a programmer to build a robust, maintainable system. Examples of these include:
* A fragile system for importing definitions that relies on literal text inclusion and redundantly keeping prototypes and function definitions in sync.
* A cumbersome compilation model that forces manual dependency tracking and inhibits compiler optimizations between modules (except by link-time optimization).
* A weak type system that lets many clearly erroneous programs compile without errors.
* The difficulty of creating opaque structures, which results in programs that tend to violate information hiding.
* The un-intuitive declaration syntax, particularly for function pointers. In the words of language researcher Damian Conway speaking about the very similar C++ declaration syntax:
::Specifying a type in C++ is made difficult by the fact that some of the components of a declaration (such as the pointer specifier) are prefix operators while others (such as the array specifier) are postfix. These declaration operators are also of varying precedence, necessitating careful bracketing to achieve the desired declaration. Furthermore, if the type ID is to apply to an identifier, this identifier ends up at somewhere between these operators, and is therefore obscured in even moderately complicated examples (see Appendix A for instance). The result is that the clarity of such declarations is greatly diminished.
::''Ben Werther & Damian Conway. [http://www.csse.monash.edu.au/~damian/papers/HTML/ModestProposal.html#section3.1.1 A Modest Proposal: C++ Resyntaxed]. Section 3.1.1. 1996.''
== History ==
=== Early developments ===
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. It was named "C" because many of its features were derived from an earlier language called "B programming language".
Accounts differ regarding the origins of the name "B": Ken Thompson credits the BCPL programming language, but he had also created a language called Bon (programming language) in honor of his wife Bonnie.
There are many legends as to the origin of C and its related operating system, Unix, including:
* The development of C was the result of the programmers' desire to play Spacewar. They had been playing it on their company's mainframe, but being underpowered and having to support about 100 users, Thompson and Ritchie found they didn't have sufficient control over the spaceship to avoid collisions with the wandering asteroid. Thus, they decided to port the game to an idle PDP-7 in the office. But it didn't have an operating system (OS), so they set about writing one. Eventually they decided to port the operating system to the office's PDP-11, but this was onerous since all the code was in assembly language. They decided to use a higher-level portable language so the OS could be ported easily from one computer to another. They looked at using B, but it lacked functionality to take advantage of some of the PDP-11's advanced features. So they set about creating the new language, C.
*The justification for obtaining the original computer that was used to develop Unix was to create a system to automate the filing of patents. The original version of Unix was developed in assembly language. Later, the C language was developed in order to rewrite the operating system.
By 1973, the C language had become powerful enough that most of the Unix kernel (computers), originally written in PDP-11/20 assembly language, was rewritten in C. This was one of the first operating system kernels implemented in a language other than assembly, earlier instances being the Multics system (written in PL/I programming language), TRIPOS (written in BCPL), and MCP (Master Control Program) for Burroughs B5000 written in ALGOL in 1961.
=== K&R C ===
In 1978, Ritchie and Brian Kernighan published the first edition of ''The C Programming Language (book) ''. 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 the following features to the language:
* struct data types
* long int data type
* unsigned int data type
* The =+ operator was changed to +=, and so forth (=+ was confusing the C compiler's lexical analyzer; for example, i =+ 10 compared with i = +10).
K&R C is often considered the most basic part of the language that is necessary for a C compiler to support. For many years, even after the introduction of ANSI C, it was considered the "lowest common denominator" that C programmers stuck to when maximum portability was desired, since not all compilers were updated to fully support ANSI C, and reasonably well-written K&R C code is also legal ANSI C.
In these early versions of C, only functions that returned a non-integer value needed to be defined or declared (using a prototype statement) before use. A function used without any declaration was assumed to be one returning an integer. Function parameters were not type checked, although some compilers would issue a warning message if a function was called with the wrong number of arguments.
In the years following the publication of K&R C, several "unofficial" features were added to the language, supported by compilers from AT&T and some other vendors. These included:
* void functions and void * data type
* functions returning struct or union types
* struct field names in a separate name space for each struct type
* assignment (computer science) for struct data types
* const qualifier to make an object read-only
* a C standard library incorporating most of the functionality implemented by various vendors
* enumerations
* the single-precision float type
=== ANSI C and ISO C ===
During the late 1970s, C began to replace BASIC programming language 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.
The language they produced, called C Plus Plus, is now the most common application programming language on the Microsoft Windows operating system; C remains more popular in the Unix world. Another language developed around that time is Objective-C which also adds object oriented programming to C. While, now, not as popular as C++, it is used to develop Mac OS X's Cocoa (API) applications.
In 1983, the American National Standards Institute (ANSI) formed a committee, X3J11, to establish a standard specification of C. After a long and arduous process, the standard was completed in 1989 and ratified as ANSI X3.159-1989 "Programming Language C". This version of the language is often referred to as ANSI C.
In 1990, the ANSI C standard (with a few minor modifications) was adopted by the International Organization for Standardization (ISO) as ISO 9899.
One of the aims of the ANSI 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++), and a more capable preprocessor.
ANSI C is now supported by almost all the widely used compilers. Most of the C code being written nowadays is based on ANSI C. Any program written ''only'' in standard C is guaranteed to perform correctly on any system platform with a conforming C implementation. However, many programs have been written that will only compile on a certain platform, or with a certain compiler, due to (i) the use of non-standard libraries, such as for Graphical user interface, and (ii) some compilers' not adhering to the ANSI C standard, or its successor, in their default mode, or (iii) reliance on the exact size of certain datatypes as well as on the endianness of the platform.
=== C99 ===
After the ANSI standardization process, the C language specification remained relatively static for some time, whereas C Plus Plus programming language continued to evolve. (Normative Amendment 1 created a new version of the C language in 1995, but this version is rarely acknowledged.) However, the standard underwent 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.
The new features in C99 include:
* 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 (to reduce the pain of the looming 32-bit to 64-bit transition), an explicit Boolean datatype data type, and a complex type representing complex numbers
* variable-length arrays
* support for one-line comments beginning with //, like in BCPL or C++, and which many C compilers have previously supported as an extension
* several new library functions, such as snprintf()
* several new header files, such as stdint.h
Interest in supporting the new C99 features appears to be mixed. Whereas GNU Compiler Collection and several other compilers now support most of the new features of C99, the compilers maintained by Microsoft and Borland do not, and these two companies do not seem to be interested in adding such support.
== Relation to C++ ==
The C Plus Plus programming language was originally derived from C. However, contrary to popular belief, not every C program is a valid C++ program. As C and C++ have evolved independently, there has been an unfortunate growth in the number of incompatibilities between the two languages [http://david.tribble.com/text/cdiffs.htm]. The latest revision of C, C99, created a number of additional conflicting features. The differences make it hard to write programs and libraries that are compiled and function correctly as either C or C++ code, and confuse those who program in both languages. The disparity also makes it hard for either language to adopt features from the other one.
Bjarne Stroustrup, the creator of C++, has repeatedly suggested [http://www.research.att.com/~bs/sibling_rivalry.pdf] that the incompatibilities between C and C++ should be reduced as much as possible in order to maximize inter-operability between the two languages. Others have argued that since C and C++ are two different languages, compatibility between them is useful but not vital; according to this camp, efforts to reduce incompatibility should not hinder attempts to improve each language in isolation.
Today, the primary differences between the two languages are:
* inline — inline functions are in the global scope in C++, and in the file (so-called "static") scope in C. In simple terms, this means that in C++, any definition of any inline function (but irrespective of C++ function overloading) must conform to C++'s "One Definition Rule" or ODR, requiring that either there be a single definition of any inline function or that all definitions be semantically equivalent; but that in C, the same inline function could be defined differently in different ''translation units'' (translation unit typically refers to a Computer file).
* The bool keyword in C99 is in its own header, . Previous C standards did not define a boolean type, and various (incompatible) methods were used to simulate a boolean type.
* Character constants (enclosed in single quotes) have the size of an int in C and a char in C++. That is to say, in C, sizeof('a') == sizeof(int); in C++, sizeof('a') == sizeof(char). Nevertheless, even in C they will never exceed the values that a char can store, so (char)'a' is a safe conversion.
* Additional keywords were introduced in C++, and thus they cannot be used as identifiers as they could in C. (for example, try, catch, template, new, delete, ...)
* In C++, the compiler automatically creates a "tag" for every struct, union or enum, so struct S {}; in C++ is equivalent to typedef struct S {} S; in C.
C99 adopted some features that first appeared in C++. Among them are:
* Mandatory prototype declarations for functions
* The inline keyword
* The removal of the "implicit int" return value
== See also ==
*C preprocessor
*C standard library
*C library
*C syntax
*List of articles with C programs
*Objective-C
*Operators in C and C Plus Plus
*Programming tools: Cygwin, Dev-C Plus Plus, DJGPP, GNU Compiler Collection, Little C compiler, Linker, make, SPlint, Small-C, C--, C Plus Plus
== References ==
* Brian Kernighan, Dennis Ritchie: ''The C Programming Language (book)''. Also known as K&R — The original book on C.
**1st, Prentice Hall 1978; ISBN 0-131-10163-3. Pre-ANSI C.
**2nd, Prentice Hall 1988; ISBN 0-131-10362-8. ANSI C.
*British Standard Institute: ''The C Standard'', John Wiley & Sons, ISBN 0-470-84573-2. The official ISO standard (C99) in book form.
*Samuel P. Harbison, Guy L. Steele: ''C: A Reference Manual''. This book is excellent as a definitive reference manual, and for those working on C compilers. The book contains a Backus-Naur form grammar for C.
**4th, Prentice Hall 1994; ISBN 0-133-26224-3.
**5th, Prentice Hall 2002; ISBN 0-130-89592-X.
*Robert Sedgewick: ''Algorithms in C'', Addison-Wesley, ISBN 0-201-31452-5 (Part 1–4) and ISBN 0-201-31663-3 (Part 5)
* William H. Press, Saul A. Teukolsky, William T. Vetterling, Brian P. Flannery: Numerical Recipes in C (The Art of Scientific Computing), ISBN 0 521 43108 5
== External links ==
=== C ===
*[http://www.faqs.org/faqs/C-faq/faq/index.html ''comp.lang.c Frequently Asked Questions''] by Steve Summit
*[http://cm.bell-labs.com/cm/cs/who/dmr/chist.html ''The Development of the C Language''] by Dennis M. Ritchie
*[http://www.lysator.liu.se/c/ Programming in C] (document collection at Lysator)
*[http://www.ioccc.org International Obfuscated C Competition]
*[http://en.wikibooks.org/wiki/Programming:C ''Programming C''] at Wikibooks
*[http://www.computer-books.us/c.php Computer-Books.us] Online books about C
*[http://www.allfreetutorials.com/index.php?pg=c-tutorials.htm Collection of free C tutorials]
=== C99 ===
* [http://www-106.ibm.com/developerworks/linux/library/l-c99.html?ca=dgr-lnxw07UsingC99 Open source development using C99 — Is your C code up to standard?] by Peter Seebach
*[http://www.kuro5hin.org/?op=displaystory;sid=2001/2/23/194544/139 Are you Ready For C99?]
Curly bracket programming languages
C programming language
C programming language family
Major programming languages
Systems programming languages
ANSI standards
ISO standards
lv:C (programmēšanas valoda)
ms:Bahasa pengaturcaraan C
th:ภาษาซี
C programming language==Reorganization!== I propose a reorganization of the series on the C programming language. I put forth the following four general article headings: #History of the C programming language—The creation and original uses of C, its development at AT&T/Bell Labs, the various attempts at standardising #Syntax of the C programming language—The syntax (grammar rules, not behavior) of C #Semantics of the C programming language—The semantics (behavior and relationships of syntatic components) of C #Standard C library—History of std library, list of headers, platform variations (BSD, GNU, VC++/Win32, Borland/Win32, etc.) I think that the current situation is a mess and needs reform. Comments welcome, of course.—User:Kbolino 06:00, Apr 7, 2005 (UTC) :Sounds good. I assume you will also have a main article that will provide an overview of the language and pointers to the subarticles w/ explanations of their content. I would suggest that we also add a section on common techniques, which would cover and/or refer to additional articles that cover other subjects (e.g., malloc, the dynamic array issue, etc.) User:Wrp103 - User talk:Wrp103 16:03, 7 Apr 2005 (UTC) == O Operators == can somebody please expand on the O(1) req on operators? O(1) as a function of what? Is a C impl on x86 for example is not complaint if for example the expr: ' x >>= c; ' is O(c) ? (since x86 shl reg,cl is O(cl)), how about mult/div? --Oyd11 ---- Do you think it's good to use :C_programming_language/Evolution for writing down the changes, or :C_programming_language/K and R C will suffice? Thanks, User:Uriyan Actually I don't think either topic deserves a sub-page. I think a section on the main page would do. --User:drj == Eccentricity == :In C, the months of the year are numbered wrong. They are all low by 1. For example: January is month 0. December is month 11. This is not an obvious problem to English-speakers, but if your native language uses numbers to name the months of the year... Numbering from 0 is not eccentricity. It's how computers think. --User:Taw :That also has nothing at all to do with the C language, but with the standard libraries--that distinction should be made. --LDC ::Then why aren't days-of-month numbered starting from 0? User:193.167.132.66 08:53, 10 Feb 2005 (UTC) :::Because you probably don't have an array that stores the days of the month in text format. It is quite common to have an array of character strings w/ "Jan", "Feb", "Mar", etc. The same thing applies to week days ("Mon", "Tue", etc.) User:Wrp103 - User talk:Wrp103 15:43, 10 Feb 2005 (UTC) I don't even think thats an eccentricity since its fairly common in programming to do it that way --Alan D :Fairly common today, but it wasn't in the Good Old Days. That makes it a fad, albeit a fairly long running one. Hint: make a list of languages since Day 1 and see when numbering from 0 began. GregLindahl Java uses the same numbering scheme. Possibly because there is a zero-based array of month names. Although numbering from zero in both C and Java is more of a convenience for the routines that perform array handling than anything else. If your array starts at location AC00, the address of the 0th element is AC00, the address of element 1 is AC00 + element_length, the address of the 2nd element is AC00 + 2 * element_length, and so on. I prefer to start at element 1, but we're all pretty much stuck with the convention. (Perhaps I'll step up and start writing about software engineering, something I actually have some expertise in.) User:Ed Poor ----- Example date (works for C and Java): May 29 Month=4, day=29 Why is the month shifted but not the day of the month? Besides, if it is "the month is not a number", you deserve a slap in the face from your Korean (or Japanese or Chinese) secretary; ask her about it! In all these languages, May is literally "five month". (I think.) -- Juuitchan Yes, but you'd translate it into english as 'month five' if you didn't want to use the name - 五月 is how you'd write it in chinese characters. If you want to blame someone for the system in use, I'd suggest starting with the Jesuits, who as the scientific wing of the catholic church spread clockwork and steel cannons across much of asia. They also brought their date systems with them, which is why a 24 hour clock and seven day week is pretty much universally accepted. The difference between day and month in terms of indexing can be reduced to that of the difference between both nominal and cardinal values and ordinal values. The system we use uses nominal values for months, and ordinal values for days and years. It should probably use ordinal values for the lot. Note, though that Korean (and I expect Japanese and Chinese) uses cardinal values, rather than ordinal values for month. O-Ueol (五月) vs' O-Beon-JJae-Ueol (五番째月), except that normally the chinese characters aren't used in the second case. This was probably far more than you wanted to know, but the point is that given that there are trivial mappings between these forms, the particular representation format chosen isn't that big a deal. If you want to see _real_ problems with computational notions of time, I'd refer you to 'A Long, Painful History of Time' (Erik Naggum [http://heim.ifi.uio.no/~enag/lugm-time.html]). -- 203.231.161.129 ---- The claim that C is the dominant microcomputer applications language is now somewhat dubious, IMHO. In the Windows world, it's probably a toss-up between Microsoft's C++ and Visual Basic, I'd guess. C still rules for embedded systems (that is, the ones not written in Assembler), in the Unix world (particularly for apps that don't have a GUI), and people who can't be bothered remembering C++'s arcane semantics for multiple inheritance and operator overloading :) --User:Robert Merkel :How much Visual C++ is actually plain C with a tiny bit of C++ here and there? GregLindahl ::Good question, but one which I don't know the answer to. The only big Windows development project I ever saw was in "real" C++, with a class hierachy etc. etc. It was an absolute PITA to work with, though - in the end I just gave up and wrote the code I needed by monitoring a socket interface this code had with a socket monitor I ginned up with :Cygwin.--User:Robert Merkel :Any windows programming that uses MFC is making extensive use of C++, just by virtue of modelling windows in terms of objects. I think that's actually a good portion of Visual C++ programming. :Yup. Visual C++ (the language) is mostly C++. Of course, the product Microsoft Studio, Visual C++, has compilers for both the language C and the language C++ (and options to turn microsoft extensions to both of those language on or off). Secretly the two compilers are the same, but accepting different input languages. ---- Recent stats of what percent of code in RedHat distro is written in which programing language, clearly shows that C is dominant, at least on Unices. It's very probable that C++ is much more popular on Windoze world, but I seriously doubt that many apps are written in VB. --User:Taw ---- It would be nice to have links to an online manual and online tutorials. There probably are some with under the GNU Free Documentation License. --Hirzel ---- comp.lang.c (a wonderful resource for C, btw -- some very competent people there) recommends Tom Torfs' tutorial at http://www.geocities.com/tom_torfs/c.html and Steve Summit's class notes at http://www.eskimo.com/~scs/cclass/cclass.html. Honestly, most online tutorials for C are terrible and demonstrate that the author has little clue about the actual C standard. --User:mgmei ---- Moved from article: :''C is a high level language, meaning that the source code of a program can be written without detailed knowledge of the computer's Central processing unit type. Before the program can be used, the source code must be translated into the required machine language by a compiler. In contrast, programs written in an assembly language can only be run on one type of CPU.'' :''(The above definition of high level language is not meaningful. Consider the evolution of the x86 instruction set toward virtual machinehood. Does this mean that x86 assembly is a high level language?)'' I'm not sure what 203.231.161.129 means about "virtual machinehood", but I can see that under this definition, x86 assembly language could be considered a high level language due to the existence of emulators. -- User:Tim Starling 07:54 26 Jun 2003 (UTC) Modern x86 implementations are more and more moving toward risc cores running virtual machines (in microcode or whatever) which provide x86 compatible instruction sets. Hyperthreading for example, is a case of attempting to exploit such an underlying architecture without affecting the definition of the x86 machine (ie, mapping many registers to the x86's few, so that you can increase some hparallelism). In this regard the x86 architecture is shifting toward defining a virtual machine, much like JVM, rather than specifying a hardware cpu. Not that there is a meaningful distinction in any case. Likewise, we can see C programs running in a virtual machine defined by the C standard, and supported by the runtime structure of the binary produced. High and low level are fundamentally ideological terms, and have almost no objective meaning, nor objective definition what-so-ever. So far the only meaningful definition of level that I've found has been in terms of 'the ability to express invariant structure' with more being higher level. Note that by this definition, python ends up being only slightly higher than assembly, since it has almost no ability to define invariant structure. I wish people would stop using these terms, anyhow, as they are very silly. -- 203.231.161.129 ==B origin from Wombat encyclopedia== The page says the language B got its name from BCPL. But [http://wombat.doc.ic.ac.uk/foldoc/foldoc.cgi?B Wombat]'s encyclopedia says this is wrong. It says "B had nothing to do with BCPL. B was in fact a revision of an earlier language, bon, named after Ken Thompson's wife, Bonnie." User:Jay 22:44 5 Jul 2003 (UTC) Maybe it does but, at best, Wombat is a tertiary source, quoting a secondary source, Foldoc, and we have to ask ourselves "How do they know that ? What is their primary source for that information ?" Our source for the statement that "B is based on BCPL" is the primary source, ''User Reference for B'', written by Ken Thompson, in which he states that: ::''B is a computer language directly descendant from BCPL. B 1s running at Murray Hill on the DEC PDP-11 computer under the UNIX-11 time sharing system. B is good for recursive, non-numeric, machine independent applications, such as system and language work.'' This is a pretty unequivocal statement of B's ancestry from Ken Thompson himself. Having said that, I have no doubt that Ken reused code from his work on the ''bon'' compiler in writing the B compiler -- that's the nature of programming -- but according to Ken himself, the language design was based on BCPL, not ''bon'', so it doesn't really matter what others say. -- User:Derek Ross | User talk:Derek Ross 19:01, 2004 Jul 14 (UTC) :We're dealing with name-origins here, not the development of programming languages themselves, which you would realize is a different issue. You can have a look at the link in the below discussion #B from Bon or BCPL ?, for a primary source. User:Jay 19:16, 14 Jul 2004 (UTC) And indeed I did as you will see if you read my comments there. However it turns out that the document concerned is a description of ''bon''. It has nothing to say about B. The best information that we have about the link between the names is that which comes from Dennis Ritchie, and he favours the B from BCPL explanation. I would also like to point out that the Wombat article which you quote does not appear to be talking about name-origins of B, but rather about the development of the programming languages themselves, which is, of course, a different issue. It states that the B language is a revision not of BCPL, but of ''bon'' (which I have shown to be contradicted by Ken Thompson's statements) and that the origin of the name ''bon'' is Ken's wife's name (which may be true) but it does not say anything about the origin of the name, B.-- User:Derek Ross | User talk:Derek Ross 22:07, 2004 Jul 14 (UTC) :On looking more closely, the page talks about both name origins and prog. language development, and it claims B is from bon both ways, which as you say is contradictory. The languaging suggests that it is intended to debunking the B is from BCPL myth. The page also gives a reference to backup its claim "["The Programming Language B", S.C. Johnson & B.W. Kernighan, CS TR 8, Bell Labs (Jan 1973)].". So I guess until someone reads up the book and doesn't find the statements as mentioned, whats written on that page will get the benefit of doubt. User:Jay 19:06, 16 Jul 2004 (UTC) Dennis Ritchie has made "The Programming Language B", available at [http://cm.bell-labs.com/cm/cs/who/dmr/bintro.html]. It is in two sections, both of which I have read. Both mention the connection between B and BCPL. Neither mentions any connection between B and ''bon''. In fact neither mentions ''bon'' at all, so I guess that we can discount what Wombat says. -- User:Derek Ross | User talk:Derek Ross 20:56, 2004 Jul 16 (UTC) == Spelling of behaviour == I see that the spelling "behaviour" in place of "behavior" is used in the C programming language article. My copy of the C Standard uses "behavior". Should this Wikiopedia article use spelling consistent with the C language Standard? Or, maybe there is more than one English language version of the C Standard? User:Lfwlfw 01:57, 3 Aug 2003 (UTC) :I think we should use the spelling of the ISO C standard for technical terms such as ''undefined behavior'' and ''unspecified behavior''. I'm pretty sure that there's only one official English-language version of the ISO standard (at least for C99). --User:Zundark 10:31, 3 Aug 2003 (UTC) :: OK -- User:Lfwlfw 23:16, 3 Aug 2003 (UTC) ==B from Bon or BCPL ? == There were no responses to my comment of 5th July suggesting B was a revision of bon and not BPCL as claimed. Hence I've made the change in the main page. User:Jay 11:42, 10 Aug 2003 (UTC) :Jay,see http://cm.bell-labs.com/cm/cs/who/dmr/chist.html. I think the language there implies that C descended from BCPL, not bon. (At the end of the History: the setting section). -- User:Kushal kumaran ::The line is not conclusive enough. Ritchie says "most probably", and also does not claim that the ''Bon'' theory is wrong. Moroever, the creator of "B" is Ken Thompson and the Bon theory is taken from a paper/book (Thompson 69) written by him (this is mentioned in the same line). User:Jay 18:35, Aug 20, 2003 (UTC) :Firstly, Ken Thompson states in the ''User Reference to B'' that ''B was designed and implemented by D. M. Ritchie and the author.'', so I think that we have to give quite a lot of credit to what Dennis Ritchie believes and if he says ''most probably'', then as the acknowledged co-creator of B, he is in a much better position to know the truth than anyone other than Ken Thompson. ::You're right, I didn't about know the contribution of Dennis Ritchie to B. The first paragraph of B programming language doesn't have a mention of Ritchie. User:Jay 19:06, 16 Jul 2004 (UTC) :Secondly, the [Thompson 69] reference is to ''Bon — an Interactive Language'', which is an undated AT&T Bell Laboratories internal memorandum from around 1969 which describes the ''Bon'' language. B did not exist when it was written and thus it has nothing to say about the relationship between the two languages, although it may explain why ''Bon'' is so named. ::How can you say B didn't exist when the memorandum was written (in 1969)? The same page (C history paper) says in the Acknowledgments section, "Ken Thompson created the B language in 1969-70". Moreover the author of the paper (Dennis M. Ritchie) has listed the references he used in writing the paper, and when he has mentioned Thompson 69 as a reference to the alternate theory, it does mean he has read up the memorandum to get the info. Considering that Ken Thomson initially developed B and Ritchie pitched in later, its safer to assume that Thompson's words have more weightage. User:Jay 19:06, 16 Jul 2004 (UTC) ::The memorandum was written "ca. 1969" which could easily mean 1968 or 1970. Since it is a technical description of a MULTICS language, I think that it's highly likely that it was written between the time that Thompson finished work on ''bon'' and the end of Bell Labs involvement with MULTICS. Bell pulled out in April 1969, so that would place the memorandum sometime before May 1969. Work on Unix started in the summer after Bell Labs pulled out of Multics. At first Unix was written in assembler. Work on B started after Unix was substantially complete which puts it towards the end of 1969 or the beginning of 1970 (In fact according to Bell Labs pages on Unix History at [http://www.bell-labs.com/history/unix/btoc.html], work on B didn't start until 1971 which can't be correct). So the memorandum on the ''bon'' programming language was probably written between 3 and 18 months before work on B started. -- User:Derek Ross | User talk:Derek Ross 22:09, 2004 Jul 16 (UTC) :In any case the conclusive line implying that C descended from BCPL can be found in the ''User Reference to B'' as I stated earlier. -- User:Derek Ross | User talk:Derek Ross 19:20, 2004 Jul 14 (UTC) ::Did you mean B ? User:Jay 19:06, 16 Jul 2004 (UTC) :I meant C. I was referring to Kushal kumaran's response above. In any case saying that the statement is true for C, implies that it's true for B. -- User:Derek Ross | User talk:Derek Ross 22:09, 2004 Jul 16 (UTC) == Other libraries == It would be nice to have a paragraph about other important libraries besides the standard C library. : Which other libraries? Maybe glib, for instance. -- User:TakuyaMurata 01:38, Nov 16, 2003 (UTC) ---- :''By 1973, the C language had become powerful enough that most of the kernel of the Unix operating system was reimplemented in C, perhaps following the examples of the Multics system (implemented in PL/I), Tripos (implemented in BCPL), and perhaps others.'' But according to the TRIPOS page, TRIPOS was was developed in 1978, five years ''after'' Unix was re-implemented in C. Which is right? -- User:Khym Chanur -------- I would like to split off anatomy of C section to its own aritcle and merge ANSI C and ANSI C standard library with this article. It is more relevant to discuss history or standards than syntax details in this article, I think. Any thought or objection is welcomed. -- User:TakuyaMurata 01:38, Nov 16, 2003 (UTC) ---- " During the late '70s, C began to replace BASIC as the standard microcomputer language," : BASIC was never a "Standard" programming language in the '70s. It was a ''teaching'' language. Only with the rise of MS's Visual basic did it become a real-world language, and it's still very limited in its application (it's mostly useful for GUIs). User:Orthogonal 04:01, 26 Nov 2003 (UTC) :I think it depends on how you interpret the word "standard". If you mean "defined by formal written standards", then I'd agree that BASIC is not, and has never been, a standard language. But I don't think that's what's meant here. During the late 1970s, microcomputers were typically programmed in either assembly or BASIC - consider as examples the MITS Altair 8800 & 680, Commodore Pet, TRS-80, Apple 2, VIC-20 and Commodore 64. I'm rather suspicious of the IBM(1981) reference, though: I was using IBM PC's in 1989 and there wasn't a hint of C on them. -- User:Ortonmc 04:15, 26 Nov 2003 (UTC) :: For amateur end users, perhaps; but I was taking it to men that most ''applications'' were written in BASIC, which I find dubious. User:Orthogonal 04:26, 26 Nov 2003 (UTC) :It wasn't only a teaching language. At least one OS, DEC's RSTS, was implemented nearly entirely in BASIC (DEC's BASIC-PLUS in this case). - User:Lady Lysine Ikinsile 08:45, Jun 9, 2004 (UTC) I have removed the "standard" comment, since it is ambiguous. As well as the 1981 comment, since this text is not overly clear on this. Please direct such specific comments to my talkpage, to be sure I noticed them. User:Lir ---- 'C++ (thus avoiding the issue of whether the successor to "B" and "C" should be "D" or "P".)' Why would it be "P"? -- User:Ortonmc 04:15, 26 Nov 2003 (UTC) :: BCPL was the precursor to B and C... -- User:Merphant ::: Ah, I see. That would definitely limit the number of successors. :-) -- User:Ortonmc ---- Removed the image. To my understading of the ANSI C standard, void main(void); is not an acceptable prototype for main. User:Dysprosia 10:39, 27 Apr 2004 (UTC) A C program that prints "Hello world!" #includeA C program that prints "Hello world!" day_tab[i][j]
rather than
day_tab[i, j]
: I might be misunderstanding you, but here's my response. Many dynamic arrays literally store arrays of array pointers, and static arrays have this semantics, but static arrays are not stored this way, in a literal sense. They're laid out as one-dimensional arrays in row-major order, and pointers to subarrays are computed, rather than retrieved. That's why this code example produces "1234":
#include main method is required in all C programs that are intended to be executed, but it is not necessary for those that are not so intended." sounds like a joke; in fact, AFAIK the difference between a "C program" and a bunch of C code that's not a "program" is precisely the presence of main. Even if I'm wrong and the sentence is perfectly correct, I don't think that sentence will help readers of this article.
Dereferencing a pointer does not change what that pointer refers to, but produces a new value which is distinct from the pointer's value, and which refers to the data at the address stored in the pointer. But that's too long-winded for the article; I think my new phrasing is okay.
The fact that void pointers exist does not "indicate" that they point to an object of unknown type; the fact that they are CALLED 'void pointers' does, but again that's too long to say and it can be finessed with a rephrase.
I fixed a few grammatical errors too.
User:DanielCristofani 14:10, 17 May 2005 (UTC)
== "ALL" as a dangerous term ==
A recent editor reverted out an addition of mine, stating:
:''All C compilers ignore // comments; the standard requires it. But that is not particularly relevant here anyway''
(My addition was ''(Many modern C compilers can also ignore the // comment delimiter used by C++ code, although doing so may require a command-line qualifier.)''
I'm always amused whenever anyone suggests that ''all'' somethings do something or ''no'' somethings do something; such a statement is usually wrong. And it's certainly wrong in this case: There's no doubt that the class of C compilers also includes the compilers that implemented only the Old Testament and not just the ANSI standard, and those Old Testament compilers most assuredly do not implement that "//" comment delimiter. So the "modern" part of my comment was absolutely correct. And the portion about requiring a command-line qualifier was also correct; reasonably-recent and definitely ANSI-compliant versions of the Sun Workshop compilers most-assuredly ''DO'' require such a qualifier, although GCC doesn't.
: This depends on what you mean by "C compiler". I usually take it as an implementation of the C standard, and for the sake of clarity, this seems to make the most sense in an encyclopedic article. In which case the statement is provably correct. And if Sun Workshop does not accept it by default, then it's clearly not standards compliant. --User:Mellum 16:20, 18 May 2005 (UTC)
And, of course, my comment was inserted directly after the explanation of "/* */"-style comments, so it was in a relevant context.
: However, the point of that paragraph was not to give a complete explanation of comment syntax, but rather to give a brief overview of C's syntax in general; therefore, I considered these obscure details too distracting. But I don't feel very strongly about that. BTW, "//" is not from C++, but from BCPL. --User:Mellum 16:20, 18 May 2005 (UTC)
So I'm putting it back (although correcting my language as I do so). And if someone wants to go out on the limb and change it from "Many modern C compilers" to "All modern C compilers", more power to you. But you'd better be sure you've inspected all modern C compilers including some rather obscure ones!
User:Atlant 23:48, 17 May 2005 (UTC)
----
"remember, C is entirely the work of Dennis Ritchie, I am but a popularizer." (Kernighan in an interview)
Which raises the question: should we say it was "developed" by Thompson and Ritchie? What was Thompson's involvement apart from contributing the B language that C was based on? Maybe it needs a further rephrase...
User:DanielCristofani 23:31, 6 Jun 2005 (UTC)
==Feature list - Metaprogramming==
I disagree with the list of features C does not have - in that C supports Metaprogramming.
In the article on Metaprogramming, listed as an example of Metaprogramming, is the 'Quine' - "a special kind of metaprogram that has its own source as its output". There are plenty of Quines in the C language. Here's an example:
char*f="char*f=%c%s%c;main(){printf(f,34,f,34,10);}%c";main(){printf(f,34,f,34,10);}
Therefore, by at least this one example, C supports Metaprogramming. Many more are likely to be found as entries for the IOCCC.
User:Ben-Arba 07:08, Jun 18, 2005 (UTC)
:The concept of "metaprogramming" is rather difficult to define, as Talk: Metaprogramming shows. Under some loose definitions, even compilation is metaprogramming. I'd say that a C program, in general, cannot reason about or modify its own structure, except maybe with macros, and there only in a primitive sense. User:Dcoetzee 01:03, 19 Jun 2005 (UTC)
But do we have to be in the agreement whether C supports metaprogramming or not? Aside from the difficulty pointed out above, answering this question sounds rather original research. So I think we'd be better off if we simply list features of C and put some example like Ben-Arba gave in the article and let readers decide. -- User:TakuyaMurata 06:35, Jun 19, 2005 (UTC)
:Okay, that's fine, I'll remove it. User:Dcoetzee 07:46, 19 Jun 2005 (UTC)C programming languageThis category concerns the programming language called C programming language. Programming languages C programming language family See other meanings of words starting from letter: CCA | CB | CD | CE | CF | CG | CH | CI | CJ | CK | CL | CM | CN | CO | CP | CR | CS | CT | CU | CW | CX | CY | CZ |Words begining with C_programming_language: C_Programming_Language C_programming_language C_programming_language C_programming_language C_programming_language_family
Sponsored links: praca, nurkowanie.
|
These materials are based on Wikipedia and licensed under the GNU FDL
YouTube.com videos better site than Turbo Tax 2007 |
|
|