<br>This chapter gives an overview of the C Programming language. A series of programs is presented, and the elements of each program are carefully explained.
<br>Experimentation and interaction are emphasized throughout the text. In this chapter, we emphasize how to use the basic input/output functions of C.
<br>Note carefully that all our C code also serves as C++ code and that all the ideas we discuss hold for c++ as well as for C. Of course, the C++ programmer has available a richer set of tools and techniques out of which programs can be constructed. (See Chapter 13, í ÞMoving from C to C++.í µ)
<br>Except for Section 1.8, í ÞArrays, Strings, and Pointers,í µ on page 36, everyone should read all the material in this chapter. Any reader who has had experience with arrays, pointers, and files in some other language can read all the sections of this chapter to get a more complete overview of C.
<br>Others can come back to material when they feel they are ready. Everyone should read this chapter with the understanding that technical details and further explanations will come in later chapter.
<br>Resident on the machine is a collection of special programs called the operating system. Commonly available operating systems include MS-DOS. OS/2, and UNIX. An operating system manages machine resources, provides software for the user, and acts as an interface between the user and hardware.
<br>Among the many software packages provided by the operating system are the C compiler and various text editors. The principal text editor on the UNIX system is called vi. Some systems, such as Borland C++, integrate the text editor and the compiler.
<br>We assume that the reader is able to use some text editor to create files containing C code. Such files are called source files, and they are compiled on most UNIX system with the cc command, which invokes the C compiler. Because the cc command invokes the compiler, the name of the command is also the name of the compiler. Thus, C compiler and cc compiler are used interchangeably. Roughly speaking, a compiler translates source code to object code that is executable.
<br>On UNIX systems, this compiled code is automatically created in a file named a.out.
<br>On MS-DOS systems, this compiled code is automatically created in a file with the same name as the .c file, but with the .exe extension replacing the .c extension. At the end of this chapter, in Section 1.10, í ÞOperating System Considerations,í µ on page 53, we present in detail the steps necessary to edit, compile, and execute a program.
<br>Programs must communicate to be useful. Our first example is a program that prints on the screen the phrase í Þfrom sea to shining C.í µ The complete program is
<br>Using a text editor, we type this into a file whose name ends in .c. The choice of a file name should be mnemonic. Let us suppose the program has been written in the file sea.c. To compile the program, we give the command
<br>Dissection of the sea Program (óÔú¡ÞŒ•‚º×¤ seaòµÚ óÕ·º¶ )
<br>
<br>#include
<br>
<br>A preprocessor is built into the C compiler. When the command to compile a program is given, the code is first preprocessed and then compiled. Lines that begin with a # communicate with the preprocessed. This #include line causes the preprocessor to include a copy of the header file at this point in the code.
<br>This header file is provided by the C system. The angle brackets around indicate that the file is to be found in the usual place, which is system-dependent. We have included this file because it contains information about the printf() function.
<br>This is the first line of the function definition for main(). (We write parentheses after the name main to remind the reader that main() is a function.) The two words int and void are keywords, also called reserved words. They have special meaning to the compiler. In Section 2.4, í ÞKeywords,í µ on page77, we will see that there are 32 keywords in C, including int and void.
<br>Every program has function named main(). Program execution always starts with this function. The top line should be read as í Þmain() is a function that takes no arguments and return an int value.í µ
<br>Here, the keyword int tells the compiler that this function returns a value of type int. The word int stands for integer, but the word integer itself cannot be used. The parentheses following main indicate to the compiler that main is a function.
<br>This idea is confusing at first because what you see following main is (void), but only the parentheses () constitute an operator telling the compiler that main is a function. The keyword void indicates to the compiler that this function takes no arguments. When we write about functions such as main() and printf(), we usually follow the name in print with parentheses. This indicates to the reader that we are discussing a function. (Many programming books follow this practice.)
<br>The C system contains a standard library of functions that can be used in programs.
<br>This is a function from the library that prints on the screen. We included the header file stdio.h because it provides certain information to the compiler about the function printf().
<br>A string constant in C is a series of characters surrounded by double quotes. This string is an argument to the function printf(), and it controls what gets printed. The two characters \n at the end of the string(read backslash n) represent a single character called newline. It is a nonprinting character. It advances the cursor on the screen to the beginning of the next line.
<br>This is a return statement. It causes the value zero to be returned to the operating system, which in turn may use the value in some way, but is not required to do so.
<br>(See Section 12.7, í ÞReturning the Status,í µ on page 579, for further discussion.)
<br>Our use of this return statement keeps the compiler happy. If we do not use it, the compiler will complain.(See exercise 4, on page 60.) One of the principal rules of programming is í Þkeep your compiler happy.í µ
<br>The function printf() acts to print continuously across the screen. It moves the cursor to the start of a new line when a newline character is read. The screen is a two-dimensional display that prints from left to right and top to bottom. To be readable, output must appear properly spaced on the screen.
<br>Although it is different from the first version, it will produce the same output. Each time printf()is called, printing begins at the position where the previous call to printf() left off. If we want to print our phrase on three lines, we can use newline characters.
<br>Let us write one additional on this program, one that will box the phrase in a rectangle of asterisks. It will show how each character, including blanks and newline characters, is significant, and when it is executed, it will give some sense of the screen proportions.
<br>We will write a program to convert the distance of a marathon in miles and yards to kilometers. In English units, a marathon is defined to be 26 miles and 385 yards. These numbers are integers.
<br>To convert miles to kilometers, we multiply by the conversion factor 1.609, a real number. In memory, computers represent integers differently from real. To convert yards to miles, we divide by 1760.0, and, as we shell see, it is essential to represent this number as a real rather as an integer.
<br>In C, all variables must be declared, or named, at the beginning of the program. A variable name, also called an identifier, consists of a sequence of letters, digits, and underscores, but may not start with a digit.
<br>printf(í Þ\nA marathin is %f kilometers. \n\ní µ, kilometers);
<br>return 0;
<br>}
<br>
<br>The output of the program is
<br>
<br>A marathon is 42.185970 kilometers.
<br>
<br>Dissection of the marathon Program
<br>
<br>/* The distance of a marathon in kilometers. */
<br>
<br>Anything written between the characters /* and */ is a comment and is ignored by the compiler. All programs in this book that start with a comment are listed in the index.
<br>This is a declaration. Declarations and statements end with a semicolon. int is a keyword and is one of the fundamental types of the language. It informs the compiler that the variables flowing it are type int and are to take on integer values. Thus, the variables miles and yards in this program are of type int.
<br>This is a declaration. float is a keyword and is one of the fundamental types of the language. It informs the compiler that the variables following it are of type float and are to take on real values. Thus, the variable kilometers in this program is of type float.
<br>These are assignment statements. The equal sign is an assignment operator. The two numbers 26 and 385 are integer constants. The value 26 is assigned to the variable miles. The value 385 is assigned to the variable yards.
<br>This is assignment statement. The value of the expression on the right side of the equal sign is assigned to the variable kilometers. The operator *, +, and / stand for multiplication, addition, and division, respectively. Operations inside parentheses are performed first. Because division has higher precedence than addition, the value of the sub expression
<br>is calculated first. (See Appendix E, í ÞOperator precedence and Associativity.í µ) That value is added to the value of the variable miles to produce a value that is then multiplied by 1.609. This final value is then assigned to the variable kilometers.
<br>printf(í Þ\nA marathon is %f kilometers.\n\ní µ, kilometers);
<br>
<br>This is a statement that invokes, or calls, the printf() function. The function printf() can have a variable number of arguments. The first argument is always a string, called the control string. The control string in this example is
<br>It is the first argument to the function printf(). Inside this string is the conversion specification, or format, %f. The formats in a control string, if any, are matched with the remaining arguments in the printf() function.
<br>In this case, %f is matched with the argument kilometers. Its effect is to print the value of the variable kilometers as a floating-point number and insert it into the print stream where the format %f occurs.