The difference between single equal sign and double equal sign in C language

下次还敢
Release: 2024-05-02 14:54:14
Original
485 people have browsed it

The single equal sign (=) is used for assignment, assigning the right value to the left variable; the double equal sign (==) is used for comparison, to determine whether the two operands are equal, and return 0 (false) or 1 (real).

The difference between single equal sign and double equal sign in C language

The difference between single equal sign and double equal sign in C language

In C language, single equal sign (=) and the double equal sign (==) have different usages:

Single equal sign (=)

  • Assignment operator: is used to assign the value on the right to the variable or memory address on the left.

Double equal sign (==)

  • ##Comparison operator: is used for comparison Whether the values ​​of the two operands are equal.
  • The comparison type can be integer, floating point number or string.

Specific differences

FeaturesSingle equal sign (Double equal sign (##UsageOperation resultReturn Value
= ) ==)
Assignment Comparison
The left operand is assigned the right value The return value indicates whether the two operands are equal (true or False)
None 0 (False) or 1 (True)
Example

<code class="c">int x = 10; // 赋值
if (x == 10) { // 比较
    // 代码块
}</code>
Copy after login

Note:

The single equal sign cannot be used interchangeably with the double equal sign.
  • Since the assignment operator has a higher priority than the comparison operator, when used in an expression, the assignment operator is executed first.
  • When comparing strings, the double equal sign (
  • ==
  • ) only compares the address of the string, not the string content. To compare string contents, use the strcmp() function.

The above is the detailed content of The difference between single equal sign and double equal sign in C language. 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