CHAPTER 1: A Tutorial Introduction
1.1 Getting Started
Normally you are at liberty to give functions whatever names you like, but "main" is special -- your program begins executing at the beginning of main. This means that every program must have a main somewhere.
main will usually call other functions to help perform its job, some that you wrote, and others from libraries that are provided for you.
The sequence \n in the string is C notation for the newline character, which when printed advances the output to the left margin on the next line.(就是打印了一行后,移到下下一行再打印)
printf never supplies a newline automatically.
Notice that \n represents only a single character. An escape sequence like \n provides a general and extensible mechanism for representing hard-to-type or invisible characters.
Among the others that C provides are \t for tab, \b for backspace, " for the double quote, and \for the backslash itself. (complete list in the Section 2.3)
1.2 Variables and Arithmetic Expressions
Any characters between /* and */ are ignored by the complier.
Comments may appear anywhere a blank or tab or newline can.
In C, all variables must be declared before they are used, usually at the beginning of the function before any executable statements.
A declaration announces the properties of variables;(int, float, char, etc). It consists of a type name and a list of variables, such as
e.g. :
int xxxxx
char xxxx, xxxx
char character -- a single byte
short short integer
long long integer
double double-precision floating point
In C, as in many other languages, integer division truncates(截断): any fractional part is discarded.
If an arithmetic operator has integer operands, an integer operation is performed. If an arithmetic operator has one floating-point operand and one integer operand, however, the integer will be converted to floating point before the operation is done.
Among others, printf also recognizes %o for octal(八进制), %x for hexadecimal(十六进制), %c for character, %s for character string, and %% for % itself.
1.3 The For Statement
#include <stdio.h>
/* print Fahrenheit-Celsius table */
main() {
int fahr;
for ( fahr = 0; fahr <= 300; fahr = fahr + 20)
printf("%3d %6.1f\n", fahr, (5.0/9.0)*(fahr-32);
}
This last change is an instance of a general rule --in any context where it is permissible to use the value of a variable of some type, you can use a more complicated expression of that type: ( (5.0/9.0)*(fahr-32)).
Since the third argument of printf must be a floating-point value to match the %6.1f, any floating-point expression can occur there.
The for statement is a loop, a generalization of the while.
Within the parentheses, there are three parts, separated by semicolons.
The first part, the initialization
fahr = 0
is done once, before the loop proper is entered.
The second part is the test or condition that controls the loop:
fahr <= 300
This condition is evaluated; if it is true, the body of the loop (here a single printf) is executed. Then the increment step
fahr = fahr + 20
is executed, and the condition re-evaluated.
The loop terminates if the condition has become false.
The choice between while and for is arbitrary, based on which seems clearer. The for is usually appropriate for loops in which the initialization and increment are single statements and logically related, since it is more compact(精简) than while and it keeps the loop control statements together in one place.
网友评论