加法运算符:+ 和 -
expression + expression
expression – expression
备注
相加运算符为:
加 (+)
减 (–)
这些二进制运算符具有从左至右的关联性。
相加运算符采用算术或指针类型的操作数。 加法 (+) 运算符的结果是操作数之和。 减法 (–) 运算符的结果是操作数之差。 如果一个操作数是指针或两个操作数都是指针,则它们必须是指向对象的指针,而不是指向函数的指针。 如果两个操作数都是指针,则结果没有意义,除非它们是指向同一数组中的对象的指针。
相加运算符采用 arithmetic、integral 和 scalar 类型的操作数。 下表定义了这些操作数。
用于相加运算符的类型
类型 |
含义 |
---|---|
arithmetic |
整型和浮点类型统称为“算术”类型。 |
integral |
所有大小(long、short)和枚举数的 char 和 int 类型为“整数”类型。 |
scalar |
标量操作数是算术类型或指针类型的操作数。 |
这些运算符的合法组合为:
arithmetic + arithmetic
scalar + integral
integral + scalar
arithmetic – arithmetic
scalar – scalar
请注意,加法和减法不是等效运算。
如果两个操作数都是算术类型,则算术转换中介绍的转换适用于这两个操作数,并且结果为已转换的类型。
示例
// expre_Additive_Operators.cpp
// compile with: /EHsc
#include <iostream>
#define SIZE 5
using namespace std;
int main() {
int i = 5, j = 10;
int n[SIZE] = { 0, 1, 2, 3, 4 };
cout << "5 + 10 = " << i + j << endl
<< "5 - 10 = " << i - j << endl;
// use pointer arithmetic on array
cout << "n[3] = " << *( n + 3 ) << endl;
}