这是《C primer plus》的教材,很详细
Listing 2.1. The first.c Program
#include <stdio.h>
int main(void) /* a simple program */
{
int num; /* define a variable called num */
num = 1; /* assign a value to num */
printf("I am a simple "); /* use the printf() function */
printf("computer.\n");
printf("My favorite number is %d because it is first.\n",num);
return 0;
}
If you think this program will print something on your screen, you're right! Exactly what will be
printed might not be apparent, so run the program and see the results. First, use your favorite
editor (or your compiler's favorite editor) to create a file containing the text from Listing 2.1.
Give the file a name that ends in .c and that satisfies your local system's name requirements.
You can use first.c, for example. Now compile and run the program. (Check Chapter 1,
"Getting Ready," for some general guidelines to this process.) If all went well, the output should
look like the following:
I am a simple computer.
My favorite number is 1 because it is first.
All in all, this result is not too surprising, but what happenedto the \ns and the %d in the
program? And some of the lines in the program do look strange. It's time for an explanation
The Example Explained
We'll take two passes through the program's source code. The first pass ("Pass 1: Quick
Synopsis") highlights the meaning of each line to help you get a general feel for what's going on.
The second pass ("Pass 2: Program Details") explores specific implications and details to help
you gain a deeper understanding.
Figure 2.1 summarizes the parts of a C program; it includes more elements than our first
example uses.
Figure 2.1. Anatomy of a C program.
Pass 1: Quick Synopsis
This section presents each line from the program followed by a short description; the next
section (Pass 2) explores the topics raised here more fully.
#include <stdio.h> include another file
This line tells the compiler to include the information found in the file stdio.h, which is a
standard part of all C compiler packages; this file provides support for keyboard input and for
displaying output.
int main(void) a function name
C programs consist of one or more functions, the basic modules of a C program. This program
consists of one function called main. The parentheses identify main() as a function name. The
int indicates that the main() function returns an integer, and the void indicates that main()
doesn't take any arguments. These are matters we'll go into later. Right now, just accept both
int and void as part of the standard ISO/ANSI C way for defining main(). (If you have a pre-
ISO/ANSI C compiler, omit void; you may want to get something more recent to avoid
incompatibilities.)
/* a simple program */ a comment
The symbols /* and */ enclose comments, remarks that help clarify a program. They are
intended for the reader only and are ignored by the compiler.
{ beginning of the body of the function
This opening brace marks the start of the statements that make up the function. The function
definition is ended with a closing brace (}).
int num; a declaration statement
This statement announces that you are using a variable called num and that num will be an int
(integer) type.
num = 1; an assignment statement
The statement num = 1; assigns the value 1 to the variable called num.
printf("I am a simple "); a function call statement
The first statement using printf() displays the phrase I am a simple on your screen, leaving
the cursor on the same line. Here printf() is part of the standard C library. It's termed a
function, and using a function in the program is termed calling a function.
printf("computer.\n"); another function call statement
The next call to the printf() function tacks on computer to the end of the last phrase printed.
The \n is code telling the computer to start a new line—that is, to move the cursor to the
beginning of the next line.
printf("My favorite number is %d because it is first.\n", num);
The last use of printf() prints the value of num (which is 1) embedded in the phrase in quotes.
The %d instructs the computer where and in what form to print the value of num.
return 0; a return statement
A C function can furnish, or return, a number to the agency that used it. For the present, just
regard this line as part of the ISO/ANSI C requirement for a properly written main() function.
} the end
As promised, the program ends with a closing brace.
Now that you have an overview of Listing 2.1, we'll take a closer look. Once again, we'll examine
the individual lines from the program, this time using each line of code as a starting point for
going deeper into the details behind the code and as a basis for developing a more general
perspective of C programming features.
Listing 2.1. The first.c Program
#include <stdio.h>
int main(void) /* a simple program */
{
int num; /* define a variable called num */
num = 1; /* assign a value to num */
printf("I am a simple "); /* use the printf() function */
printf("computer.\n");
printf("My favorite number is %d because it is first.\n",num);
return 0;
}
If you think this program will print something on your screen, you're right! Exactly what will be
printed might not be apparent, so run the program and see the results. First, use your favorite
editor (or your compiler's favorite editor) to create a file containing the text from Listing 2.1.
Give the file a name that ends in .c and that satisfies your local system's name requirements.
You can use first.c, for example. Now compile and run the program. (Check Chapter 1,
"Getting Ready," for some general guidelines to this process.) If all went well, the output should
look like the following:
I am a simple computer.
My favorite number is 1 because it is first.
All in all, this result is not too surprising, but what happenedto the \ns and the %d in the
program? And some of the lines in the program do look strange. It's time for an explanation
The Example Explained
We'll take two passes through the program's source code. The first pass ("Pass 1: Quick
Synopsis") highlights the meaning of each line to help you get a general feel for what's going on.
The second pass ("Pass 2: Program Details") explores specific implications and details to help
you gain a deeper understanding.
Figure 2.1 summarizes the parts of a C program; it includes more elements than our first
example uses.
Figure 2.1. Anatomy of a C program.
Pass 1: Quick Synopsis
This section presents each line from the program followed by a short description; the next
section (Pass 2) explores the topics raised here more fully.
#include <stdio.h> include another file
This line tells the compiler to include the information found in the file stdio.h, which is a
standard part of all C compiler packages; this file provides support for keyboard input and for
displaying output.
int main(void) a function name
C programs consist of one or more functions, the basic modules of a C program. This program
consists of one function called main. The parentheses identify main() as a function name. The
int indicates that the main() function returns an integer, and the void indicates that main()
doesn't take any arguments. These are matters we'll go into later. Right now, just accept both
int and void as part of the standard ISO/ANSI C way for defining main(). (If you have a pre-
ISO/ANSI C compiler, omit void; you may want to get something more recent to avoid
incompatibilities.)
/* a simple program */ a comment
The symbols /* and */ enclose comments, remarks that help clarify a program. They are
intended for the reader only and are ignored by the compiler.
{ beginning of the body of the function
This opening brace marks the start of the statements that make up the function. The function
definition is ended with a closing brace (}).
int num; a declaration statement
This statement announces that you are using a variable called num and that num will be an int
(integer) type.
num = 1; an assignment statement
The statement num = 1; assigns the value 1 to the variable called num.
printf("I am a simple "); a function call statement
The first statement using printf() displays the phrase I am a simple on your screen, leaving
the cursor on the same line. Here printf() is part of the standard C library. It's termed a
function, and using a function in the program is termed calling a function.
printf("computer.\n"); another function call statement
The next call to the printf() function tacks on computer to the end of the last phrase printed.
The \n is code telling the computer to start a new line—that is, to move the cursor to the
beginning of the next line.
printf("My favorite number is %d because it is first.\n", num);
The last use of printf() prints the value of num (which is 1) embedded in the phrase in quotes.
The %d instructs the computer where and in what form to print the value of num.
return 0; a return statement
A C function can furnish, or return, a number to the agency that used it. For the present, just
regard this line as part of the ISO/ANSI C requirement for a properly written main() function.
} the end
As promised, the program ends with a closing brace.
Now that you have an overview of Listing 2.1, we'll take a closer look. Once again, we'll examine
the individual lines from the program, this time using each line of code as a starting point for
going deeper into the details behind the code and as a basis for developing a more general
perspective of C programming features.