**Framework**
An overview of the 3.1C statement (statement describes a behavior, an instruction issued to a computer to perform an operation)
1. Expression statements
An expression is followed by an';'to form an expression statement.
Example:
Add an existing variable definition: int a,b; The following statements make sense: a=3; //assignment expression forms statement, assignment variable a b=a+3; //assignment expression forms a statement that assigns the result of a+3 to b A++; //self-additive expression forms a statement, variable a self-adds after execution a=3,b=4; //comma expression forms a statement, the value of variable a after execution is 3, and the value of B is 4
2. Process control statements
(1) control statements for selection structure: if double-branch selection structure, which multi-branch selection structure.
(2) Loop structure control statements: while statement and for statement control immediate loop structure, do~while control until loop structure.
(3) Control process transformation statements: also known as jump statements, including break, contiune, goto, return statements.
3. Empty statement: There is only one semicolon to perfect the selection and looping structure.
4. Compound statement
A relatively well-functioning program segment enclosed in curly brackets'{}'.
Example:
#include <stdio.h> int add(int x, int y) { int t; t = x + y; return (t); } void main() { int a, b, sum; scanf("%d%d", &a, &b); sum = add(a, b); printf("sum=%d\n", sum); }
5. Function Call Statements
#include <stdio.h> #Include <math.h> //compile preprocessing command, not C language void main() { int x, y; //Variable Definition scanf_s("%f", &x); //Function call statement, call library function scanf_s() if (x < 0) //Statement that controls if of the selection structure { x = -x; y = sqrt(x + 6); //Assignment statement } //Compound statement else y = 1; printf("y=%f", y); //Function call statement, call library function printf() }
3.2 Use of common library functions
Use of 3.2.1 library functions
-
The general form of a function call is the function name (parameter table column).
-
The function name indicates which function was called.The Parameter Table Column is a table column of determinate values and is the parameter passed to the function. The parameters in the Parameter Table Column must be consistent with the number and type of parameters in the function prototype.Function call followed by';'constitutes a function call statement.
-
A library function is a function placed in a function library. When calling a function, the compiler must tell the compiling system which library the library function belongs to, so that the compiler can check whether the function is legal and execute the function effectively. This process is called declaration.
-
Standard library functions are declared as: #include<header file>
The following is a table of commonly used standard library functions
Example: The prototype sine function is: *double sin(double x)*
#include <stdio.h> #Include <math.h> //Call the header file math.h containing the sin function void main() { float x, y; x = 1.5; y = sin(x); printf("%f\n", y); }
3.2.2 Output Function
1. Character output function (putchar)
- Pu putchar outputs a single character to the terminal.
General format: The putcahr argument can be either a cahr data type or an int data type.
Example:
#include <stdio.h> int main() { putchar('a'); //Characters stored in output variable a }
#include <stdio.h> int main() { putchar('\n'); //Output Line Break Characters }
#include <stdio.h> int main() { putchar('\101'); //Output Character'A' }
2. Format Output Function (printf)
-
The general form of a printf function call: printf (format control, output list)
-
Format Control is a string of common characters starting with'%'; output list is a list of data items to be output, separated by commas.
-
Format Control (caused by double quotation marks): Format Control Characters, Escape Characters, Common Characters
-
Output list: constants, variables, expressions, function calls.
#include <stdio.h> int main() //Use int instead of void. { int a = -1; //Define Constant a printf("%d,%o,%x,%X,%u", a, a, a, a, a ); //%d outputs signed integers in decimal form,%o outputs machine codes for integers in octal,%x outputs machine codes for integers in hexadecimal,%X outputs machine codes for integers in hexadecimal (uppercase), and%u outputs integers in unsigned decimal. } //Output: -1, 177777, ffff, FFFF,65535
#include <stdio.h> void main() { float a = 123.456; printf(" % f, % e", a, a); } //Output results: 123.456000,1.23456e+02
#include <stdio.h> void main() { printf(" % c, % s", 'A', "china"); } //Output: A,china
#include <stdio.h> void main() { int a, b; a = 123, b = 12345; printf(" % d, % 4d, % -4d, % #o, % 4d", a, a, a, a, b); } //Output: 123, 123, 123, 0173, 12345
void main() { float a = 123.456; printf(" % f, % 8f, % 8.2f, % .2f", a, a, a, a); } //Output: 123.456000, 123.456000, 123.46, 123.46
%c controls single character output.
%s controls string output.
3.2.3 Input Functions
1. Character input function
-
A getchar enters a character from the terminal into the program.The stdio.h library must be included when using, and the preprocessing command: #includde <stdio.h>
-
The general form of a call to the character input function getcahr(): getcahr()
#include <stdio.h> int main() { char c1, c2; c1 = getchar(); c2 = getchar(); putchar(c1); putchar(c2); } //Input ab //Execute ab
2. Format input function
-
The general form of scanf function call is scanf (format control, address table column);
-
Format Control: Format Characters, Normal Characters
-
Address table column: take address operator: &; variable address representation: &a.
//vc2019 is used here, scanf is not safe enough, so vs got a scanf_s, but it looks like this code can only be compiled on vs #include <stdio.h> #pragma warning(disable:4996) int main() { int k; float f; scanf_s("%3d%*4d%f",&k,&f); printf("%d,%f",k,f); }