- Compiling Your Program
- Running Your Program
- Understanding Your First Program
- Displaying the Values of Variables
- Comments
- Exercises
Understanding Your First Program
Take a closer look at your first program. The first line of the program
#include <stdio.h>
should be included at the beginning of just about every program you write. It tells the compiler information about the printf output routine that is used later in the program. Chapter 13, "The Preprocessor," discusses in detail what this line does.
The line of the program that reads
int main (void)
informs the system that the name of the program is main, and that it returns an integer value, which is abbreviated "int." main is a special name that indicates precisely where the program is to begin execution. The open and close parentheses immediately following main specify that main is the name of a function. The keyword void that is enclosed in the parentheses specifies that the function main takes no arguments (that is, it is void of arguments). These concepts are explained in great detail in Chapter 8, "Working with Functions."
Now that you have identified main to the system, you are ready to specify precisely what this routine is to perform. This is done by enclosing all program statements of the routine within a pair of curly braces. All program statements included between the braces are taken as part of the main routine by the system. In Program 3.1, you have only two such statements. The first statement specifies that a routine named printf is to be invoked or called. The parameter or argument to be passed to the printf routine is the string of characters
"Programming is fun.\n"
The printf routine is a function in the C library that simply prints or displays its argument (or arguments, as you will see shortly) on your screen. The last two characters in the string, namely the backslash (\) and the letter n, are known collectively as the newline character. A newline character tells the system to do precisely what its name impliesthat is, go to a new line. Any characters to be printed after the newline character then appear on the next line of the display. In fact, the newline character is similar in concept to the carriage return key on a typewriter. (Remember those?)
All program statements in C must be terminated by a semicolon (;). This is the reason for the semicolon that appears immediately following the closing parenthesis of the printf call.
The last statement in main that reads
return 0;
says to finish execution of main, and return to the system a status value of 0. You can use any integer here. Zero is used by convention to indicate that the program completed successfullythat is, without running into any errors. Different numbers can be used to indicate different types of error conditions that occurred (such as a file not being found). This exit status can be tested by other programs (such as the Unix shell) to see whether the program ran successfully.
Now that you've finished analyzing your first program, you can modify it to also display the phrase "And programming in C is even more fun." This can be done by the simple addition of another call to the printf routine, as shown in Program 3.2. Remember that every C program statement must be terminated by a semicolon.
Program 3.2
#include <stdio.h> int main (void) { printf ("Programming is fun.\n"); printf ("And programming in C is even more fun.\n"); return 0; }
If you type in Program 3.2 and then compile and execute it, you can expect the following output in your program's output window, sometimes called the "console."
Program 3.2 Output
Programming is fun. And programming in C is even more fun.
As you will see from the next program example, it is not necessary to make a separate call to the printf routine for each line of output. Study the program listed in Program 3.3 and try to predict the results before examining the output. (No cheating now!)
Program 3.3 Displaying Multiple Lines of Output
#include <stdio.h> int main (void) { printf ("Testing...\n..1\n...2\n....3\n"); return 0; }
Program 3.3 Output
Testing... ..1 ...2 ....3