What are the differences between = and == in C++?

青灯夜游
Release: 2020-11-30 14:37:40
Original
17764 people have browsed it

Difference: 1. "=" means assignment and is an assignment operator; and "==" is an equality operator, used to determine whether both sides are equal; 2. The "=" operator has a mandatory type Conversion, while "==" does not have a cast.

What are the differences between = and == in C++?

The difference between = and == in c

1. Meaning Difference:

"=" means assignment.

Its function is to assign the value of an expression to an lvalue. An expression is either an lvalue or an rvalue. The so-called lvalue refers to an expression that can be used on the left side of an assignment operation. lvalues ​​must be modifiable and cannot be constants. We use variables as lvalues, and we can also see that pointers and references can also be used as lvalues.

"==" is the equality operator, which determines whether both sides are equal. Using the equality operator, if you want the equality operator to return true, the values ​​or statements on both sides of the operator must be equal; if they are not equal, the == operator returns false.

2. Whether there is forced conversion:

"=" This operator will perform forced type conversion, so there will also be some special rules:

(1) If one operand is a number and the other is a string, the == operator will try to convert the string to a number before comparing;

(2) If one operand is a Boolean value , then it will be cast to a numeric type by the operator before comparison, in this case, true is converted to 1, and false is converted to 0.

(3) If one operand is null and the other is underfined, the comparison result is true;

(4) If one or both operands are NaN, the comparison result is false.

There is no forced conversion in "==", but the assignment operation copies the value of the original variable to the new variable (assignment by value), so changing one does not affect the other.

3. The result of the return value is different:

The result of "=" actually means assigning the value of the expression on the right to the operand on the left. The value of an assignment expression is the assigned value. That is, the value of "$a = 3" is 3.

"==" is different, it will return false or true value. So a statement such as "4" == 4 will return true because JavaScript will convert the character "4" into the number 4. If you want the statement to return false, you need to use the identity operator (===).

What are the differences between = and == in C++?

For more programming related knowledge, please visit: Programming Video Course! !

The above is the detailed content of What are the differences between = and == in C++?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template