This is the third day of my participation in the August More text Challenge. For details, see:August is more challenging
1.1 Initial value and type conversion of variables
1.1.1 Assigning initial values to variables
It is often necessary to assign an initial value to a variable in a program in order to use it. Language programs can have a variety of methods, when the definition of an initial value of the method, this method is called initialization. The general form for assigning an initial value in a variable specification is: Type specifier variable 1= value 1, variable 2= value 2,… ;
Such as:
int a=b=c=5;
float x=3.2,y=3f,z=0.75;
char ch1='K',ch2='P';
Copy the code
Note that continuous assignments are not allowed in the specification, such as a=b=c=5 is illegal.
void main(a)
{
int a=3,b,c=5;
b=a+c;
printf("a=%d,b=%d,c=%d\n",a,b,c);
}
a<--- 3,b<-0,c<--- 5
b<--a+c
Copy the code
1.2 Conversion of variable types
The data type of a variable is convertible. There are two methods of conversion, one is automatic conversion, one is mandatory conversion.
1.2.1 Automatic conversion
Automatic conversion occurs when the quantity of different data types is mixed, and is completed automatically by the compilation system. Automatic conversion follows the following rules:
- If the types involved in the operation are different, they are first converted to the same type and then the operation is carried out.
- The conversion is carried out in the direction of increasing the data length to ensure that the accuracy is not reduced. For example, to operate on int and long, first convert an int to a long and then operate on it.
- All floating-point operations are performed in double precision. Even expressions containing only a float operation must be converted to a double before the operation is performed.
- The types char and short must first be converted to an int to participate in the operation.
- In an assignment operation, the type of the item on the right-hand side of the assignment sign is converted to the type of the item on the left-hand side if the data types on both sides of the assignment sign are different. If the length of the data type on the right is longer than that on the left, a part of the data will be lost, which will reduce the accuracy. The missing part will be rounded forward. Figure 21 shows the rules for automatic type conversion.
void main(a)
{
float PI=3.14159;
int s,r=5;
s=r*r*PI;
printf("s=%d\n",s);
}
PI<-3.14159
s<-0,r<-- 5S <--r*r*PI displays the program running resultfloat PI=3.14159;
int s,r=5;
s=r*r*PI;
Copy the code
In this example program, PI is real; S, r are integers. When s=rrPI is executed, both r and PI are converted to double, and the result is also double. But since s is an integer, the result of the assignment is still an integer, excluding the decimal part.
1.2.2 Cast
Casting is done through a type conversion operation.
Its general form is: type specifier (expression) Its function is to cast the result of an expression to the type represented by the type specifier. For example: (float) a converts a to a real type (int)(x+y) Converts the result of x+y to an integer. Both type specifiers and expressions must be parentheses (single variables may not be parentheses). For example, writing (int)(x+y) as (int)x+y is converting x to int and then adding y. 2. Either cast or automatic conversion is a temporary conversion of the length of the variable for the purpose of this operation, without changing the type of the variable defined in the data description.
main()
{
float f=5.75;
printf("(int)f=%d,f=%f\n", (int)f,f);
}
f<-5.75
Copy the code
Cast float f to int f float f=5.75; printf(“(int)f=%d,f=%f\n”,(int)f,f); This example shows that the forced conversion of f to int only works temporarily in the operation, and the type of F itself does not change. Thus, (int)f has a value of 5(with decimals removed) and f still has a value of 5.75.
1.3 Basic operators and expressions
1.3.1 Type, precedence, and associativity of operators
The number of operators and expressions in C is unusual among high-level languages. It’s the richness of operators and expressions that makes C so functional. This is one of the main features of the C language.
C operators not only have different priorities, but they are also associative. In the expression, the sequence of operations involved in the operation should not only comply with the operator priority, but also be restricted by the operator associativity, so as to determine whether to operate from left to right or from right to left. This associativity is absent from operators in other high-level languages and adds to the complexity of C.
1.3.2 Operator classification
C operators fall into the following categories:
- Arithmetic operator
Used for all kinds of numerical operations. Including the plus (+) and minus (-), multiply (*), in addition to (/), yu (or operator, %), since the increase (+ +), minus (-), a total of seven.
- Relational operator
Used for comparison operations. Include greater than (>), less than (<), equal to (==), greater than or equal to (>=), less than or equal to (<=), and not equal to (! =) six.
- Logical operator
Used for logical operations. And (&), or (| |), not (!) Three.
- Bitwise operator
The quantity involved in the operation, in terms of binary bits. Including and (&), or (|), not (~) and an exclusive or (^), left (< <), moves to the right (> >) six.
- Assignment operator
Used for assignment operators, divided into simple assignment (=), composite arithmetic assignment (+ = – =, * = / =, % =) and compound operation assignment (& =, | = ^ =, >, > =, < < =) three categories, a total of 11 kinds.
- Conditional operator
This is a triadic operator used for conditional evaluation (? :).
- Comma operator
Used to combine several expressions into a single expression (,).
- Pointer operator
For both the content (*) and address (&) operations.
- Byte count operator
Used to calculate the number of bytes (sizeof) occupied by the data type.
- Special operator
There are parentheses (), subscripts [], members (→,.). Wait for a few kinds.
1.3.3 Priority and associativity
In C language, the priority of operators is divided into 15 levels. Level 1 is the highest, level 15 is the lowest. In an expression, the higher-priority one is evaluated before the lower-priority one. However, when the priority of operators on both sides of an operation is the same, the direction of combination specified by the associativity of operators is processed.
The associativity of operators in C language can be divided into two types: left associativity (from left to right) and right associativity (from right to left). For example, arithmetic operators are associative from left to right. If there is an expression x-y+z, y should be combined with the “-” sign to perform the x-y operation, and then perform the +z operation. This left-to-right direction of binding is called left-associativity. The right-to-left direction of binding is called right-associativity. The most typical right associative operator is the assignment operator. For example, x=y=z, because of the right associativity of “=”, y=z should be performed first and then x=(y=z) operation should be performed. There are many right associative operators in C language, so we should pay attention to the difference to avoid understanding mistakes.
1.3.4 Arithmetic Operators and arithmetic expressions Basic arithmetic operators
-
Addition operator + The addition operator is a binocular operator, that is, there should be two quantities involved in the addition operation. Such as a + b, 4 + 8, etc. It’s right binding.
-
Subtraction operator “-” The subtraction operator is the binocular operator. However, “-” can also be used as a negative operator, which is a monocular operation, such as -x,-5 and so on.
-
The multiplication operator “*” is a binocular operation with left associativity.
-
The division operator “/” binocular operation is left associative.
Participate in the operation of the amount of integer, the result is also integer, to the decimal. If one of the operations is of real type, the result is of double precision real type.
void main(a){
printf("\n\n%d,%d\n".20/7.- 20/7);
printf("%f,%f\n".20.0/7.20.0/7);
}
Copy the code
Binocular operation has left associativity. Participate in the operation of the amount of integer, the result is also integer, to the decimal. If one of the operations is of real type, the result is of double precision real type.
printf("\n\n%d,%d\n".20/7.- 20/7);
printf("%f,%f\n".20.0/7.20.0/7);
Copy the code
In this example, both 20/7 and -20/7 are integers, and all decimals are eliminated. The results of 20.0/7 and -20.0/7 are of real form because real numbers are involved in the operation.
- The mod operator “%” binocular operation has left associative property.
The quantities required to participate in the operation are of integral type. The result of the remainder operation is the remainder of the division of two numbers. void main(){ printf(“%d\n”,100%3); } binocular operation, with left associative. The % remainder operator requires that all the quantities involved in the operation be integers. This example prints the remainder 1 of 100 divided by 3.
1.3.5 Increment 1, decrement 1 operators
The increment 1 operator, denoted “++”, increments the value of the variable by 1. The decrement 1 operator, denoted by –, decrement the value of a variable by 1. The increment 1 and decrement 1 operators are monocular and have right associative property. It can take the following forms: ++ I I increments by 1 before participating in other operations. — I I decrement by 1 before participating in other operations. I ++ I is going to increment I by 1. I — the value of I decrement by one after I participates in the operation. Mistakes in understanding and using are I ++ and I –. Especially when they occur in more complex expressions or statements, they are often difficult to understand and should be carefully analyzed.
void main(a){
int i=8;
printf("%d\n",++i);
printf("%d\n",--i);
printf("%d\n",i++);
printf("%d\n",i--);
printf("%d\n",-i++);
printf("%d\n",-i--);
} i<-- 8 -
i<--i+1
i<--i- 1
i<--i+1
i<--i- 1
i<--i+1
i<--i- 1 int i=8;
printf("%d\n",++i);
printf("%d\n",--i);
printf("%d\n",i++);
printf("%d\n",i--);
printf("%d\n",-i++);
printf("%d\n",-i--); The initial value of I is zero8
第2Do I add1The output after is therefore9; The first3Line to reduce1The output after is therefore8; The first4Line output I is8After add1(for9); The first5Line output I is9After will be a reduction of1(for8); The first6Line of the output- 8 -After add1(for9); The first7Line of the output9 -After will be a reduction of1(for8)
void main(a){
int i=5,j=5,p,q;
p=(i++)+(i++)+(i++);
q=(++j)+(++j)+(++j);
printf("%d,%d,%d,%d",p,q,i,j);
}
i<-- 5,j<-- 5,p<-0,q<-0
i+i+i--->p,i+1-->i,i+1-->i,i+1-->i
j+1->j,j+1->j,j+1->j,j+j+j->q int i=5,j=5,p,q;
p=(i++)+(i++)+(i++);
q=(++j)+(++j)+(++j);
Copy the code
In this program, P=(I ++)+(I ++)+(I ++) should be understood as the sum of three I’s, so P is 15. And then I increments by 1 three times which is the same thing as adding 3 so the final value of I is 8. Q =(++j)+(++j)+(++j) should be understood as q increases by 1 first, and then participates in the operation. Since q increases by 1 three times, the value is 8, and the sum of the three 8s is 24, while the final value of j is still 8. Arithmetic expressions An expression is a combination of constants, variables, functions, and operators. An expression has a value and its type equal to the value and type of the result of evaluating the expression. Expressions are evaluated in the order specified by the precedence and associativity of the operators. Individual constants, variables, and functions can be considered special cases of expressions.
1.4 Arithmetic Expressions
Is an expression joined by an arithmetic operator and parentheses. The following is an example of an arithmetic expression:
a+b(a*2) /c(x+r)*8- (a + b) /7++isin(x)+sin(y) (++i)-(j++)+(k--)
Copy the code
1.4.1 Assignment operators and assignment expressions
Simple assignment operator and expression. The simple assignment operator is denoted as “=”. Expressions joined by “=” are called assignment expressions. The general form is: variable = expression
Such as:
x=a+b
w=sin(a)+sin(b) y=i+++--j the function of an assignment expression is to evaluate the value of the expression and assign it to the left-hand variable. Assignment operators are right associative. Therefore a = b = c =5A =(b=(c=)5))
Copy the code
In other high-level languages, assignment forms a statement called an assignment statement. In C, “=” is defined as the operator to form assignment expressions. An assignment expression occurs wherever an expression can occur. For example, x=(a=5)+(b=8) is legal. What it means is that you give 5 to a, 8 to B, add a and b, and give x, so x should be equal to 13.
Assignment statements can also be made in C. According to C, any expression that ends with a semicolon is a statement. Therefore, such as x = 8; A = b = c = 5; Both are assignment statements, which we have used extensively in the previous examples. If the data types on both sides of the assignment operator are different, the system automatically converts the type on the right of the assignment to the type on the left.
Specific provisions are as follows:
- The real type is given to the integral type, and the decimal part is eliminated. Example 2.9 above illustrates this situation.
- An integer is given a real type. The value is not changed, but it is stored as a floating point, that is, by increasing the fractional part (which has a value of 0).
- The character type is assigned to the integer type. Since the character type is one byte and the integer type is two bytes, the ASCII code value of the character is placed in the lower eight bits of the integer and the higher eight bits are 0.
- Integers are given character types, and only the lower eight bits are given character quantities.
void main(a){
int a,b=322;
float x,y=8.88;
char c1='k',c2;
a=y;
x=b;
a=c1;
c2=b;
printf("%d,%f,%d,%c",a,x,a,c2);
}
int a,b=322;
float x,y=8.88;
char c1='k',c2;
printf("%d,%f,%d,%c",a=y,x=b,a=c1,c2=b);
Copy the code
This example shows the rules for type conversion in the assignment operation described above. If a is an integer, give the value of y of real type 8? X is a real type, and b is given the value 322, then the decimal part is added. When c1 is assigned to a, the character type becomes an integer. When C2 is assigned to b, the lower eight bits of b become a character type (the lower eight bits of B are 01000010, which is 66 in decimal and corresponds to character B in ASCII code).
1.4.2 Compound assignment characters and Expressions
Compound assignment can be formed by adding other binary operators before the assignment “=”. Such as + = – =, = / = % =, <, < =, > > =,, & = ^ =, | =. The general form of a compound assignment expression is: The variable binocular operator = expression which is equivalent to the variable = variable operator expression for example: A +=5 is equivalent to a=a+5x=y+7 is equivalent to x=x*(y+7)r%=p is equivalent to r=r%p Compound assignment. The comma operator and comma expression are in
1.4.3 Comma operator
The C comma “, “is also an operator, called the comma operator. Its function is to join two expressions to form a single expression, called a comma expression. The general form is: Expression 1, expression 2. The evaluation process is to evaluate the two expressions separately, and the value of expression 2 is used as the value of the entire comma expression.
void main(a){
int a=2,b=4,c=6,x,y;
x=a+b,y=b+c;
printf("y=%d,x=%d",y,x);
}
a<-2 -,b<-4 -,c<-- 6,x<-0,y<-0
x<--a+b,y<---b+c
Copy the code
In this case, y is equal to the value of the entire comma expression, which is expression 2, and x is the value of the first expression. There are two more notes about comma expressions: 1. Expressions 1 and 2 in the general form of comma expressions can also be comma expressions. For example, expressions 1, (2, 3) form a nested case. So you can extend a comma expression to the following form: expression 1, expression 2,… The value of the entire comma expression of expression n is equal to the value of expression n. 2. When a comma expression is used in a program, it is usually required to evaluate each expression within the comma expression separately, but not necessarily to require the value of the entire comma expression. 3. Not all commas are used to form comma expressions. For example, in the variable description, the comma is used only as an interval between variables in the function argument list.
1.5 summary
1.5.1 Data types of C
Basic type, construct type, pointer type, null type
1.5.2 Classification and characteristics of basic types
Type descriptor Byte Numeric range character typechar 1C Character set basic integerint 2 - 32768.~32767Short integershort int 2 - 32768.~32767Long integerlong int 4 - 214783648.~214783647Unsigned typeunsigned 2 0~65535Unsigned long integerunsigned long 4 0~4294967295Single precision realfloat 4 3/4E-38~3/4E+38Double precision real typedouble 8 1/7E-308~1/7E+308
Copy the code
1.5.3 Constant suffixes
L or L long integer U or U unsigned number F or F floating point number
1.5.4 Constant types
Integer, long integer, unsigned, floating point, character, string, symbol constant, escape character.
1.5.5 Data type conversion
· Automatic conversion In the mixed operation of different types of data, the system automatically realizes the conversion from the low-byte type to the multi-byte type. When different types of quantities are assigned to each other, the system automatically converts the type on the right of the assignment to the type on the left. · Cast The cast is performed by the cast operator.
1.5.6 Operator precedence and associativity
In general, the monocular operator takes precedence and the assignment operator takes precedence. Arithmetic operators have higher precedence, and relational and logical operators have lower precedence. Most operators are left associative, monocular operators, triadic operators, assignment
1.5.7 expression
An expression is an expression composed of constants, variables, and functions connected by an operator. Each expression has a value and a type. Expressions are evaluated in the order specified by the precedence and associativity of the operators.