Home > Backend Development > C++ > body text

The difference between == and = in c++

下次还敢
Release: 2024-04-26 20:09:13
Original
753 people have browsed it

In C, the == operator is used to compare expressions for equality and return a Boolean value (true or false); the = operator is used for assignment, assigning the value of an expression to a variable and returning the assigned value. variable.

The difference between == and = in c++

The difference between == and = in C

In the C programming language, the double equal sign (== ) and single equal sign (=) operators have different meanings and uses.

== Operator

Function: Used to compare whether the values ​​​​of two expressions are equal.

Syntax:

<code class="cpp">expression1 == expression2</code>
Copy after login

Return value:

  • If the values ​​​​of the two expressions are equal, return true .
  • If the values ​​of the two expressions are not equal, return false.

Example:

<code class="cpp">int a = 5;
int b = 10;
bool result = (a == b); // result 将为 false</code>
Copy after login

= Operator

Function: is used for assignment .

Syntax:

<code class="cpp">variable = expression</code>
Copy after login

Return value:

  • Assigns the value of an expression to a variable.
  • Return the assigned variable.

Example:

<code class="cpp">int a;
a = 5; // a 的值现在为 5</code>
Copy after login

Key Difference

  • == operator is used for comparison while = operator is used for assignment. The
  • == operator returns a boolean value, while the = operator returns a variable to which a value has been assigned.
  • == operator cannot modify the operand, while = operator can modify the operand on the left.

The above is the detailed content of The difference between == and = in c++. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
c++
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template