Rozmiar: 8938 bajtów


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.
main()
{
    printf("hello, world\n");
}
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:
#include 

int main(void)
{
    printf("hello, world\n");

    return 0;
}
What follows is a line-by-line analysis of the above program:
#include 
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 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: * inlineinline 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!"
:This is fixed now, but some question remains about whether an image is a useful thing to have. It's displayed on the right. Dysprosia suggests, quite reasonably, that a text box might be more appropriate (see User talk:aarchiba); I think the image is useful to have for possible use on the front page and for its syntax highlighting and smaller size (also the text box does not format correctly on my machine).
#include 

int
main(void)
{
   printf("Hello World!");
   return 0;
}
A C program that prints "Hello world!"
:The text box is as shown here. --User:Aarchiba 11:15, Apr 27, 2004 (UTC) One could probably use the new/nifty table image syntax to achieve a better result, but I'll muck around with trying that later. User:Dysprosia 12:33, 27 Apr 2004 (UTC) I think the syntax highlighting is a mistake. While syntax highlighting is useful to programmers, anyone reading the article to learn about C won't know enough to benefit from the syntax highlighting, and worse, ''might mistakenly conclude that the syntax highlighting is essential to the code, or that one set of highlighting rules is standardized across the C Programming Language''. (Furthermore, I think anyone learning the language should avoid highlighting until they fully understand the various entities being highlighted.) User:Orthogonal 09:18, 15 May 2004 (UTC) :I agree, I think the syntax highlighting is distracting and possibly confusing for those unfamiliar with it. -- anonymous, 17:25, 15 May 2004 (BST) :Thirded. I am a huge fan of syntax highlighting, but I don't think it serves to help anyone understand C; one might potentially think "oh, so if I write a phrase in pink, it gets printed on the screen." -- User:Wapcaplet 16:24, 15 May 2004 (UTC) :I removed the syntax highlighting and the references to it in the explanatory text. I also removed the 'hello, world' image; seems silly to have a picture of something that can be found on the page in text. -- User:Merphant 22:02, 15 May 2004 (UTC) == Handwritten assembly is always better... == ''It should be noted, however, that for complex modern processors the machine code generated by C compilers is usually faster than hand-written assembly.'' As Robert Dewar has pointed out, that is not a meaningful statement. The human can not approach the speed with which a computer compiles code. But if there is no time limit put on the human, eventually she will produce code that is optimal. In fact, a common first step in producing hand-tuned assembly is compiling the code with a good compiler, which makes it hard to be slower than the code generated by that compiler. Any real comparision is going to involve a particular set of compilers, certain time limits and certain programmers (some of the published comparisons involve undergrads, which is certainly different from results using RMS and Mel), and is not going to be useful much beyond that. --User:Prosfilaes 01:40, 31 May 2004 (UTC) Well, ''better'' is a tricky term. I think you have actually advanced an argument that a human, under the right circumstances, can make (a little bit of) a program run faster than the compiler can. This is surely true. One old joke was that you could always take out 10% (it just kept getting harder to do). On the other hand, there is a famous cautionary tale, I can not recall the source at the moment, about an "optimization" that did not get called for years. There is another school of thought which says that people can better spend their (valuable) time thinking about the choice of algorithm and overall design, and that the same effort spent in this way usually results in much larger payoffs in terms of speed.User:AJim 05:56, 31 May 2004 (UTC) I think they meant faster as in faster speed in execution, not writing. User:Dysprosia 05:59, 31 May 2004 (UTC) I also think they meant speed of execution. What I meant to say was that an hour invested in working on the overall design was likely to yield more of a speedup in execution than an hour invested in trying to write better machine code than the compiler. It is well known that given the same problem to solve, a group of programmers will sometimes produce programs with an enormous range of execution times. We are talking orders of magnitude here, not a few percent. Experienced programmers usually do much better than novices in terms of speed. What distinguishes the faster programs is not faster low-level optimizations, but faster algorithms. User:AJim 16:11, 1 Jun 2004 (UTC) == confusion about arrays -- should we describe a rarely-used "feature", or a common work-around ? == Think I disagree with this sentence: :The arrays can appear to have more than one dimension; dimensions are laid out in row-major order (stricly speaking, they are arrays of arrays). I believe it could be :Two-dimensional arrays can be implemented, for example, as ''arrays of pointers to arrays of pointers'', with higher-dimensioned arrays defined analogously: ''arrays of pointers to arrays of pointers to arrays of pointers'', etc. -- anonymous :: This statement used to make sense. Some ill-informed contributor made it confusing. Static multidimensional arrays are in fact, single-dimensional in reality, and laid out in row-major order, but they can be implemented as arrays of arrays. I'll try to fix it. User:Dcoetzee 20:59, 10 Jul 2004 (UTC) Summary On one hand, I want to say your proposed replacement is incorrect -- the original correctly describes 2 dimensional static arrays (and higher dimensions), while your replacement does not. On the other hand, static arrays of more than 1 dimension are very rarely used in real programs. Nearly everyone builds higher-dimensional arrays out of pointers and (1D) arrays, as dynamic arrays, in exactly the way your proposed replacement describes. (A few people use "sparse arrays", which are dynamically built out of pointers and 1D arrays in a completely different way). Details The data structure that C creates when the source code says double NewYork [10][10] = {0.0}; (a 2 dimensional static array) is simply a block of 100 double variables packed one right after the next; there is no array of pointers. The handling of 2 dimensional static arrays in C is widely believed to be seriously flawed; see the C FAQ: * http://www.eskimo.com/~scs/C-faq/q6.15.html * http://www.eskimo.com/~scs/C-faq/q6.19.html * http://www.eskimo.com/~scs/C-faq/q6.21.html ) "Rather than worrying about all of this, one approach to using multidimensional arrays of various sizes is to make them all dynamic" -- http://www.eskimo.com/~scs/C-faq/q6.20.html So when you want what mathematicians would call a 2D array of real numbers, most people end up using a (1D) array of pointers to (1D) arrays of double. We end up using an "array of pointers to arrays of pointers to arrays of double" for 3D arrays. That's all described in http://www.eskimo.com/~scs/C-faq/q6.16.html . Because it's a bit of a hassle to do this for every array, many people wrap this all up in a matrix-manipulation library -- and unfortunately, there are tons of incompatible libraries for manipulating matrices in C. --User:DavidCary 21:47, 10 Jul 2004 (UTC) : You make a strong case; however, the issue is not that I consider static arrays more important, but that this is the ''Types'' section being written in, and dynamic arrays are not a type supported at the language level. It certainly deserves mention, and perhaps reference from this section, but not ''in'' this section, which I consider to be reserved for language-supported type constructs. User:Dcoetzee 18:31, 11 Jul 2004 (UTC) User:Wrp103 I'm the person who inserted the comment about multi-dimensional arrays being arrays of arrays. According to K&R section 5.7 Multi-Dimensional Arrays (I have the original here at work, so the updated version for ANSII C might have changed), when explaining the array day_tab, they state: a two-dimensional array is really a one-dimensional array, each of whose elements is an array. Hence subscripts are written as 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 int main() { int a[2][2] = {{1,2},{3,4}}; int* b = &a[0][0]; int i; for(i=0; i<4; i++) printf("%d", b[i]); return 0; } : User:Dcoetzee 02:39, 13 Jul 2004 (UTC) :: Actually, I wasn't talking about dynamically allocated multi-dimensional arrays. I was talking about the fact that in C, there aren't really multi-dimensional arrays, but rather arrays of arrays, which produce the same effect. ::I do a lot of dynamic arrays (and in fact created an ADT that is available from my web site), but only single dimension arrays. Generally, I create a dynamic array of pointers to structures, so I haven't ever bothered to try multi-dimensional arrays. I have always felt that multi-dimensional arrays were mostly useful for matrix algebra and other math applications, not general data structures. ::You are correct that C will allocate a single block of code, and map the coordinates to the proper memory location. That's why I didn't understand what was meant by metadata. :: User:Wrp103 18:12, 13 Jul 2004 (UTC) As for allocating multi-dimensional arrays, I had never heard of anyone doing it the way you describe, although it is no doubt possible. I'm not sure what you meant by metadata, however, since an array is simply a block of memory of the necessary size to store the data. In the case of a multi-dimensional array, each element would be a pointer to the second array. BTW - I almost never use multi-dimensional arrays, and encourage my students to use arrays of structures instead. IMHO, they are a carry-over from when we didn't have data structures, and would use multi-dimensional arrays where the rows were for a given entity, and the columns were the fields of that entity. :It's true that multidimensional arrays were used for this purpose, but this was never the intended purpose nor even a good idea. Static multidimensional arrays were more useful in numerical computational of the sort normally done with Fortran. User:Dcoetzee 02:39, 13 Jul 2004 (UTC) BTW2 - I admit I'm an old fogey, but I really object to adding the comment lines for the hello, world application. Actually, I don't care for the return 0 being added for that matter. The beauty of that programming example was in its simplicity. The fact that the program is well explained immediately below doesn't (IMHO) the addition. : return 0 is necessary. main returns an int, and leaving it off is sloppy. User:Dysprosia 23:43, 12 Jul 2004 (UTC) ::Actually, in C99 this is not true anymore. To quote the standard: :::5.1.2.2.3 Program termination :::1 If the return type of the main function is a type compatible with int, a return from the :::initial call to the main function is equivalent to calling the exit function with the value :::returned by the main function as its argument;10) reaching the } that terminates the :::main function returns a value of 0. If the return type is not compatible with int, the :::termination status returned to the host environment is unspecified. :::Forward references: definition of terms (7.1.1), the exit function (7.20.4.3). ::User:Lady Lysine Ikinsile | User talk:Lady Lysine Ikinsile 07:54, 2004 Jul 13 (UTC) :: My comment on the return 0 was because it wasn't included in the original "hello, world" program. :: User:Wrp103 18:12, 13 Jul 2004 (UTC) : I agree that the comments are misplaced; explanation belongs in the text. The ''return 0'' is important, not so much because of what's allowed, but because it helps demonstrate the procedural paradigm in a very simple setting. User:Dcoetzee 02:39, 13 Jul 2004 (UTC) I think part of the confusion that arose from my original edits were because I made two unrelated changes that were perceived as related. I commented on the nature of multi-dimensional arrays in C, and I added an example of how to create single-dimension dynamic arrays in C. I think somebody assumed I thought they were related, which is what started this whole thread. I almost never use multi-dimensional arrays, and never even considered trying dynamic multi-dimensional arrays. However, I have noticed that many of the C text books I have used for various classes claim that you can't have dynamic arrays in C, which of course is wrong. So, when I'm teaching C, I make it a point to mention those two things. Sorry for any confusion. I'm still getting used to this sytem. User:Wrp103 18:27, 13 Jul 2004 (UTC) User:Wrp103 16:17, 5 Aug 2004 (UTC) I noticed that somebody deleted my example of a constructor, and replaced calls to malloc followed by clearing memory to calloc. The purpose of the constructor is to initialize the data structure to default/valid values; my example of zeroing the structure was for simplicity. BTW - NULL isn't always all zeros (I recall working on a mainframe where this messed up a lot of programs that used things like "if (ptr)", assuming NULL would return False.) :That was me. Yes, I'm aware that (although NULL is always 0), the value of a NULL value might not be 0. However, I think I only removed the code which called memset() with '\0' as the argument, which is not compatible with systems where null isn't 0 anyway, and is basically identical to what calloc does. The code which explicitly assigns ... = NULL was left. :In any case I like the new (useful) example of a C ctor. User:KateUser:Kate | User talk:Kate 16:23, 2004 Aug 5 (UTC) (Christopher Thompson added the next bit) The following makes no sense: In C, sizeof('a') == sizeof(int); In C, sizeof('a') == sizeof(char) (For the record this has been [http://en.wikipedia.org/w/wiki.phtml?title=C_programming_language&diff=5621022&oldid=5620961 corrected] now. User:Cyoung 04:12, 23 Sep 2004 (UTC)) :Wrp103, the statement you make above: ''BTW - NULL isn't always all zeros (I recall working on a mainframe where this messed up a lot of programs that used things like "if (ptr)", assuming NULL would return False.)'' :While it is true that NULL isn't always all zeros, "if (ptr)" will ALWAYS return false if ptr is NULL, since the compiler will recognise that ptr is a pointer, and compare it against whatever bit pattern is used to represent NULL on the architecture. This means that if (!p) and if (p == NULL) and if (p == 0) are all equivalent and correct, regardless of what bit pattern is used to represent NULL. See [http://www.eskimo.com/~scs/C-faq/q5.3.html Steve Summit's comp.lang.c FAQ Q5.3] User:Cyoung 04:12, 23 Sep 2004 (UTC) == Array of structs? == In all honesty I find the example of converting a multidimensional array to an array of structs not only unnecessarily long and drawn out, but also misled. Multidimensional arrays should never be used as arrays of records; this may or may not be obvious, but in any case, multidimensional arrays have perfectly legitimate uses, such as representing matrices, that have nothing to do with structs, and it was for these purposes that they were designed. Much of the material in the example perhaps should be in the article somewhere, or some other article, but its current placement isn't useful. In particular, constructors and destructors as used in C are a practice, and are not relevent to the C language ''per se''. The same is true of the previous content I added related to creating dynamic arrays, which I admit now is misplaced. There are dozens of mentionable practices in C, including placing constants on the left side of equality comparisons, Duff's device, arrays that extend off the end of records, setting pointers to deallocated objects to NULL, parenthesizing macro arguments, and so on. In short, if we are really to tabulate C practices, this should either be done in a separate section, or eventually, a separate article. Let storage talk about the basics of storage as built in to the language itself. Keep common practice cleanly separated from core language features. I am prepared to make this change. Is there any opposition? User:Dcoetzee 04:08, 31 Oct 2004 (UTC) :I went ahead and created the section, entitled C programming language#Common Practices, and I moved my bit on allocating dynamic arrays there. If there is no objection, I will remove the code on converting multidimensional arrays to arrays of structures, and insert a section on constructors and destructors into the new section. User:Dcoetzee 05:04, 31 Oct 2004 (UTC) ::With no objections thus far, this change has been performed. If objections later arise, feel free to edit or revert and discuss. User:Dcoetzee 01:37, 1 Nov 2004 (UTC) : The contents of Common Practices section are fine, but I think that perhaps they better belong in someplace like [http://en.wikibooks.org/wiki/Programming:C wikibook's Programming:C] tutorial, rather than in the main encyclopedia article. The section is no longer describing the language, but various people's preferred use of the language... - User:Key45 19:15, 17 Nov 2004 (UTC) :: I agree. I introduced this section mainly as a compromise (in order to get that content extracted from other sections without upsetting anyone), and don't really think it belongs in an article about the language. User:Dcoetzee 19:19, 17 Nov 2004 (UTC) ::: This section now lives in its new home at [http://en.wikibooks.org/wiki/Programming:C_common_practices Wikibooks' C common practices]. I invite those interested in this section to contribute to it at its new location. User:Dcoetzee 10:14, 16 Jan 2005 (UTC) == C90 == C90 redirects here. But "C90" isn't anywhere on this article. User:Brianjd 09:28, 2004 Nov 29 (UTC) :C90 is an informal term for the C language as described by the 1990 international ISO standard. C89, similarly, describes the ANSI 1989 standard, which is almost identical. They're not mentioned here mainly because they are informal and not totally universal, although the standards are described. User:Dcoetzee 22:11, 15 Dec 2004 (UTC) == large programs == This is no accident; C was created with one important goal in mind: to make it easier to write large programs with ... If we mean it's original creation circa 1972, this is certainly not the case. C was at that stage for a machine with max program size 12K, there was no way you could write a large program in C. Rather, C was created so they would have something slightly nicer and more portable than assembler to use on a machine that was too small to fit almost any other language then in existence. Some support for large programs emerged later, but really large programs have never been C's strength. It shines for small to medium sized, fast, close to the metal stuff. :It's a matter of what you mean by "large." C was an important early structural programming language, and structural programming enables the creation of more modular and organized and so larger programs. Its type system also facilitates maintenance of larger programs that are difficult in assembly. Even a program of a few K in code bytes is quite an undertaking in assembly, while something that could be written as a small assembly program really doesn't require a higher-level language like C (assuming porting it isn't a big effort.) Maybe this isn't reflected in the text, but I'm not sure how to word it. User:Dcoetzee 22:06, 15 Dec 2004 (UTC) == far too many external links == There are far too many external links, some are for rather unimportant programs, almost seems like a bit of advertising or fanfare has slipped in here. I'm taking a bush-whacker to it, but feel free to add anything back that seems critical. User:Daniel Quinlan 08:39, Feb 10, 2005 (UTC) :I agree, but think some of these should later reappear in articles about "C tools" and so on. User:Dcoetzee 19:28, 10 Feb 2005 (UTC) == Array changes == For what it's worth, dynamic arrays are not really supported by the C language proper. Arrays whose size are known at runtime can be allocated using the standard library, but these do not have array ''types'' — they are assigned to pointer variables and merely treated syntactically in a manner similar to arrays. This is the original reason I originally omitted them. I realise it seems counterintuitive to say C doesn't have dynamic arrays, but really it's something you ''implement'' in C, not something C gives you. This is why I've softened the language of this part. Oh, and multidimensional arrays are ''not'' arrays of arrays. This is just syntax — they're laid out in row-major order, and the index is computed using multiplication, not obtained by successive pointer fetching as in a ''true'' array of arrays. I've attempted to clarify this. User:Dcoetzee 04:45, 6 Apr 2005 (UTC) :We've discussed this before, and again I refer you to K&R, which specifically states that C doesn't have multidimensional arrays, but rather arrays of arrays. As for dynamic arrays in C, the reason I keep bringing it up is because my students keep refering to books & articles claim you can't have dynamic arrays in C. If you want to call it something else, that's fine, but I think it is important that people are aware that they can implement dynamic arrays in C. User:Wrp103 - User talk:Wrp103 03:49, 7 Apr 2005 (UTC) ::I guess you're right. Semantically, they are arrays of arrays, even if they're not necessarily stored in that manner. I agree with respect to dynamic arrays (let's mention it like we are now but just not use that term). User:Dcoetzee 01:22, 9 Apr 2005 (UTC) I think there are a lot of areas of overlap between the C language, C syntax, and C standard library articles. I do think that this needs to be resolved.—User:Kbolino 04:20, Apr 7, 2005 (UTC) == Text added to C syntax section == Before anyone complains (or reverts me), I wanted to explain the text I added to the C syntax section — the idea is to provide a supercondensed summary for those who don't want to dive into all the details of the full article. This is a very common thing to do wherever you see a ''main article'' note, and I think it's appropriate here, although the summary is necessary technical and missing lots of information. If they need to know more they can visit the main article. User:Dcoetzee 04:11, 18 Apr 2005 (UTC) : Oy! You removed the comment bit that I'd just rewritten. I agree with your changes b.t.w. -- it should be in syntax, but you coulda checked if it was there first! Never mind, I moved it myself. I still think the main page is far too heavyweight and detailed. Most of the information should be moved to separate articles, with just very brief coverage on the main page. User:Akihabara 08:22, 18 Apr 2005 (UTC) ==C: High, middle, or low-level language == Okay, I can see we're seting up for a revert war here with the question of whether C is a high-level language, low-level language, or something in-between. Personally, I've always thought of C as one step above assembler, so it's a pretty low-level language to me. (It's got no inherent matrix operations, no string operations, no garbage collection, etc. You get to manage memory all by your lonesome self. Pointers are a nearly-direct representation of what PDP-11s and VAX computers do in single machine-language instructions.) And other text in the article supports this assertion. But I'd like to hear what others have to say before changing this again. User:Atlant 12:06, 17 May 2005 (UTC) :I want to say "medium"; "low-level" as I've heard it used has a strong connotation of "assembler or machine language", but C is certainly the lowest thing that has been called a high-level language. User:DanielCristofani 14:10, 17 May 2005 (UTC) ::Well, compared to languages that are being designed today, C is relatively low-level, but I think the historical precedent has been to describe C as high-level. In contrast, languages like Python are often called "very-high level languages". In any case I don't think we should use the term "medium-level language", as that is not at all standard usage (googling for "high-level language C" yields more than 5.5 million hits; googling for "medium-level language C" yields fewer than than 35,000). Therefore, given the choice between high- and low-level, I think HLL is a better description of C. User:Neilc 03:39, 27 May 2005 (UTC) :::What would happen if we didn't use any of these phrases (at least prominently near the start of the article)? If they don't have clear, agreed-upon meanings even among people knowledgeable about programming languages, I doubt they're much use to the less-informed readers the article is written for. I'm going to remove the phrase and see if anyone objects. User:DanielCristofani 04:47, 27 May 2005 (UTC) ::::A link to medium-level programming language somewhere in the article is necessary, as it describes many important characteristics of C and other C-level languages. It doesn't have to be on the top. The terms high-level and low-level are historically pretty well defined (at least better than some like "object-oriented" or "structured"), and C is a canonical example of neither, but a borderline case. Hence the invention of term "medium-level" (or "portable assembler" / "high-level assembler" etc.). And I didn't know about the 17 May revert when I did the change, no revertwarish intentions here, just a coincidence. User:Taw 11:55, 27 May 2005 (UTC) :::::I've just made "low level" and "medium level", down in the first paragraph of "Overview", into links to the appropriate articles. User:DanielCristofani 03:50, 28 May 2005 (UTC) ::::::C is not "typically" called a "low level language". It is at ''least'' a medium-level language (I would still argue it is a high-level language according to historical precedent). User:Neilc 05:24, 28 May 2005 (UTC) :::::::I've tried to rephrase it; it's a little awkward and maybe the paragraph now needs rewriting but it should probably say the same things it now says and should include all three links... User:DanielCristofani 10:19, 28 May 2005 (UTC) I certainly wouldn't object to the lede not trying to characterize the "level" of C. User:Atlant 11:35, 27 May 2005 (UTC) The fact is, these "levels" are too ill-defined and inconsistent in their usage to characterize the level of nearly any language, especially one lying near the boundary like C. Better might be to give comparisons; I think we can all agree it's higher-level than assembly, but lower-level than Ada, Smalltalk, Java, or Haskell. Or we might just say, "some call it X because Y" for each one — just don't go on about it in an inappropriate place like the intro paragraph. User:Dcoetzee 09:54, 28 May 2005 (UTC) ==Notes on small changes.== "method" is an unusual word to use for C functions. "The 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 language



This category concerns the programming language called C programming language. Programming languages C programming language family


See other meanings of words starting from letter:

C

CA | 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


These materials are based on Wikipedia and licensed under the GNU FDL



YouTube.com videos better site than Turbo Tax 2007
encyklopedia online