This section contains a brief introduction to the C language. It is intended as a tutorial on the language, and aims at getting a reader new to C started as quickly as possible. It is certainly not intended as a substitute for any of the numerous textbooks on C.
The best way to learn a new ``human'' language is to speak it right from the outset, listening and repeating, leaving the intricacies of the grammar for later. The same applies to computer languages--to learn C, we must start writing C programs as quickly as possible.
An excellent textbook on C by two well-known and widely respected authors is:
The C Programming Language -- ANSI C
Brian W. C. Kernighan & Dennis M. Ritchie
Prentice Hall, 1988
Dennis Ritchie designed and implemented the first C compiler on a PDP-11 (a prehistoric machine by today's standards, yet one which had enormous influence on modern scientific computation). The C language was based on two (now defunct) languages: BCPL, written by Martin Richards, and B, written by Ken Thompson in 1970 for the first UNIX system on a PDP-7. The original ``official'' C language was the ``K & R'' C, the nickname coming from the names of the two authors of the original ``The C Programming Language''. In 1988, the American National Standards Institute (ANSI) adopted a ``new and improved'' version of C, known today as ``ANSI C''. This is the version described in the current edition of ``The C Programming Language -- ANSI C''. The ANSI version contains many revisions to the syntax and the internal workings of the language, the major ones being improved calling syntax for procedures and standarization of most (but, unfortunately, not quite all!) system libraries.
Let's be polite and start by saluting the world! Type the following program into your favorite editor:
#include < stdio.h>
void main()
{
printf("\nHello World\n");
}
gcc hello.c
This creates an executable file a.out, which is then executed simply by typing its name. The result is that the characters `` Hello World'' are printed out, preceded by an empty line.
A C program contains functions and variables. The functions specify the tasks to be performed by the program. The ``main'' function establishes the overall logic of the code. It is normally kept short and calls different functions to perform the necessary sub-tasks. All C codes must have a ``main'' function.
Our hello.c code calls printf, an output function from the I/O (input/output) library (defined in the file stdio.h). The original C language did not have any built-in I/O statements whatsoever. Nor did it have much arithmetic functionality. The original language was really not intended for ''scientific'' or ''technical'' computation.. These functions are now performed by standard libraries, which are now part of ANSI C. The K & R textbook lists the content of these and other standard libraries in an appendix.
The printf line prints the message ``Hello World'' on ``stdout'' (the output stream corresponding to the X-terminal window in which you run the code); ``\n'' prints a ``new line'' character, which brings the cursor onto the next line. By construction, printf never inserts this character on its own: the following program would produce the same result:
#include < stdio.h>
void main()
{
printf("\n");
printf("Hello World");
printf("\n");
}
Try leaving out the ``\n'' lines and see what happens.
The first statement ``#include < stdio.h>'' includes a specification of the C I/O library. All variables in C must be explicitly defined before use: the ``.h'' files are by convention ``header files'' which contain definitions of variables and functions necessary for the functioning of a program, whether it be in a user-written section of code, or as part of the standard C libaries. The directive ``#include'' tells the C compiler to insert the contents of the specified file at that point in the code. The ``< ...>'' notation instructs the compiler to look for the file in certain ``standard'' system directories.
The void preceeding ``main'' indicates that main is of ``void'' type--that is, it has no type associated with it, meaning that it cannot return a result on execution.
The ``;'' denotes the end of a statement. Blocks of statements are put in braces {...}, as in the definition of functions. All C statements are defined in free format, i.e., with no specified layout or column assignment. Whitespace (tabs or spaces) is never significant, except inside quotes as part of a character string. The following program would produce exactly the same result as our earlier example:
#include < stdio.h>
void main(){printf("\nHello World\n");}
The reasons for arranging your programs in lines and indenting to show structure should be obvious!
Nema komentara:
Objavi komentar