Search This Blog

Tuesday, 22 October 2013

C

The C programming language was developed in the early 1970s by Dennis Ritchie, who based it on the earlier lan-guages BCPL and B. C was first used on DEC PDP-11 computers running the newly developed UNIX operating system, where the language provided a high-level alterna-tive to the use of PDP Assembly language for develop-ment of the many utilities that give UNIX its flexibility. Since the 1980s, C and its descendent, C++, have become the most widely used programming languages.

Language Features

Like the earlier Algol and the somewhat later Pascal, C is a procedural language that reflects the philosophy of programming that was gradually taking shape during the 1970s (see structured programming). In general, C’s approach can be described as providing the neces-sary features for real world computing in a compact and efficient form. The language provides the basic control structures such as if and switch (see branching state-ments) and while, do, and for (see loop). The built-in data types provide for integers (int, short, and long), floating-point numbers (float and double), and characters (char). An array of any type can be declared, and a string is implemented as an array of char (see data types and characters and strings).

Pointers (references to memory locations) are used for a variety of purposes, such as for storing and retrieving data in an array (see pointers and indirection). While the use of pointers can be a bit difficult for beginners to under-stand, it reflects C’s emphasis as a systems programming language that can “get close to the hardware” in manipulat-ing memory.

Data of different types can be combined into a record type called a struct. Thus, for example:
struct Employee_Record { char [10] First_Name; char [1] Middle_Initial; char [20] Last_Name; int Employee_Number;

} ;

(There is also a union, which is a struct where the same structure can contain one of two different data items.)

The standard mathematical and logical comparison operators are available. There are a couple of quirks: the equals comparison operator is = =, while a single equal sign = is an assignment operator. This can create a pitfall for the wary, since the condition

if (Total = 10)

printf (“Finished!”);

always prints Finished, since the assignment Total = 10 returns a value of 10 (which not being zero, is “true” and satisfies the if condition).

C also features an increment ++ and decrement - - oper-ator, which is convenient for the common operation of rais-ing or lowering a variable by one in a counting loop. In C the following statements are equivalent:
Total = Total + 1;

Total += 1;

Total ++;

Unlike Pascal’s two separate kinds of procedures (func, or function, which returns a value, and proc, or proce-dure, which does not), C has only functions. Arguments are passed to functions by value, but can be passed by reference by using a pointer. (See procedures and functions.)

Sample Program

The following is a brief example program:

#include <stdio.h> float Average (void); main () {

printf (“The average is: %f”, Average() );

}

float Average (void) { int NumbersRead = 0; int Number;

int Total = 0;

while (scanf(“%d\n”, &Number) == 1)

{

Total = Total + Number; NumbersRead = NumbersRead + 1;
}

return (Total / NumbersRead);

}

}

Statements at the beginning of the program that begin with # are preprocessor directives. These make changes to the source code before it is compiled. The #include directive adds the specified source file to the program. Unlike many other languages, the C language itself does not include many basic functions, such as input/output (I/O) state-ments. Instead, these are provided in standard libraries. (The purpose of this arrangement is to keep the language itself simple and portable while keeping the implementa-tion of functions likely to vary on different platforms sepa-rate.) The stdio.h file here is a “header file” that defines the I/O functions, such as printf() (which prints formatted data) and scanf() (which reads data into the program and formats it).

The next part of the program declares any functions that will be defined and used in the program (in this case, there is only one function, Average). The function declaration begins with the type of data that will be returned by the function to the calling statement (a floating point value in this case). After the function name comes declarations for any parameters that are to be passed to the function by the caller. Since the Average function will get its data from user input rather than the calling statement, the value (void) is used as the parameter.

Following the declaration of Average comes the main() function. Every C program must have a main function. Main is the function that runs when the program begins to exe-cute. Typically, main will call a number of other functions to perform the necessary tasks. Here main calls Average

within the printf statement, which will print the average as returned by that function. (Calling functions within other statements is an example of C’s concise syntax.)

Finally, the Average function is defined. It uses a loop to read in the data numbers, which are totaled and then divided to get the average, which is sent back to the calling statement by the return statement.

A programmer could create this program on a UNIX system by typing the code into a source file (test.c in this case) using a text editor such as vi. A C compiler (gcc in this case) is then given the source code. The source code is compiled, and linked, creating the executable program file a.out. Typing that name at the command prompt runs the program, which asks for and averages the numbers.


% gcc test.c 

% a.out 

5

7

9

.

The average is: 7.000000

Success and Change

In the three decades after its first appearance, C became one of the most successful programming languages in history. In addition to becoming the language of choice for most UNIX programming, as microcomputers became capable of running high-level languages, C became the language of choice for developing MS-DOS, Windows, and Macintosh programs. The application programming interface (API) for Windows, for example, consists of hundreds of C functions, structures, and definitions (see application programming interface and Microsoft Windows).

However, C has not been without its critics among computer scientists. Besides containing idioms that can encourage cryptic coding, the original version of C (as defined in Kernighan and Ritchie’s The C Programming Language) did not check function parameters to make sure they matched the data types expected in the func-tion definitions. This problem led to a large number of hard-to-catch bugs. However, the development of ANSI standard C with its stricter requirements, as well as type checking built into compilers has considerably amelio-rated this problem. At about the same time, C++ became available as an object-oriented extension and partial rec-tification of C. While C++ and Java have considerably supplanted C for developing new programs, C program-mers have a relatively easy learning path to the newer languages and the extensive legacy of C code will remain useful for years to come.




No comments:

Post a Comment