Chapter III: C language program design preliminary
C language program design This lesson introduces the basic method of C language program design and basic program statement. From the perspective of the program process, the program can be divided into three basic structures, namely the sequential structure, branch structure, and loop structure. These three basic structures can form all of all complex programs. The C language provides a variety of statements to implement these program structures. This chapter introduces these basic statements and its applications, making readers a preliminary understanding of the C procedure, and lay the foundation for the latter chapters. The execution portion of the c program is composed of statements. The functionality of the program is also implemented by executing statements. The C statement can be divided into the following five categories: Function call statement 3. Control statement 4. Composite statement 5. Empty statement 1. Expression statement expression statement is incorporated by a semicolon ";" . Its general form is: expression; execution expression statement is the value calculating expression. For example: x = y z; assignment statement y z; addition operation statement, but calculation results cannot be retained, no practical meaning i ; self-increasing 1 statement, I value increase 1
2. Function call statement consists of function name, actual parameters, and a semicolon ";" Its general form is: function name (actual parameter table); execution function statement is to call the function body and assign the actual parameter to the formal parameters in the definition of the function, and then perform the statement in the invigible representation to obtain a function value. (In the fifth chapter function, more details), such as Printf ("C Program"); call the library function, output strings.
3. Control the statement control statement to control the process of the program to implement the various structural methods of the program. They consist of a particular statement definition. The C language has nine control statements. It can be divided into the following three categories: (1) Conditional judgment statement if statement, Switch statement (2) loop execution statement do while statement, while statement, for statement (3) steering statement BREAK statement, goto statement, continue statement, return statement 4. The composite statement puts a plurality of statements that consist of multiple statements in parentheses {}. In the program, the composite statement should be regarded as a single statement, not a plurality of statements, such as {x = y z; a = B C; Printf ("% D% D", X, a);} is a Composite statement. Each statement in the complex statement must be terminated in a semicolon ";", it cannot be added to the bracket "}". 5. The empty statement is only a semicolon ";", the statement is called empty statement. The null statement is the statement that does not execute. The empty statement in the program can be used as a empty cycle. For example, while (GetChar ()! = '/ N'); the function of this statement is to re-enter as long as the characters entered from the keyboard are not cargo. The cyclic body here is an empty statement. The assignment statement assignment statement is an expression statement configured by the assignment expression. Its general form is: variable = expression; the function and characteristics of assignment statements are the same as assignment expressions. It is one of the most useful statements in the program. I need to pay attention to the following points in the use of assignment statements: 1. Since the expression on the right side of the assignment, it is also an assignment expression, therefore, the following form variable = (variable = expression); is established To form a nesting situation. The general form after the development is: variable = variable = ... = expression; for example: a = b = c = D = E = 5; according to the right bondability of the assignment operator, therefore actually equivalent to: E = 5 ; D = E; c = D; b = C; a = b; 2. Note the difference between the variable assignment and assignment statements in the variable description. The initial value to the variable is part of the variable description, and the number of variables after the initial value must still be comma intervals between the other similar variables, and the assignment statement must end with the semicolon. 3. In the variable description, it is not allowed to be assigned to multiple variables to the first value. The following description is wrong: int a = b = c = 5 must be written as int a = 5, b = 5, c = 5; and assignment statements allow continuous assignments 4. Note the difference between assignment expressions and assignment statements. Assignment expressions are an expression that can appear in any place where the expression appears, and the assignment statement cannot be. The following statement is legal: if ((x = y 5)> 0) z = x; the function of the statement is that if the expression x = y 5 is greater than 0 z = x. The following statement is illegal: IF ((x = y 5;)> 0) z = x; because = y 5; is a statement, it cannot appear in the expression. Data Output State This section describes the statement that outputs data to the standard output device display. In the C language, all data input / output is done by the library function. It is therefore all function statements. This section describes the Printf function and the PUTCHAR function. The Printf function printf function is called the format output function, and the last letter f is the meaning of "format". Its function is to display the specified data on the display screen by the format specified by the user. In the first example we have used this function multiple times. First, the general form of the Printf function call is a standard library function, its function prototype in the header file "stdio.h".
However, as a special case, the stdio.h file must be included before using the Printf function. The general form of the Printf function call is: Printf ("Format Control String", Output List) where the format control string is used to specify the output format. Format control strings can be composed of two format strings and non-format strings. The format string is a string that starts with%, with a variety of format characters in%, to illustrate the type, form, length, and lower number of output data. Such as "% D" means that the "% LD" is output according to the decimal output, "% C" indicates that according to the character output, etc. The discussion will be given later. Non-format strings are illuminated when the output is displayed, and prompts in the display. Each output item is given in the output list, requiring the format string and each output item to correspond to the number and type. Void main () {INT A = 88, B = 89; Printf ("% D% D / N", A, B); Printf ("% D,% D / N", A, B); Printf (" % C,% C / N ", A, B); Printf (" A =% D, B =% D ", A, B);} a <- 8, b <- 89
Printf ("% D% D / N", A, B); Printf ("% D,% D / N", A, B); PrintF ("% C,% C / N", A, B); Printf ("A =% D, B =% D", A, B); the value of A and B is output four times in this example, but due to the different format control strings, the results of the output are different. In the output statement format control string of the fourth line, a space (non-format character) is added between the two formatter% D, so there is a space between the outputs A and B values. The PRINTF statement format of the fifth line is added to the non-format character comma, so there is a comma between the output A and B values. The format string of the sixth line requires the A and B values in accordance with the character type. In the seventh line, in order to prompt the output, the non-format string is added. Second, the format string is in general forms of format strings in Turbo C to: [Mark] [Output Minimum Width] [. Precision] [Length] Type 1 of which is optional. The meaning of each item is as follows: 1. Type type character is used to indicate the type of output data, its formatting character and meaning the following table shows: Indicated format character format character in the output type, output with symbol integers in ten-way form (positive Do not output symbols) o Output unsigned integer (no output prefix O) x in an octazone form (no output prefix OX) U output unsigned integer f in decimal form, output order, Double precision real est 8 output single, double precision real g is shorter output width output single, double precision real number C output a single character S Output string 2. Sign flag characters -, , # Four kinds of spaces, the meaning shown below: Sign Format Character Sign Significance - Results The left alignment, the right edge fill the space output symbol (positive or negative) space output value is timing crown, the crown Negative number # 无 类 类 类 类;; 类 类 类 类 类 类 类 类 类 类 类 类 类 类 类 类 类 类 类 类 类 类 类 类 类 类 类 类 类 类 类 类 0 类 类 类 类 类 类 类 0 类 类 类 类 类 类 0 类 类 类 类 类 类 0 类 类 类Different points 3. Output minimum width with decimal integers to represent the minimum number of outputs. If the actual number of digits is more than the defined width, then the actual number of digits is output. If the actual number of bits is less than the defined width, it is safer or 0.4. The precision accuracy format is "." Begins, and then following the decimal integer. The meaning of this is: If the number is output, the number of digits is indicated; if the output is the character, the number of output characters is indicated; if the actual number of digits is greater than the defined accuracy, the exceeding portion is cut. 5. The length of the length format is H, L, h indicates that the shorter amount is output, and L is indicated by the long output.
Void main () {int a = 15; float b = 138.3576278; Double C = 35648256.3645687; char d = 'p'; Printf ("A =% D,% 5D,% O,% x / N", A, A , a, a); Printf ("B =% F,% LF,% 5.4LF,% E / N", B, B, B, B); Printf ("C =% LF,% F,% 8.4LF / N ", C, C, C); Printf (" D =% C,% 8C / N ", D, D);} a <- 15b <- 138.3576278c <- 35648256.3645687d <- ' P 'main () {Int a = 29; float b = 1243.2341; Double C = 24212345.24232; char c =' H'Printf ("A =% D,% 5D,% O,% x / N", A, A , a, a); Printf ("B =% F,% LF,% 5.4LF,% E / N", B, B, B, B); Printf ("C =% LF,% F,% 8.4LF / N ", C, C, C); Printf (" D =% C,% 8C / N ", D, D);} In this case, the value of the integer variable A is output in four formats, of which "% 5D" requires the output width of 5, and a A value is 15 seconds of two, and three spaces. The value of the real amount B is output in four formats in four formats. Wherein "% f" and "% LF" format are the same, indicating that the "L" symbol has no effect on the "F" type. "% 5.4LF" specifies that the output width is 5, the accuracy is 4, and the actual length is output, and the number of decimal bits exceeds the 4 digits to be cut. The ninth line output double precision implement, "% 8.4LF" cuts more than 4 digits due to the designated accuracy of 4. The tenth line outputs the character quantity D, where "% BC" specifies that the output width is 8 so that 7 spaces are added before the output character P. Pay attention to a problem when using the Printf function, which is the order in the output table column. Different compilation systems are not necessarily the same, can be left to right, or from right to left. Turbo C is made from right to left. If an example 2.13 rewritten as follows: void main () {INT i = 8; Printf ("% D / N% D / N% D / N% D / N% D / N% D / N", i, - i, i -, i , - i - I -);} i <- 8 This program is compared with Example 2.13 to change multiple printf statements to the outputf statement output. However, it can be seen from the results. Why is the result that the result will be different? Just because the PrintF function is performed from the order in which each quantity in the output table is performed. In the formula, the last "-i-" value is first, the result is -8, and then i is 7 after 1. Then the "-i " item is worth -7, and then I is 8 afterwards. Then, the "I-" item is worth 8, then I will then reduce 1 after 7. Request "i " item 7, then I will increase by 8 after another. Request "--i" item, i first below 1, output, the output value is 7. Finally, the first " i" in the output table column will be obtained. At this time I outputs 8 afterwards. However, it must be noted that although the order sequence is from right to left, but the output order is or from left to right, the resulting result is the output result. The character output function PUTCHAR function PUTCHAR function is a character output function that is functional to output a single character on the display.
Its general form is: PUTCHAR (character variable), for example: PUTCHAR ('a'); output uppercase letters aputchar (x); output character variable x value PUTCHAR ('/ n'); Retracence pair control characters perform control functions, Not displayed on the screen. You must use the file with the file to contain the file: #include
Such as: 7 8 9 or 789 Format string format string general form is:% [*] [Enter Data Width] [Length] Type With square bracket [] items for options. The significance is as follows: 1. Type indicates the type of input data, its formatting and meaning shown below. Format Character Sign D Enter Ten Import Integer O Input Octa Integer X Enter Hexadecimal Entario 10 No Symbol Decimal Integer F or E Enter Truth (Forms) C Enter a single character S Enter string 2. " * "The value is used to indicate that the input item does not give the corresponding variable after reading, that is, skip the input value. Such as Scanf ("% D% * D% D", & A, & B); when the input is: 1 2 3, 1 gives 1 to a, 2 is skipped, 3 give B. 3. The width uses the decimal integer specifies the width of the input (ie, the number of characters). For example: SCANF ("% 5D", & A); input: 12345678 Give only 12345 to the variable A, the remainder is cut off. Another example: scanf ("% 4D% 4D", & A, & B); input: 12345678 will give 1234 to A, and give 5 b. 4. The length of the length format is L and H, and L represents the input length integer data (such as the% LD) and the double precision floating point number (as% LF). h represents the short integer data. You must also pay attention to the following: a. There is no precision control in the scanf function, such as Scanf ("% 5.2f", & a); is illegal. You cannot try to use this statement to enter a decimal number of 2 digits. b. SCANF is required to give a variable address, if the variable name will be wrong. Such as scanf ("% d", a); is illegal, it should be changed to SCNAF ("% D", & a); it is legal. c. When entering multiple numerical data, if there is no non-format character in the format control string, there is a space, TAB, or a carriage return. C compiles the space, Tab, carriage return, or illegal data (if "12A" input "12A" is input to "% D", that is, it is considered to end. d. When entering the character data, if there is no non-format character in the format control string, all the characters that all entered are valid characters. For example: SCANF ("% C% C% C", & A, & B, & C); input to: D e f, gives 'D' to A, 'F' Give B, 'E' Give C. On only when the input is: DEF, the 'D' is assigned to a, 'E' gives C. If you add spaces in the format control as spacing, such as Scanf ("% C% C% C", & A, & B, & C); then add space between each data when input. Void main () {Char A, B; Printf ("Input Character A, B / N"); Scanf ("% C% C", & A, & B); Printf ("% C / N", A, b);} scanf ("'C14F14% C% C", & A, & B); Printf ("% C / N", A, B); due to the SCANF function "% C% C" no space, input Mn, the result is only M.
When the input is changed to Mn, the MN can be output, see the following input operation: Input Character A, BMnmnvoid main () {Char A, B; Printf ("Input Character A, B / N"); scanf (" % C% C ", & A, & B); Printf (" / n% C / N ", A, B);} Scanf ("% C% C ", & A, & B); this example represents SCANF format control When there is a space between the string "% C% C", the input data can be spaced apart. e. If there is a non-format character in the format control string, you should also enter the non-format character. For example: SCANF ("% D,% D,% D", & A, & B, & C); in which the non-format "," is separated, the input should be: 5, 6, 7, such as: scanf ( "A =% D, B =% D, C =% D", & A, & B, & C); then the input should be A = 5, b = 6, c = 7g. If the input data is inconsistent with the output type Although the compilation can pass, the result will be incorrect. Void main () {int A; Printf ("Input A Number / N"); Scanf ("% D", & A); Printf ("% ld", a);} Due to the input data type is integer, output The format string of the statement is explained as a long integer, so the output result and the input data do not match. If the change program is as follows: void main () {long a; printf ("Input A long integer / n"); scanf ("% ld", & a); printf ("% ld", a);} Run results: Input a long integer12345678901234567890 When the input data is changed to a long integer, the input and output data is equal. The keyboard input function getChar function getchar function is entered from the keyboard. Its general form is: getChar (); usually gives an input character to a character variable, constitutes an assignment statement, such as: CHAR C; c = getchar (); # include