A First Program
Let's be polite and start by saluting the world! Type the following program into your favorite :Save the code in the file hello.c, then compile it by typing:
#include <>
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:
Try leaving out the ``\n'' lines and see what happens.
#include <>
void main()
{
printf("\n");
printf("Hello World");
printf("\n");
}
The first statement ``#include <>'' 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 <>
void main(){printf("\nHello World\n");}
The reasons for arranging your programs in lines and indenting to show structure should be obvious!
1. A First Program
2. Let's Compute
3. Loops
4. Symbolic Constants
5. Conditionals
6. Pointers
7. Arrays
8. Character Arrays
9. I/O Capabilities
10. Functions
11. Command-line Arguments
12. Graphical Interfaces: Dialog Boxes
No comments:
Post a Comment