Chapter 2 Introducing C

Listing 2.1 The first.c Program

 1 #include <stdio.h>
 2 
 3 int main(void)                   /* a simple program */
 4 {                   
 5     int num;                     /* define a variable called num */
 6     num = 1;                     /* assign a value to num */
 7 
 8     printf("I am a simple ");    /* use the printf() function */
 9     printf("computer.\n");
10     printf("My favorite number is %d because it is first.\n", num);
11     return 0;
12 }
13 
14 /* Microsoft Visual C++
15 --------------------------------------------
16 I am a simple computer.
17 My favorite number is 1 because it is first.
18 --------------------------------------------
19 */

 

Listing 2.2 The fathm_ft.c Program

 1 // fathm_ft.c -- converts 2 fathoms to feet
 2 
 3 #include <stdio.h>
 4 int main(void)
 5 {
 6     int feet, fathoms;
 7 
 8     fathoms = 2;
 9     feet = 6 * fathoms;
10     printf("There are %d feet in %d fathoms!\n", feet, fathoms);
11     printf("Yes, I said %d feet!\n", 6 * fathoms);
12 
13     return 0;
14 }
15 
16 /* Microsoft Visual C++
17 ------------------------------------
18 There are 12 feet in 2 fathoms!
19 Yes, I said 12 feet!
20 ------------------------------------
21 */

 

Listing 2.3 The two_func.c Program

 1 /* two_func.c -- a program using two functions in one file */
 2 #include <stdio.h>
 3 void butler(void); // ISO/ANSI C function prototyping
 4 int main(void)
 5 {
 6     printf("I will summon the butler function.\n");
 7     butler();
 8     printf("Yes, Bring me some tea and writeable CD-ROMS.\n");
 9 
10     return 0;
11 }
12 
13 void butler(void)     /* start of function definition */
14 {
15     printf("You rang, sir?\n");
16 }
17 
18 /* Microsoft Visual C++
19 --------------------------------------------
20 I will summon the butler function.
21 You rang, sir?
22 Yes, Bring me some tea and writeable CD-ROMS.
23 --------------------------------------------
24 */

 

你可能感兴趣的:(Chapter 2 Introducing C)