Identifier: a string used in a program to name variables and functions
The rules for identifiers are:
First: the identifier is composed of numbers, letters and underscores,
Second: numbers cannot start
Third: you can't have the same name as the keyword
Fourth: case sensitive
Variables and constants:
Variable: the amount of change in the value of the program during operation
Constant: the amount of value that will not change during the running of the program
Setting and initialization of variables:
Format:
Data type variable name; int num; char num; float num; double num; int num[5]; int *num; struct mystruct{}num; union mysunion{}num; enum myenum{}enum;
This format contains the following key points to note:
1. Variable name - the name of a variable is a legal identifier
2. Data type of variable -- except for the variable name, the rest is the data type of the variable
3. The type of data stored in a variable -- the basic data type that appears when determining a variable is the type of data stored, except for structures, commons and enumerations
Input / output:
printf("format controller", output list)
When an operation occurs in printf, the order of execution is from right to left
scanf("format controller", input list)
Format controller:
%d:
%f:
%c:
%s:
%o:
%x:
%u:
%l:
%e:
%lu:
%p:
Transfer character:
Symbols beginning with \ are called escape characters
\n: Line feed
\t: A tab
Operator:
Operator: it is the symbol with each function concerned with data processing
Operands are also included in operations involving operators
Operand: is the expression or data involved in the operation
Expression: an expression consisting of operands and operators
Classification of operators:
Divided from operands:
unary operator
Binocular operator:
Ternary operator:
From function division:
Comma operator:,
Play the role of segmentation
int num;int m;int num,m;
#include <stdio.h> int main() { int m=10,n=20; int num=(1,2,3,4,5); //int num1=1,2,3,4,5;// wrongful printf("m=%d\tn=%d\n",m,n); printf("m=%d\t\n",num); //printf("m=%d\t\n",num1); return 0; }
Assignment operator:=
The assignment operator is used to assign the expression or data on the right of the assignment symbol to the variable on the left
Lvalue: (lvalue is invalid)
Lvalues are expressions used to indicate an object. The simplest lvalue is the variable name. Lvalues are called "left" (L for left) because an lvalue represents an object, which can appear to the left of the assignment operator, such as "left expression = right expression".
Other expressions (those that represent a value but do not specify an object) are similarly referred to as rvalue s. An R-value is an expression that can appear to the right of an assignment operator instead of to the left. For example, constants and arithmetic expressions.
Left expression = right expression tip: be sure to distinguish between = (assignment symbol) and = = (equals sign)
Arithmetic operator:
Symbol: + - * /%
There are two monocular operators (positive and negative) and five binocular operators (multiplication, division, modulo, addition and subtraction), among which monocular operators have the highest priority.
The modulus operator% means to calculate the remainder obtained by dividing two integers, that is, to find the remainder.
/: quotient operation, which only obtains the quotient of two values
1-1/2+1/3-1/4+1/5
Implicit conversion: implicit conversion will be involved if arithmetic operation occurs in the process of program execution
int m=10,n=3; 10/3=3 int a=10; float b=2.2; a*b;
For binocular arithmetic operators, when the two variables involved in the operation are of the same type, no type conversion occurs, and the operation results will be accommodated by the types of the variables involved in the operation. Otherwise, type conversion will occur to make the types of the two variables consistent.
The conversion rules are as follows:
Convert to high-precision type
First, promote char, bool, short and other types to int (or unsigned int, depending on the symbolism of the original type);
If there is a variable with the type of long double, the other variable will be converted to the type of long double;
Otherwise, if one variable type is double, the other variable will be converted to double;
Otherwise, if one variable type is float, another variable will be converted to float type;
Otherwise (that is, the two variables involved in the operation are of integer type):
If the two variables have the same sign, convert the type with smaller bit width to the type with larger bit width;
Otherwise, if the bit width of the unsigned variable is not less than that of the signed variable, the signed number is converted to the type corresponding to the unsigned number;
Otherwise, if the type of the signed operand can represent all the values of the unsigned operand type, convert the unsigned operand to the type corresponding to the signed operand;
Otherwise, the signed number is converted to the corresponding unsigned type.
#include <stdio.h> int main() { int m=10,n=20; //addition printf("and=%d\n",m+n); //subtraction printf("difference=%d\n",m-n); //multiplication printf("product=%d\n",m*n); //division printf("merchant=%d\n",m/n); //Mod printf("remainder=%d\n",m%n); return 0; }
%: remainder operation to obtain the remainder of two numbers
Divisor ÷ divisor = quotient...... remainder
The sign of the result obtained by the remainder operation is consistent with the divisor
#include <stdio.h> int main() { int m=20,n=6; int a=-20,b=6; int c=-20,d=-6; int f=20,g=-6; //Mod printf("m yes n Seeking remainder=%d\n",m%n); printf("a yes b Seeking remainder=%d\n",a%b); printf("c yes d Seeking remainder=%d\n",c%d); printf("f yes g Seeking remainder=%d\n",f%g); return 0; }
Relational operators:
greater than
=Greater than or equal to
< less than
< = less than or equal to
==Equals
!= Not equal to
The expression involving relational operators will have a result returned, and the result is true or plus
C language stipulates that non-0 is true and 0 is false (usually 1 is used to identify true)
5 > 6 ×
5 < 6 √
#include <stdio.h> int main() { int m=20,n=6; int result=m>n; int res=m<n; printf("result=%d\n",result); printf("res=%d\n",res); printf("m!=n:%d\n",m!=n); printf("m=n:%d\n",m==n); return 0; }
Tip: relational expressions usually exist as conditions for branch statements in process control statements
if(m>n) printf("m>n\n"); else printf("m<n\n");
Logical operators:
Operator function
&&Logic and
||Logical or
! Logical non
There are three logical operators:
(expression 1) & & (expression 2): if the expressions on both sides of & & are true, the result is true, otherwise it is false
#include <stdio.h> int main() { int m=20,n=6; if((m>n) && (n>m)) /*if As a conditional statement, when the result in parentheses after if is true, execute the statement in braces under if, otherwise execute the statement in braces under else*/ { printf("if The condition of the statement is true\n"); } else { printf("if The condition of the statement is false\n"); } return 0; }
(expression 1) | (expression 2): if one of the expressions on both sides of & & is true, the result is true, otherwise it is false
#include <stdio.h> int main() { int m=20,n=6; if((m>n) || (n>m)) /*if It is a conditional statement. When the result in parentheses after if is true, execute the statement in braces under if, otherwise execute the statement in braces under else*/ { printf("if The condition of the statement is true\n"); } else { printf("if The condition of the statement is false\n"); } return 0; }
! (expression): if the expression is true, the result is false, if the expression is false, the result is true
#include <stdio.h> int main() { int m=20,n=6; if(!((m>n) && (n>m))) /*if It is a conditional statement. When the result in parentheses after if is true, execute the statement in braces under if, otherwise execute the statement in braces under else*/ { printf("if The condition of the statement is true\n"); } else { printf("if The condition of the statement is false\n"); } return 0; }
Bitwise operator:
Operator function
~Bitwise non
&(binocular) position by position and
|Bitwise OR
^Bitwise XOR
< < shift left bit by bit
Shift right bit by bit
&: bitwise and operator. There are two operands participating in bitwise operation
All one out of one, there are 0 out of 0
1010
& 1101
1000
#include <stdio.h> int main() { int m=10,n=13; printf("m&n=%d\n",m&n); return 0; }
|: bitwise OR operator. There are two operands participating in bitwise operation
There are 1 out of 1, all 0 out of 0
1010
1101 |
---|
1111
#include <stdio.h> int main() { int m=10,n=13; printf("m|n=%d\n",m|n); return 0; }
~: bit by bit inversion. There are 1 operands participating in bit by bit inversion
0 to 1 to 0
~1101=0010
^: XOR operator. There are two operands participating in XOR operation
The difference is 1 and the same is 0
1010
^ 1101
0111
#include <stdio.h> int main() { int m=10,n=13; printf("m^n=%d\n",m^n); return 0; }
: shift right operator. There are two operands, usually metadata on the left and the number of bits moved on the right
data >> n
The result of shifting n bits to the right is data. The data on the right is removed and N is supplemented by N zeros on the left
10101100 >> 4 = 00001010
< <: shift left operator. There are two operands, usually metadata on the right, and the number of bits moved on the left
n << data
The result of shifting n bits to the left is data data (the number of bits is within 32), and N zeros are added to the right
4<<10101100 = 101011000000
10101100 >> 4 = 00001010
Tips:
The left shift operation is equivalent to multiplying 2n on the basis of metadata, and the right shift operation is equivalent to dividing 2n on the original data for quotient
Self increasing and self decreasing operators:
++: autoincrement operator
–: subtraction operator
Self increasing and self decreasing operators increase or decrease in units of 1
Pre increment and self decrement: pre is the operation in front of a variable or expression
The pre operator is characterized by first adding (subtracting) 1 by itself, and then participating in other operations
Suffix self increasing and self decreasing: the post operator is in front of the variable or expression
The post operator is characterized by participating in other operations first, and then adding (subtracting) 1 by itself
#include <stdio.h> int main() { int num=10; int m=num++;//First assign the value of num to m, and then num itself + 1 int n=++num;//First add num itself + 1, and then assign the value of num to n printf("m=%d\tn=%d\n",m,n); printf("num=%d\n",--num);//Put num-1 before printing printf("num=%d\n",num--);//Print first, then return num-1 to 0; }
Compound operator:
The so-called compound operator refers to the combination operator generated by the combination of all operators and assignment operators
==
+=: add before assign
-=: subtraction before assignment
/=: Division before assignment
*=: multiply before assign
&=: first and then assignment
|=: first or last assignment
int num=10;
num+=1; Equivalent to num=num+1;
sizeof operator:
Find the size of variables and data types
#include <stdio.h> int main() { int num=10; printf("sizeof(int)=%lu\n",sizeof(int)); printf("sizeof(num)=%lu\n",sizeof(num)); return 0; }
Ternary operator:
(expression)? (result 1): (result 2)
If the result of the expression is true, the result of the result area is 1, otherwise the result is 2
Note: only one value is taken, and the other does not do any operation
#include <stdio.h> int main() { int a = 0, b = 0, c = 0,max=0; scanf("%d%d%d", &a, &b, &c); max = (a > b) ? a : b; max = (max > c) ? max : c; printf("max=%d\n", max); return 0; }
Priority of operator:
Type conversion:
Cast type:
Human intervention to convert the type of data from one type to another
Format:
(data type after cast) variable name
int num=1.2345;
Implicit type conversion:
It mostly refers to the conversion from the following data type to the previous data type due to the inconsistency of data types during the operation of the program
#include <stdio.h> #include <stdlib.h> int main() { int num = 1.123;//Implicit type conversion printf("num=%d\n", num); printf("num=%d\n", (int)1.123);//Cast type printf("num=%d\n", 1.123);//Incorrect form. You cannot use% d to directly output floating-point data. The data will be lost in system("pause"); return 0; }
Please use #define to define a macro to identify the number of seconds in a year
#define second 365*24*60*60L
Int: the int type is an integer type. The maximum data space occupied is 4 bytes (32-bit environment) and 2 bytes in 16 bit environment
Long: long can also store integers, but long is at least 4 bytes (16 bit and 32-bit environments are the same), and 8 bytes under 64 bit
#include <stdio.h> #include <stdlib.h> int main() { int num = 123456789; long int num1 = 123456789L; float m = 1.234f; printf("num=%d\tnum1=%d\n", num, num1); system("pause"); return 0; }