Table of Contents
Data types, operators and expressions
1.Data type
Basic data types include
Ranges
2. Constants and variables
constant
variable
Define variables
Classification of variables
Why use variables
3.Input and output
Format output function printf()
Output type when printing
Format input function scanf()
4. Arithmetic operators and arithmetic expressions
Two unary operators and five binary operators
Increment/decrement operators
Comma operator and comma expressions
Find byte operator
sizeof operator
strlen function
5.getchar, getch, putchar functions
6. Relational operators and relational expressions
priority
7. Logical operators and logical expressions
8.if statement
9. Conditional operators
10.switch statement
Data types, operators and expressions
1.Data type
Basic data types include
- integer type
- Basic integer type (int)
- short
- long
- long long
- Character type (char)
- Boolean type (bool)
- floating point type
- Single precision floating point type (float)
- Double precision floating point type (double)
Value range
Data type | Value range | Bytes |
char | – 128~127 | 1 byte (8 bits) |
short | -32768~32767 | 2 bytes (16 bits) |
int | -2147483648~2147483647 | 4 bytes (32 bits) |
long | -2147483648~2147483647 | 4 bytes (32 bits) |
long long | -9223372036854775808~9223372036854775807 | 8 bytes (64 bits) |
float | 1.175*10^-38~3.402*10^38 | 4 bytes (32 bits) |
double | 2.225*10^-308~1.797*10^308 | 8 bytes (64 bits) |
2. Constants and variables
constant
Constants include:
- literal constant
- Any type: boolean, integer, float, character, string, etc.
- const modified constant
- const modifies ordinary variables, resulting in a constant being defined, but this constant can be modified through a pointer
- #define modified identifier constant
- The defined identifier does not occupy memory and is just a temporary symbol. This symbol will no longer exist after precompilation.
- Use: #define MAXSIZE 100
- All MAXSIZE in the code will be replaced with 100
- enum constants
- enum enumeration type name {constant 1, constant 2, constant 3, …};
Variable
Define variables
int a=10;
Classification of variables
Variables are divided into: global variables and local variables
- The life cycle of local variables is: the life cycle begins when entering the scope and ends when it exits the scope.
- The life cycle of global variables is: the life cycle of the entire program.
When global variables and local variables exist at the same time, local variables are executed first.
The output result of this code is 15
Why use variables
When inputting, you must first define variables, and when looping, you also need to increment or decrement variables…
The auto-increment of the variable i defined here can output a series of increasing numbers.
3.Input and Output
Format output function printf()
Format:
printf(“Format control string”, output list);
Format control: A string enclosed in double quotes that specifies the output format.
Output table column: A list of data to be output, separated by commas, and can be any legal expression.
Output type when printing
Data type | Print type |
char | %c |
short | %hd |
int |
%d (decimal) %o (octal) %x (hex) |
long | %ld |
long long | %lld |
float | %f |
double | %lf |
Data type | Print type | ||||
Unsigned number (decimal) | %u | ||||
String | %s | ||||
Print a Percent sign (%) | %% | ||||
float The type retains two decimal places (a total of five decimal places including the decimal point) | %5.2f | ||||
Output real numbers in standardized exponential form (standard retains six decimal places) | %e |
Monocular positive | + |
Monocular negative | – |
Multiply | * |
Except | / |
Modulo | % |
Addition | + |
Subtraction | – |
- Operators are executed in order from high to low according to their precedence.
- If one of the two numbers operated with +, -, *, / is of double type, the result will also be of double type. This is because all real numbers are operated on the double type.
increment/decrement operator
int a=1;
a=a + 1;
printf(“%d”,a);
The statement expressing a=a + 1; here can also be written as:
- a + =1;
- a + + ;
- + + a;
But there is still a difference between a + + and + + a
int a=1;
int b=a + + ;
printf(“%d\
“,a);printf(“%d\
“,b);int c= + + a;
printf(“%d\
“,a);printf(“%d\
“,c);
Comma operator and comma expression
//Output 9 9 8
Mainly to make better use of the space in one line and make the code more compact
Look for byte operator
sizeof operator
The sizeof operator is used to calculate the number of bytes occupied by the data type, which is to calculate the space occupied by the data. The unit is bytes, and it is an operator for finding the number of bytes.
For strings, it ends with /0’, and the sizeof operator will also calculate /0’
//Output 8,9,6
strlen function
Remember to add the header file #include
The strlen() function ends when encountering \0’
//Output 3 9
5.getchar, getch, putchar function
- The header files required by getchar() and getch() are different. Generally, getchar() is used.
- The getchar() function can only receive one character, and its function return value is the character obtained from the input end.
- putchar: The putchar function prints its parameters
putchar(a); \t //equivalent to printf printf("%c",a);
6. Relational operators and relational expressions
Greater than | > | ||||
Greater than or equal to | >= | ||||
Less than | < | ||||
Less than or equal | <= | ||||
Not equal | != | ||||
Equal | == |
a | b | !a | !b | a & amp; & amp;b | a||b |
True | True | False | False | True | True |
True | False | False | True | False | True |
False | True | True | False | False | True |
False | False | True | True | False | False |
8.if statement
usage:
if (expression 1){
Statement 1;
}
else if (expression 2){
Statement 2;
}
else{
Statement 3;
}
9.Conditional operator
The conditional operator (?:) is the only ternary operator, which can effectively reduce the amount of code
10.switch statement
usage:
switch(expression){
case constant 1: statement 1
case constant 2: statement 2
default: statement n
break;
}
If a constant value that is not included in the switch statement is entered, the statement after default will be executed.