Assignment operators
An assignment symbol "=" is an assignment operator whose purpose is to assign a data to a variable. The function of "a=3" is to perform an assignment (or an assignment operation) once. Assign the constant 3 to the variable A. You can also assign the value of an expression to a variable.
Type conversions in the assignment process
If the type on either side of the assignment operator is inconsistent, but both are numeric or character types, the type conversion is performed automatically when the value is assigned.
1) When assigning floating-point data (including single and double precision) to an integer variable, discard its fractional portion.
2) When assigning integer data to floating-point variables, the values are the same, but stored in the variable as an exponent.
3) When assigning a double type of data to the float variable, be aware that the range of values cannot overflow.
4) The character data is assigned to the integer variable, and the ASCII code of the character is assigned to the integer variable.
5) Assign an int, short, or long data to a char variable, and send it as low as 8 bits intact to the char type variable (truncation occurs). For example
short int i=289;
char c;
C=i; Assigns an int type data to a char type variable
The value of the assignment is shown in Figure 2.8. For convenience, this is illustrated by the fact that an int data occupies two bytes (16 bits).
Figure 2.8
6) signed (signed) data to the same length of the unsigned (unsigned) variable, the contents of the storage unit is copied (even the original symbol bit is also transmitted as a value).
Example 2.5 transmits signed data to an unsigned variable.
- #include <iostream>
- Using namespace std;
- int main( )
- {
- unsigned short a;
- short int b=-1;
- A=b;
- cout<<"a="<<a<<endl;
- return 0;
- }
Run the result as
a=65535
The value assigned to B is-1, how does it get 65535? Take a look at the assignment shown in Figure 2.9.
Figure 2.9
1 of the complement form is 1111111111111111 (that is, all 16 bits are 1), transfer it to a, and a is an unsigned variable, 16 bits all 1 is the decimal 65535. If B is positive and is between 0~32767, the value is unchanged after the assignment.
The assignment of different types of integer data is, in the final analysis, one: to be transmitted directly in storage units.
C and C + + use flexibility, when assigning values between different types of data, often unexpected results, and the compilation system does not prompt errors, relying on the programmer's experience to find problems. This requires programmers to be aware of the cause of the problem in order to quickly troubleshoot.
Compound assignment operators
Adding additional operators before the assignment "=" can form a composite operator. If you add a "+" operator before "=", it becomes the compound operator "+ =". For example, you can have
A+=3 equivalent to A=a+3
X*=y+8 equivalent to x=x* (y+8)
X%=3 equivalent to x=x%3
As an example of "a+=3", it is equivalent to making a perform a self-add 3 operation. That is, a plus 3, and then assign to a. Similarly, the function of "x*=y+8" is to multiply X by (y+8) and then assign to X.
For ease of memory, this can be understood as:
- a+= B (where a is a variable and B is an expression)
- a+= B (Move underlined "A +" to the right of "=")
- A = a + B (on the left side of "=" to fill in the variable name a)
Note that if B is an expression that contains several items, it is equivalent to having parentheses. Such as
- X%= y+3
- x%= (Y+3)
- x = percent (y+3) (not mistaken for x=x%y+3)
Any two-yuan (two-mesh) operator can be combined into a compound assignment with an assignment. C + + can use the following compound assignment operators:
+=,-=,*=,/=,%=,<<=,>>=,&=,^=,|=
The latter 5 are related to bit operations.
C + + uses this compound operator, one is to simplify the program, so that the program is refined, and second, in order to improve the efficiency of the compilation (this way with the "inverse Polish" style, in favor of compiling, can produce high quality object code). Professional programmers in the program commonly used compound operators, beginners may not be used to, or can not use or less.
An assignment expression
The
is called an assignment expression by the assignment operator to concatenate a variable and an expression. Its general form is:
< variable > < assignment operator > < expression;
such as "a=5" is an assignment expression. The procedure for solving an assignment expression is to first seek the value of the expression on the right side of the assignment operator, and then assign the variable to the left of the assignment operator. An expression should have a value. The identifier on the left side of the assignment operator is called an "Lvalue" (left value, abbreviated to Lvalue). Not all objects can be left-valued, variables can be left-valued, and expression a+b cannot be an lvalue, and a constant variable cannot be an lvalue, because a constant variable cannot be assigned a value. The expression
appears to the right of the assignment operator is called an rvalue (right value, abbreviated rvalue). It is clear that the lvalue can also appear to the right of the assignment operator, so that the left value can be the right value. such as:
int a=3,b,c;
b=a;//B is an lvalue
c=b;//B is also an rvalue
assignment expression in expression, or an assignment expression. Examples of assignment expressions such as
a= (b=5)
are:
a=b=c=5 (Assignment expression values are 5,a,b,c values of 5)
a=5+ ( c=6) (The expression value is 11,a value is 11,c value is 6)
a= (b=4) + (c=6) (expression value 10,a = 10,b equals 4,c equals 6)
a= (b=10)/( c=2) (expression value of 5,a equals 5,b equals 10,c equals 2)
Parse the following assignment expression:
(a=3*5) =4*3
An assignment expression should be enclosed as an lvalue, and a syntax error would occur if written as follows:
A=3*5=4*3
because 3*5 is not an lvalue, it cannot appear to the left of the assignment operator. An
Assignment expression can also contain a compound assignment operator. such as
A+=a-=a*a
is also an assignment expression. If the initial value of a is 12, the solution for this assignment expression is as follows:
- First, the "A-=a*a" operation, which is equivalent to a=a-a*a=12-144=-132.
- Then the "a+=-132" operation, which is equivalent to a=a+ (-132) =-132-132=-264.
C + + assignment operators and assignment expressions