C++ 赋值运算符

C++ 赋值运算符

尝试以下示例以了解C++中可用的所有赋值运算符。

将以下C++程序复制并粘贴到test.cpp文件中,然后编译并运行此程序。

#include <iostream>
using namespace std;

main() {
   int a = 21;
   int c ;

   c =  a;
   cout << "Line 1 - =  Operator, Value of c = : " <<c<< endl ;

   c +=  a;
   cout << "Line 2 - += Operator, Value of c = : " <<c<< endl ;

   c -=  a;
   cout << "Line 3 - -= Operator, Value of c = : " <<c<< endl ;

   c *=  a;
   cout << "Line 4 - *= Operator, Value of c = : " <<c<< endl ;

   c /=  a;
   cout << "Line 5 - /= Operator, Value of c = : " <<c<< endl ;

   c  = 200;
   c %=  a;
   cout << "Line 6 - %= Operator, Value of c = : " <<c<< endl ;

   c <<=  2;
   cout << "Line 7 - <<= Operator, Value of c = : " <<c<< endl ;

   c >>=  2;
   cout << "Line 8 - >>= Operator, Value of c = : " <<c<< endl ;

   c &=  2;
   cout << "Line 9 - &= Operator, Value of c = : " <<c<< endl ;

   c ^=  2;
   cout << "Line 10 - ^= Operator, Value of c = : " <<c<< endl ;

   c |=  2;
   cout << "Line 11 - |= Operator, Value of c = : " <<c<< endl ;

   return 0;
}

当上面的代码被编译并执行时,会产生以下结果−

Line 1 - =  Operator, Value of c = : 21
Line 2 - += Operator, Value of c = : 42
Line 3 - -= Operator, Value of c = : 21
Line 4 - *= Operator, Value of c = : 441
Line 5 - /= Operator, Value of c = : 21
Line 6 - %= Operator, Value of c = : 11
Line 7 - <<= Operator, Value of c = : 44
Line 8 - >>= Operator, Value of c = : 11
Line 9 - &= Operator, Value of c = : 2
Line 10 - ^= Operator, Value of c = : 0
Line 11 - |= Operator, Value of c = : 2

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程