The usage of the assignment operator and the comma operator in C + + _c language

Source: Internet
Author: User
Tags constant numeric value

Assignment operator

The assignment symbol "=" is the assignment operator, which is to assign a data to a variable. The function of "a=3" is to perform an assignment operation (or assignment operation). Assign the constant 3 to variable a. You can also assign the value of an expression to a variable.
type conversions in the assignment process

If the types on either side of the assignment operator are inconsistent, but are both numeric or character types, type conversions are done automatically when the value is assigned.

1 when the floating-point data (including single, double precision) is assigned to the integer variable, the fractional part is discarded.

2 when the integer data is assigned to a floating-point variable, the value is unchanged, but is stored in exponential form in the variable.

3 when assigning a double data to a 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 assigns an int, short, or long data to a char variable, and sends its lower 8 bits intact to the char variable (truncation occurs). For example

short int i=289;
  char c;
  C=i; Assigns an int data to a char variable

The assignment is shown in figure. For convenience, this is illustrated with an int-type data that accounts for two bytes (16 bits).

6 Assign the signed (signed) data to the unsigned (unsigned) variable of the same length, and copy the contents of the storage cell (even the original symbol bit as a numeric value).

"Example" 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 result is

a=65535

The value assigned to B is-1, how does it get 65535? See the assignment as shown in the figure.

-1 of the complement form is 1111111111111111 (that is, all 16 bits are 1), it is transmitted to a, and a is unsigned variable, 16 bits all 1 are decimal 65535. If B is positive and is between 0~32767, the value is unchanged after the assignment.

The assignment between different types of integer data is, in the final analysis, an article that is delivered directly by storage in the storage unit.

C and C + + flexible use, in different types of data between the assignment, often appear unexpected results, and compile system does not prompt error, relying on the experience of programmers to find problems. This requires programmers to have an understanding of the cause of the problem in order to quickly troubleshoot.
Compound assignment operator

Add additional operators before the assignment "=" to form a composite operator. If you add a "+" operator before "=" it becomes the composite 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

Take "a+=3" as an example to illustrate that it is equivalent to making a 3-plus operation. Let a plus 3 be assigned to a. Similarly, the function of "x*=y+8" is to make x multiplied by (y+8) and then assigned to X.

For the sake of memory, you can understand this:

    • a+= B (where a is a variable and B is an expression)
    • a+= B (Moves the underlined "a +" to the right of "=")
    • A = a + B (fill the variable name A on the left of "=")

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) (Don't mistake x=x%y+3)

Any two-dollar (binary) operator can be combined with an assignment to form a composite assignment character. C + + can use several of the following composite assignment operators:

+=,-=,*=,/=,%=,<<=,>>=,&=,^=,|=

The latter 5 are related to bitwise operations.

C + + is the use of this composite operator, one is to simplify the program, so that the program refining, the second is to improve the efficiency of the compilation (this style and "reverse Poland" consistent, conducive to compiling, to produce high quality target code). Professional programmers in the program commonly used composite operators, beginners may not be accustomed to, but also can not use or less.
an assignment expression

The expression by which an assignment operator joins a variable with an express is called an assignment expression. Its general form is:

  < variables > < assignment operator > < expression >

such as "a=5" is an assignment expression. The procedure for solving an assignment expression is to first find 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 to the left of the assignment operator is called the left value, abbreviated to Lvalue. Not all objects can be left values, variables can be left values, and expression a+b cannot be left, and constant variables cannot be left as values, because constant variables cannot be assigned.

The expression that appears on the right side of the assignment operator is called the "Right Value" (rvalue). Obviously the left value can also appear on the right side of the assignment operator, so the left value can be the right value. Such as:

  int a=3,b,c;
  b=a;//B is the left value
  c=b;//B is also the right value

An expression in an assignment expression, and can be an assignment expression. Such as

  A= (b=5)

Here is an example of an assignment expression:

 A=b=c=5 (Value of assignment expression is 5,a,b,c value is 5) a=5+ (
 c=6) (the expression value is 11,a value is 11,c value is 6)
 a= (b=4) + (c=6) (the expression value is 10,a value is 10,b equals 6)
 a = (b=10)/(c=2) (expression value is 5,a equals 5,b equals 10,c equals 2)

Please analyze the following assignment expression:

  (a=3*5) =4*3

An assignment expression should be bracketed if it is a left value, and a syntax error occurs if written as follows:

  A=3*5=4*3

Because 3*5 is not a left value, it cannot appear on the left side of the assignment operator.

An assignment expression can also contain a composite assignment operator. Such as

  A+=a-=a*a

is also an assignment expression. If the initial value of a is 12, the assignment expression is solved by the following steps:
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 + + comma operators and comma expressions
C + + takes an assignment expression as one of the expressions, so that the assignment operation can appear not only in an assignment statement, but also as an expression in other statements, such as output statements, loop statements, and so on. This is a manifestation of the flexibility of C + + language.

Note that when you output the value of an assignment expression with a cout statement, enclose the assignment expression in parentheses if it is written as "cout<<a=b;" A compilation error will occur.

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

  3+5, 6+8

Called the comma expression, also called the Order evaluation operator. The general form of a comma expression is:

  Expression 1, Expression 2

The solution of a comma expression is to solve the expression 1 first, and then solve the expression 2. The value of the entire comma expression is the value of expression 2. For example, a comma expression

  A=3*5, A*4

Assignment operators have precedence over the comma operator, so you should first solve a=3*5 (that is, "a=3*5" as an expression). After calculation and assignment, the value of a is 15, then the solution of A*4 is 60. The entire comma expression has a value of 60.

A comma expression can also form a new comma expression with another expression, such as

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

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

  Expression 1, expression 2, expression 3, ..., Expression n

Its value is the value of the expression N.

The comma operator is the lowest level of all operators. Therefore, the following two expressions function differently:

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

In fact, the comma expression is simply a number of expressions "concatenation". In many cases, the purpose of using a comma expression is simply to get the value of each expression separately, rather than necessarily getting and using the entire value of the comma expression, which is most commonly used in circular statements (for statements).

When you output a comma-expression value with cout, enclose the comma expression in parentheses, such as:

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

C and C + + language expression is strong, one of the important aspects of its expression is rich in type, operator function is strong, so flexible, adaptable.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.