📜  =(赋值)和==(等于)运算符有什么区别

📅  最后修改于: 2022-05-13 01:54:23.783000             🧑  作者: Mango

=(赋值)和==(等于)运算符有什么区别

=运算符

= ”是一个赋值运算符,用于将右边的值赋给左边的变量。

例如:

a = 10;
b = 20;
ch = 'y';

例子:

// C program to demonstrate
// working of Assignment operators
  
#include 
  
int main()
{
  
    // Assigning value 10 to a
    // using "=" operator
    int a = 10;
    printf("Value of a is %d\n", a);
  
    return 0;
}
输出:
Value of a is 10

==运算符

'=='运算符检查两个给定的操作数是否相等。如果是,则返回 true。否则返回false。
例如:

5==5

This will return true.

例子:

// C program to demonstrate
// working of relational operators
#include 
  
int main()
{
    int a = 10, b = 4;
  
    // equal to
    if (a == b)
        printf("a is equal to b\n");
    else
        printf("a and b are not equal\n");
  
    return 0;
}
输出:
a and b are not equal

差异可以以表格形式显示如下:

===
It is an assignment operator.It is a relational or comparison operator.
It is used for assigning the value to a variable.It is used for comparing two values. It returns 1 if both the values are equal otherwise returns 0.
Constant term cannot be placed on left hand side.
Example: 1=x; is invalid.
Constant term can be placed in the left hand side.
Example: 1==1 is valid and returns 1.