C (programming language)
C is a general-purpose, procedural computer programming language which is still in use more than thirty years after its creation. C 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. As of 2007, versions of C are still used, especially for writing operating system software and embedded programs (for gadgets such as smart phones).
The historical influence of the C programming language should not be underestimated. 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 recursively. The use of the heap means that programmers can define arbitrarily complex data types. This led to a powerful programming paradigm which is still in use today. Although powerful, the heap also needs to have memory reclaimed explicitly by the programmer after data structures are no longer needed; this error-prone process, called "garbage collection", is one of the aspects of C programming that is difficult. 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.
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#. Javascript uses similar syntax but has different scope rules. The adopted 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).
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 run faster than languages requiring a runtime or interpreter.
- 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 completely managed the way in which their program uses memory.
- C, though flexible, gives the programmer many tools which if misused can be problematic. For example, pointers can make C code very hard to debug, and errors very hard to trace. See the buffer overflow article for a longer discussion.
- The low level nature of C 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.0 1.1 Kernighan, B. & D. Ritchie (1978), The C Programming Language, Prentice Hall
- ↑ Richards, Martin (21 July 1967), Richards's BCPL Reference Manual, Memorandum M-352 of MIT Project MAC
- ↑ Thompson, Ken (7 January 1972), Users' Reference to B