Home>

Assignment operator

The assignment symbol "=" is the assignment operator.Its role is to assign a data to a variable.For example, the role of "a=3" is to perform an assignment operation (or an assignment operation). Assign the constant 3 to the variable a. You can also assign the value of an expression to a variable.

Type conversion during assignment

If the types on both sides of the assignment operator are inconsistent,When both are numeric or character,Type conversion is performed automatically during assignment.

1) When assigning floating-point data (including single and double precision) to integer variables,Discard the decimal part.

2) When assigning integer data to a floating-point variable,The value does not change.But it is stored into the variable in exponential form.

3) When assigning a double-type data to a float variable, pay attention to the range of values ​​cannot overflow.

4) Character data is assigned to integer variables,Assign a character's ascii code to an integer variable.

5) Assign an int, short, or long data to a char type variable, and only send the lower 8 bits to the char type variable (truncation occurs). E.g

short int i=289;
  char c;
  c=i;//Assign an int data to a char variable

The assignment is shown in the figure.for your convenience,A case where an int type data occupies two bytes (16 bits) will be described.

6) Assign signed (signed) data to unsigned (unsigned) variables of the same length, and copy the contents of the storage unit as it is (even the original sign bit is also transmitted as a value).

[Example] Transfer signed data to unsigned variables.

#include<iostream>
using namespace std;
int main ()
{
 unsigned short a;
 short int b=-1;
 a=b;
 cout<<"a ="<<a<endl;
 return 0;
}

The result is

a=65535

The value assigned to b is -1. How can I get 65535?See the assignment shown in the figure.

The -1's complement form is 1111111111111111 (that is, all 16 binary bits are 1), and it is passed to a, and a is an unsigned variable.All 16 digits are 65535 in decimal. If b is a positive value and is between 0 and 32767, the value is unchanged after the assignment.

In the end, the assignment between different types of integer data is one:transfer directly according to the storage form in the storage unit.

C and C++ are flexible to use,When assigning values ​​between different types of data,Unexpected results often occur,The compilation system does not prompt an error,It's all up to the programmer's experience to find the problem.This requires programmers to understand the cause of the problem,For quick troubleshooting.

Compound assignment operator

Add another operator before the assignment "=",Can constitute compound operators.If you add a "+" operator before "=", it becomes a compound operator "+ =". For example, you could have

a +=3 is equivalent to a=a + 3 x *=y + 8 is equivalent to x=x * (y + 8) x%= 3 is equivalent to x=x%3

Take "a +=3" as an example.It is equivalent to making a perform a self-incrementing operation of 3. That is, add a to 3 before assigning it to a. Similarly, the role of "x *=y + 8" is to multiply x by (y + 8) and assign it to x.

To make it easier to remember,Can be understood as:

a +=b (where a is a variable and b is an expression) a +=b (move the underlined "a +" to the right of "=") a=a + b (add the variable name a to the left of "=")

Note that if b is an expression containing several terms,It is equivalent to having parentheses.Such as

x%= y + 3 x%= (y + 3) x=x%(y + 3) (don't mistake x=x%y + 3)

Any binary (binocular) operator can be combined with an assignment to form a compound assignment.C++ can use the following compound assignment operators:

+ =,-=, * =,/=,%=,<<=,>

The last five are related to bit operations.

The reason why C++ uses this composite operator,One is to simplify the process,Refine the program,The second is to improve the compilation efficiency (this way of writing is consistent with the "reverse Polish" formula, which is conducive to compilation,Can produce higher quality object code). Professional programmers often use compound operators in their programs.Beginners may not be used to it,It can also be used less or less.

Assignment expression

An expression that connects a variable with an expression by an assignment operator is called an "assignment expression." Its general form is:

 <variable><assignment operator><expression>

For example, "a=5" is an assignment expression.The process of solving an assignment expression is to first find the value of the "expression" to the right of the assignment operator, and then assign it to the variable to the left of the assignment operator.An expression should have a value.The identifier to the left of the assignment operator is called a "left value" (short for lvalue). Not all objects can be used as lvalues,Variables can be used as lvalues,The expression a + b cannot be used as an lvalue,Constants cannot be used as lvalues,Because constant variables cannot be assigned.

The expression that appears to the right of the assignment operator is called a "right value" (rvalue). Obviously lvalues ​​can also appear to the right of the assignment operator,Therefore, lvalues ​​can be used as rvalues.Such as:

 int a=3, b, c;
  b=a;//b is an lvalue
  c=b;//b is also an rvalue

The "expression" in an assignment expression can be an assignment expression again.Such as

 a=(b=5)

The following are examples of assignment expressions:

a=b=c=5 (assignment expression value is 5, a, b, and c are 5)
 a=5 + (c=6) (expression value is 11, a value is 11, and c value is 6)
 a=(b=4) + (c=6) (expression is 10, a is 10, b is 4, and c is 6)
 a=(b=10)/(c=2) (expression value is 5, a is equal to 5, b is equal to 10, c is equal to 2)

Please analyze the following assignment expressions:

 (a=3 * 5)=4 * 3

Assignment expressions should be parenthesized when used as an lvalue,If you write it like this, you will get a syntax error:

 a=3 * 5=4 * 3

Because 3 * 5 is not an lvalue,It cannot appear to the left of the assignment operator.

Assignment expressions can also contain compound assignment operators.Such as

 a +=a-=a * a

It is also an assignment expression.If the initial value of a is 12, this assignment expression is solved as follows:

First perform the operation "a-=a * a", which is equivalent to a=a-a * a=12-144=-132.

Then perform the operation "a + =-132", which is equivalent to a=a + (-132) =-132-132=-264.

C++ comma operator and comma expressionc++ uses assignment expressions as a type of expression,Making assignments not only appear in assignment statements,It can also appear in other statements (such as output statements, loop statements, etc.) as expressions. This is a manifestation of the flexibility of the C++ language.

Note that when using a cout statement to output the value of an assignment expression,To enclose the assignment expression in parentheses,If you write "cout<< a=b;" a compilation error will occur.

C++ provides a special operator-the comma operator.Use it to connect two expressions.Such as

 3 + 5, 6 + 8

Called a comma expression,Also called "sequential evaluation operator." The general form of a comma expression is:

 Expression 1, expression 2

The comma expression is solved as follows:expression 1 is solved first, then expression 2 is solved. The value of the entire comma expression is the value of expression 2. Eg, comma expression

 a=3 * 5, a * 4

The assignment operator takes precedence over the comma operator. Therefore, a=3 * 5 should be solved first (that is, "a=3 * 5" is used as an expression). After calculation and assignment, the value of a is 15 and then a * 4 is solved to obtain 60. The value of the entire comma expression is 60.

A comma expression can be combined with another expression to form a new comma expression.Such as

 (a=3 * 5, a * 4), a + 5

The general form of a comma expression can be expanded to:

 Expression 1, Expression 2, Expression 3,…, Expression n

Its value is the value of expression n.

The comma operator is the lowest level of all operators.Therefore, the following two expressions do different things:

 x=(a=3, 6 * 3)
  x=a=3, 6 * a

In fact, a comma expression is nothing more than a "concatenation" of several expressions. in many circumstances,The purpose of using a comma expression is just to get the value of each expression separately,It is not necessary to get and use the value of the entire comma expression,Comma expressions are most commonly used in loop statements (for statements).

When using cout to output the value of a comma expression,To enclose the comma expression in parentheses,Such as:

 cout<(3 * 5, 43-6 * 5, 67/3)<<endl;

C and C++ languages ​​have strong expression skills,One important aspect is that its expression types are rich,Operators are powerful,So flexible to use,Adaptable.

c
  • Previous Method to completely delete the SQL Server registry
  • Next C # reflection (Reflection) use analysis