C++ Basics (8) Operators in C/C++

I signed up for the Golden Stone Project Phase 1 Challenge - Divide the 100,000 Prize Pool, this is my 8th article, click to view the event details

Operators are the foundation of any programming language. We can define operators as symbols that help us perform specific mathematical and logical calculations on their operands. In other words, we can say that operators operate on operands. For example, '+' is the operator for addition as follows:  

c = a + b;
复制代码

Here, "+" is an operator called an addition operator, and "a" and "b" are operands. The addition operator tells the compiler to add two operands "a" and "b". 

The functionality of the C/C++ programming language is incomplete without the use of operators.

C/C++ has many built-in operators, which can be divided into 6 types:

  1. arithmetic operators
  2. relational operator
  3. Logical Operators
  4. bitwise operators
  5. assignment operator
  6. other operators

The above operators have been discussed in detail: 

1. Arithmetic operators: 

These operators are used to perform arithmetic/mathematical operations on the operands. Example: (+, -, *, /, %, ++, -). There are two types of arithmetic operators: 

a) Unary Operator : An operator that operates or uses a single operand is a unary operator. For example: increment (++) and decrement (–) operators

int val = 5;
++val;  // 6
复制代码

b) Binary Operators : An operator that operates or uses two operands is a binary operator. For example: Addition (+), Subtraction (-), Multiplication (*), Division (/) operators

int a = 7;
int b = 2;
cout<<a+b; // 9
复制代码

2. Relational operators:

These are used to compare the values ​​of two operands. For example, checking if one operand is equal to the other, whether one is greater than the other, etc. Some relational operators are (==, >= , <= ).

int a = 3;
int b = 5;
a < b;
// 运算符检查 a 是否小于 b
复制代码

3. Logical operators:

Logical operators are used to combine two or more conditions/constraints or to supplement the evaluation of the original conditions considered. The result of a logical operator is the Boolean value true or false

例如,当两个条件都满足时,在 C 或 C++ 中表示为“&&”运算符的逻辑与返回 true。 否则,它返回 false。因此,当 a 和 b 都为真(即非零)时,a && b 返回真(更多参考请参阅本文)。****

(4 != 5) && (4 < 5);     // true
复制代码

4.位运算符: 

位运算符用于对操作数执行位级操作。运算符首先转换为位级,然后对操作数执行计算。诸如加法、减法、乘法等数学运算可以在位级别执行以加快处理速度。例如,在 C 或 C++ 中表示为& 运算符的按位 AND将两个数字作为操作数,并对两个数字的每一位进行 AND。仅当两个位都为 1 时,AND 的结果才为 1。(有关更多参考,请参阅本文)。****

int a = 5, b = 9;   // a = 5(00000101), b = 9(00001001)
cout << (a ^ b);   //  00001100
cout <<(~a);       // 11111010
复制代码

5.赋值运算符: 

赋值运算符用于为变量赋值。赋值运算符的左侧操作数是一个变量,而赋值运算符的右侧操作数是一个值。右侧的值必须与左侧的变量具有相同的数据类型,否则编译器将引发错误。 

不同类型的赋值运算符如下所示 
:“=”: 这是最简单的赋值运算符。该运算符用于将右侧的值赋给左侧的变量。 
例如: 

a = 10; 
b = 20; 
ch ='y';
复制代码

湾。“+=” :该运算符是 '+' 和 '=' 运算符的组合。该运算符首先将左侧变量的当前值与右侧的值相加,然后将结果赋给左侧的变量。 
例如:

(a += b) 可以写成 (a = a + b)
如果最初存储在 a 中的值为 5。那么 (a += 6) = 11复制代码

C。“-=” :此运算符是 '-' 和 '=' 运算符的组合。该运算符首先从左侧变量的当前值中减去右侧的值,然后将结果分配给左侧的变量。 
例如: 

(a -= b) 可以写成 (a = a - b)
如果最初存储在 a 中的值是 8。那么 (a -= 6) = 2复制代码

d。“*=” :此运算符是 '*' 和 '=' 运算符的组合。该运算符首先将左侧变量的当前值乘以右侧的值,然后将结果分配给左侧的变量。 
例如: 

(a *= b) 可以写成 (a = a * b)
如果最初存储在 a 中的值为 5。那么 (a *= 6) = 30复制代码

e. “/=” :此运算符是 '/' 和 '=' 运算符的组合。该运算符首先将左侧变量的当前值除以右侧的值,然后将结果分配给左侧的变量。 
例如:

(a /= b) 可以写成 (a = a / b)
如果最初,存储在 a 中的值为 6。那么 (a /= 2) = 3复制代码

6. 其他运营商: 

除了上述运算符外,C 或 C++ 中还有一些其他运算符可用于执行某些特定任务。其中一些在这里讨论: 

sizeof 运算符: 

  • sizeof 在 C/C++ 编程语言中被广泛使用。
  • 它是一个编译时一元运算符,可用于计算其操作数的大小。
  • sizeof 的结果是无符号整数类型,通常用 size_t 表示。
  • 基本上,sizeof 运算符用于计算变量的大小。(参考这篇文章)

逗号运算符: 

  • 逗号运算符(由标记表示)是一个二元运算符,它计算其第一个操作数并丢弃结果,然后计算第二个操作数并返回此值(和类型)。
  • 逗号运算符的优先级是所有 C 运算符中最低的。
  • 逗号既充当运算符又充当分隔符。(参考这篇文章)

条件运算符: 

  • Conditional operators are of the form Expression1? expression2:expression3 .
  • Here, Expression1 is the condition to be evaluated. If the condition(Expression1) is true then we will execute and return the result of expression2, else if the condition(expression1) is false then we will execute and return the result of expression3.
  • We can replace the use of if..else statements with conditional operators.

Dot (.) and arrow (->) operators:

  • Member operators are used to refer to individual members of classes, structures, and unions.
  • The dot operator is applied to the actual object.
  • The arrow operator is used with pointers to objects.

Cast operator:

  • Conversion operators convert one data type to another. For example, int(2.2000) will return 2.

  • A cast is a special operator that forces one data type to be converted to another. 

  • The most common conversions supported by most C++ compilers are as follows -    [ (type) expression ] . &, *operator:

  • The pointer operator & returns the address of a variable. For example &a; will give the actual address of the variable.

  • The pointer operator * is a pointer to a variable. For example *var; will point to a variable var.

The following is the implementation of the above operator:

// C++ 中的运算符
#include<iostream>
using namespace std;

int main(){
 int a=10, b=5;
 // 算术运算符
 cout<<"以下是 C++ 中的算术运算符"<<endl;
 cout<<"The value of a + b is "<<a+b<<endl;
 cout<<"The value of a - b is "<<a-b<<endl;
 cout<<"The value of a * b is "<<a*b<<endl;
 cout<<"The value of a / b is "<<a/b<<endl;
 cout<<"The value of a % b is "<<a%b<<endl;
 cout<<"The value of a++ is "<<a++<<endl; // 首先打印 (a) 然后将其增加 1
 cout<<"The value of a-- is "<<a--<<endl; // 先打印 (a+1) 然后减 1
 cout<<"The value of ++a is "<<++a<<endl; // 将 (a) 增加 (a+1) 然后打印
 cout<<"The value of --a is "<<--a<<endl; // 将 (a+1) 减 (a) 然后打印
 cout<<endl;

 // 赋值运算符 --> 用于给变量赋值
 // int a =3, b=9;
 // char d='d';

 // 比较运算符 
 // 所有这些比较运算符的输出将为 (1) 如果为真,则为 (0) 如果为假
 cout<<"以下是 C++ 中的比较运算符"<<endl;
 cout<<"a == b 的值是 "<<(a==b)<<endl;
 cout<<"The value of a != b is "<<(a!=b)<<endl;
 cout<<"The value of a >= b is "<<(a>=b)<<endl;
 cout<<"The value of a <= b is "<<(a<=b)<<endl;
 cout<<"The value of a > b is "<<(a>b)<<endl;
 cout<<"The value of a < b is "<<(a<b)<<endl;
 cout<<endl;
 // 逻辑运算符
 cout<<"以下是 C++ 中的逻辑运算符"<<endl;
 cout<<"The value of this logical and operator ((a==b) && (a<b)) is:"<<((a==b) && (a<b))<<endl;
 cout<<"The value of this logical or operator ((a==b) || (a<b)) is:"<<((a==b) || (a<b))<<endl;
 cout<<"The value of this logical not operator (!(a==b)) is:"<<(!(a==b))<<endl;


 return 0;
}
复制代码

output

以下是 C++ 中的算术运算符
The value of a + b is 15
The value of a - b is 5
The value of a * b is 50
The value of a / b is 2
The value of a % b is 0
The value of a++ is 10
The value of a-- is 11
The value of ++a is 11
The value of --a is 10

以下是 C++ 中的比较运算符
The value of a == b is 0
The value of a != b is 1
The value of a >= b is 1
The value of a <= b is 0
The value of a > b is 1
The value of a < b is 0

以下是 C++ 中的逻辑运算符
The value of this logical and operator ((a==b) && (a<b)) is:0
The value of this logical or operator ((a==b) || (a<b)) is:0
The value of this logical not operator (!(a==b)) is:1
复制代码

Guess you like

Origin juejin.im/post/7142782683884552199