1 Statement Overview
1.1 normal expression statement: adding a semicolon after an expression is an expression statement. Different from an expression, a statement has no value while an expression has a value.
1.2 process control statement: A process control statement is mainly used to control the execution trend of a program and implement various structure methods of the program. It has specific keywords and can be divided into two types: branch and loop.
Branch flow control statements: if and switch.
Loop flow control statements: for, while, And do while.
1.3 jump statement: the jump statement is mainly used to redirect the program from normal execution to other parts. Common jump statements are as follows:
Interrupted statements: break and continue.
Turning statement: goto.
Return Statement: return.
1.4 compound statement: it consists of some statements and is placed in braces to form a whole. The sentence pattern is as follows:
{
Statement 1;
Statement 2;
......
Statement n;
}
The C language syntax specifies that compound statements are processed as a statement.
Sample Code:
# Include "stdio. h"
Main (){
Int a = 11, B = 21, c = 31;
Printf ("a = % d \ tb = % d \ tc = % d \ n", a, B, c );
{Int B = 22;/* compound statement start */
Float c = 3.3;
Printf ("a = % d \ tb = % d \ tc = % f \ n", a, B, c );
A = B;
}/* Compound statement ends */
Printf ("a = % d \ tb = % d \ tc = % d \ n", a, B, c );
}
The output result is as follows:
A = 11 B = 21 c = 31
A = 11 B = 22 c = 3.300000.
A = 22 B = 21 c = 31
The above code can be compared with the following code:
# Include "stdio. h"
Main (){
Int a = 11, B = 21, c = 31;
Printf ("a = % d \ tb = % d \ tc = % d \ n", a, B, c );
{Int a = 1;
Int B = 22;/* compound statement start */
Float c = 3.3;
Printf ("a = % d \ tb = % d \ tc = % f \ n", a, B, c );
A = B;
}/* Compound statement ends */
Printf ("a = % d \ tb = % d \ tc = % d \ n", a, B, c );
}
Program running result:
A = 11 B = 21 c = 31
A = 1 B = 22 c = 3.300000
A = 11 B = 21 c = 31
As you can see, the variables declared in the composite statement only play a role in the composite statement.
1.5 null statement: a null statement generally refers to a placeholder with only one semicolon. It is generally used in the initial stage of program design to indicate the function or code to be compiled.
1.6 introduction to various statements:
# Include "stdio. h"
Main (){
Int a = 10, B = 20, c;/* statement */
If (B> a)/* control statement */
{;/* Compound statement */
C = a + B;/* expression statement */
Printf ("% d", c);/* function call statement */
}
Else
;/* Empty statement */
}
2. Relationship between library function calls and input/output
2.1 Standard library functions are stored in the function library. Therefore, you must inform the computer of the library to which the function library belongs. This process is a function declaration. For example:
# Include "math. h"/* standard mathematical function declaration */
Main (){
...
Y = sin (x);/* call the library function */
...
}
Include is a specific word in the C language. It indicates a file containing. h, which is called a header file.
2.2 some input and output standard library functions are provided in C language, such as printf and scanf. the header file stdio. h of the function library must be included in the program.
3. format the output.
3.1printf: It outputs data to the terminal.
Function Format: printf ("format control statement", output item 1, output item 2 ,......);
For example: printf ("% f", j); Output floating point type variable j, % f is the control operator.
Common Format controllers:
% D -- decimal integer, % o --- octal unsigned integer, % f output real number, % s -- string, % c -- output single character, etc.
4-character output
The putchar function can be used to output 4.1 characters, which has been defined in stdio. h.
Sample Code:
# Include "stdio. h"
Main (){
Char a, B, c;
A = 'C ';
B = 'a ';
C = 'T ';
Putchar ();
Putchar (B );
Putchar (c );
Putchar ('\ n ');
Putchar ('\ 101 ');
Putchar ('B ');
Putchar ('C ');
}
Note: This function cannot output two or more character data.
5. format the input.
5.1scanf: This function is used to obtain data from the keyboard, format the data, and assign the value to the variable.
Call method: scanf (format control statement, input item );
6 Character Input
6.1 getch () and getche () functions: read a single character.
The difference between the two is that the getch () function does not display the characters read back to the screen, but getche () is displayed.
Sample Code:
# Include <stdio. h>
Main ()
{
Char c, ch;
C = getch ();/* read a character from the keyboard and do not return it to the character variable c */
Ch = getche ();/* read a character explicitly from the keyboard and send it to the character variable ch */
Putchar (c);/* output this character */
Putchar (ch );
}
6.2 getchar () function: enter a character from the terminal. The getchar function inputs space characters, tabs, and linefeeds as valid characters.
Sample Code:
# Include "stdio. h"
Main ()
{Char c1, c2;
C1 = getchar ();
C2 = getchar ();
Putchar (c1 );
Putchar (c2 );
}
7. Sequential Programming
Example 1: Calculate the root of a quadratic equation.
Code:
# Include "math. h"
Main ()
{
Double a, B, c;/* declaring coefficient */
Double x1, x2, p;
Printf ("pleaseinputa, B, c :");
Scanf ("% lf", & a, & B, & c);/* input coefficient */
Printf ("\ n ");
P = B * B-4 * a * c;/* p stores the Discriminant Value */
X1 = (-B + sqrt (p)/(2 * a);/* calculate the root of the equation */
X2 = (-B-sqrt (p)/(2 * );
Printf ("x1 = % f, x2 = % f \ n", x1, x2);/* output result */
}
Example 2: two integers in reverse order.
Code:
# Include <stdio. h>
Main ()
{
Int m, n;
Printf ("inputm (10-99 ):");
Scanf ("% d", & m );
Printf ("\ n ");
N = m % 10;
M = n * 10 + m/10;
Printf ("m = % d \ n", m );
}
From letthinking's column