C Language Programming
This lesson introduces the basic method of C language programming and the BASIC program statement.
From the point of view of program flow, the program can be divided into three kinds of basic structure, namely sequential structure, branch structure and cyclic structure. These three basic structures can be composed of all kinds of complex programs. The C language provides a variety of statements to implement these program structures. This chapter introduces these basic sentences and their applications, so that readers have a preliminary understanding of the C program, which lays the foundation for the study of the later chapters.
The statement of the C program
The execution part of the C program is made up of statements. The function of the program is also implemented by executing the statement.
The C statement can be grouped into the following five categories:
1. Expression statements
2. Function Call statement
3. Control statements
4. Compound statement
5. NULL statement
1. Expression statements
An expression statement consists of an expression plus a semicolon ";". The general form is: an expression; Executing an expression statement is a value that evaluates an expression. For example: x=y+z; Assignment Statement y+z; Addition Operation statement, but the results can not be retained, no practical significance i++; Self-Add 1 statement, I value increase 1
2. Function Call statement
Consists of the function name, the actual parameter plus a semicolon ";". Its general form is: function name (actual parameter table); The execution function statement calls the function body and assigns the actual parameter to the formal parameter in the function definition, then executes the statement in the body of the function, and then takes the function value. (described in detail in chapter fifth functions) such as printf ("C program"); Call library function, output string.
3. Control statements
The control statement is used in the process of controlling the program to realize the various structural ways.
They consist of a specific statement definition character. C language has nine kinds of control statements. 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) Turning statement
Break statement, Goto statement, continue statement, return statement
4. Compound statement
A statement that consists of multiple statements enclosed in parentheses {} is called a compound statement. A compound statement should be considered a single statement rather than multiple statements in a program, such as
{
X=y+z;
A=b+c;
printf ("%d%d", x,a);
}
is a compound statement. Each statement within a compound statement must be separated by a semicolon ";" At the end, you cannot add a semicolon outside the bracket "}".
5. NULL statement
A statement consisting of only semicolons ";" is called an empty statement. An empty statement is a statement that is not executed. In the program The Hollow statement can be used as an empty loop body. For example, while (GetChar ()!= ' \ n '); The function of this statement is to re-enter it as long as the character entered from the keyboard is not a carriage return. The loop body here is an empty statement.
Assignment statement
An assignment statement is an expression statement that is composed of an assignment expression plus a semicolon. Its general form is: variable = expression; An assignment statement has the same functionality and characteristics as an assignment expression. It is one of the most frequently used statements in a program. The following points need to be noted in the use of assignment statements:
1. Because an expression on the right side of the assignment "=" can also be an assignment expression, the following form variable = (variable = expression); is established, thus forming a nested case. The general form after its expansion is: variable = variable =...= expression;
For example:
A=b=c=d=e=5 to the right of the assignment operator, so it is actually equivalent to:
e=5;
D=e;
C=d;
B=c;
A=b;
2. Note the difference between assigning an initial value and an assignment statement in a variable description. Assigning an initial value to a variable is a part of the description of the variable, and the variable with the initial value must still be separated by a comma, and the assignment statement must end with a semicolon.
3. In the variable description, it is not allowed to assign an initial value to multiple variables in succession. The following description is incorrect: int a=b=c=5 must be written as int a=5,b=5,c=5; and assignment statements allow consecutive assignment
4. Note the difference between an assignment expression and an assignment statement. An assignment expression is an expression that can appear wherever an expression is allowed, whereas an assignment statement cannot.
The following statement is valid: if ((x=y+5) >0) z=x; The function of the statement is to z=x if the expression x=y+5 greater than 0. The following statement is illegal: if ((x=y+5;) >0) z=x because =y+5 is a statement and cannot appear in an expression.
Data output Statement
This section describes statements that output data to a standard output device display. In C, all data input/output is done by the library function. So it's all a function statement. This section introduces the printf function and the Putchar function first. The printf function printf function is called the format output function, and the last letter F of the keyword is formatted. The function is to display the specified data to the monitor screen in the format specified by the user. We have used this function several times in the previous example.
First, the general form of printf function calls
The printf function is a standard library function whose function prototype is in the header file "Stdio.h". However, as a special case, it is not required to include the Stdio.h file before using the printf function. The general form of printf function calls is: printf ("Format control string", Output table column) where the format control string is used to specify the output format. A format control string can consist of both a format string and a unformatted string. The format string is a string that begins with a%, followed by a variety of format characters, to indicate the type, form, length, scale, and so on of the output data. If "%d" indicates a decimal integer output, "%ld" indicates a decimal long integer output, and "%c" indicates a character-type output, and so on. The discussion will be given later.
Non-formatted strings are printed as they are in the output, prompting in the display. Output table columns give individual output items, requiring that the format string and output items should correspond one by one in 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);
In this example, the value of a,b is output four times, but the result of the output is not the same because the format control string is different. In the output Statement format control string for line four, a space (unformatted character) is added between the two-format string%d, so there is a space between the a,b values of the output. Line five the printf statement format Control string adds a comma of unformatted characters so that a comma is added between the a,b values of the output. The format string for line six requires that the A,B value be output by character type. Line seventh adds the unformatted string in order to prompt the output.