C (programming language)

From Citizendium
Revision as of 02:46, 18 March 2010 by imported>Pat Palmer (→‎Pros & Cons of C)
Jump to navigation Jump to search
This article is developing and not approved.
Main Article
Discussion
Related Articles  [?]
Bibliography  [?]
External Links  [?]
Citable Version  [?]
Tutorials [?]
 
This editable Main Article is under development and subject to a disclaimer.
For other uses, see C (disambiguation).

C is a general-purpose, procedural computer programming language which is still in use nearly forty years after its creation. It was developed in 1972 by Dennis Ritchie and Brian Kernighan (then of Bell Laboratories) for use with the Unix operating system, and, before it entered the standards process, in a reference book.[1] The language has been implemented for many different computer platforms and eventually became standardized by ANSI and ISO.

Although superseded by more modern languages for general application programming, as of 2009 versions of C are still used, primarily for writing operating system software and embedded programs (for gadgets such as smart phones). C and its closely related sister language, C++, are also used for games development and other graphics- or media-intensive programming. Although once used for web programming, it has been superseded for web programming by newer languages that provide more security and which enforce safer programming practices.

Historical influence of C

The C programming language set several important precedents which were adopted by many subsequent programming languages. At the time of its initial implementation, most or all programming tools did not allow recursive function calls. C, in conjunction with UNIX, takes advantage of two run-time memory-management strategies: the call stack (used by the compiler to keep track of local variables declared in functions and subroutines) and the heap (used explicitly by the programmer to pack arbitrary data structures into memory). The use of the stack meant that C allows functions to call themselves (so-called recursion); certain simple problems may be elegantly solved using a recursive function.

The use of the heap means that programmers can define arbitrarily complex data types. However, the C heap also needs to have memory reclaimed explicitly by the programmer after data structures are no longer needed. In longer programs, reclaiming heap memory, also called garbage collection, is one of the aspects of C programming that is difficult and error prone. Failure to correctly reclaim all no-longer-needed memory from the heap eventually results in a program running out of memory, a situation called a memory leak. More modern languages such as Java and C# handled reclamation of no-longer-used heap space automatically, resulting generally in more reliable programs.

The stack and heap resulted in a powerful programming paradigm which is still in widespread use today. Today, most computer architectures, operating systems, and compilers provide special capabilities facilitating the use of stacks and heaps by programming languages such as C. For example, operating systems typically contain calls to "allocate an initial heap" and then, later, to "grow the heap" using vacant memory if the current heap has filled up; however, such calls will eventually fail if heap space is not being reclaimed properly.

C's name derives from those of two of its predecessors (also worked on by the same authors at Bell Laboratories), Basic Computer Programming Language (BCPL)[2], and a BCPL variant,B[3].

Syntax of the C family of programming languages

The syntax and stack-based scope behavior of C were also adopted by several later programming languages, including C++, Java, and C#. The adopted syntax characteristics tend to include case sensitivity, ending of statements with a semi-colon (;), use of { } to enclose blocks of code, enclosing procedure parameters inside ( ) pairs, and allowing temporary variables to be declared inside blocks or procedures (which are semantically destroyed subsequent to that procedure's execution, and reinitialized from scratch during subsequent procedure executions).

Javascript uses similar syntax but has different scope rules. Quite a few other programming languages adopt many of the same syntax rules (such as semi-colon after statements, and curly braces to enclose code blocks) but have significant semantic differences. Languages with a C-like syntax include Java and C#.

Hello World

#include <stdio.h>

int main(void) {
   printf("Hello, world!\n");
   return 0;
}

Analysis of the example

The Hello World program (see above) appears in many programming languages books and articles as a cursory introduction into a language's syntax. It was introduced in the book The C Programming Language[1].

#include <stdio.h> tells the precompiler to include the contents of the header file stdio.h, which declares standard input and output functions into the program before compiling.

int main(void) { tells the compiler that there is a function named main which expects no parameters (void) and will return an integer number to the caller (int). Due to a standard convention of the language, main is the first function called after the execution environment of the program has been set up. The opening curly brace following int main(void) denotes the beginning of the function.

printf("Hello, world!\n"); will make the program output Hello, world! and a new line (\n) on the screen. printf is itself a function similar to main but predefined in a library (libc) and linked into the program at compile time or runtime. The trailing semicolon is the end of statement marker in C.

return 0; defines the value to be returned from main and leaves the function back to its caller, some standard C startup code. After some additional cleanup that code will pass the 0 on to the operating system, to which it means 'success'.

} signals the end of the function definition to the compiler.

Pros & Cons of C

Pros

  • Although C programs can either be compiled or run through an interpreter, most are compiled and run as native code. Compiled C programs tend to start running faster than languages requiring a runtime or interpreter; however, modern runtime-based languages such as Java, C# or VB.NET now run about as fast as native code once they have "warmed up" (i.e., once they have been just-in-time compiled and loaded into memory).
  • C is low-level enough to take advantage of hardware-specific capabilities, and thus any low-level software packages that have close interaction with hardware are written in C. For example, most of Unix is written in C. Most compilers and interpreters are written in C. C gives programmers access to hardware, enabling them to manipulate memory. This feature is advantageous but can prove problematic if misused.
  • C has been standardized, which minimizes differences across compilers, but it is still hardware-dependent. For example, the storage width of integers depends on the underlying hardware and may differ across two different hardware architectures. C programs can be made portable across multiples systems, but only with some diligence on the part of the programmer. C compilers are available for most systems.

Cons

  • C programmers must explicitly manage how their program uses heap memory. Although allowing great flexibility, pointers (addresses of data structures in the heap) can make C code very hard to understand and debug. Forgetting to deallocate used memory after it is no longer needed will cause programs to crash after they have been running for awhile (because the heap fills up).
  • Strings in C are implemented as special data structures. Parts of the extensive string-handling libraries in C do not always enforce correct memory management practices. Programmer errors may go undetected at compile-time and cause crashes or other problems at runtime. Programmer errors may also make a program vulnerable to malicious exploitation of the entire computer system. See the buffer overflow article for further discussion and some examples.
  • The low level nature of C, which does not hide specific hardware architectural details, makes it relatively harder to learn than other programming languages.
  • Even with programmer diligence, it is still difficult to guarantee that a C program which runs on one type of hardware will also run identically on different hardware without modification.

See also

References

  1. 1.0 1.1 Kernighan, B. & D. Ritchie (1978), The C Programming Language, Prentice Hall
  2. Richards, Martin (21 July 1967), Richards's BCPL Reference Manual, Memorandum M-352 of MIT Project MAC
  3. Thompson, Ken (7 January 1972), Users' Reference to B