Partager via


Additive Operators: + and - 

expression + expression 
expression – expression

Remarks

The additive operators are:

  • Addition (+)

  • Subtraction ()

These binary operators have left-to-right associativity.

The additive operators take operands of arithmetic or pointer types. The result of the addition (+) operator is the sum of the operands. The result of the subtraction () operator is the difference between the operands. If one or both of the operands are pointers, they must be pointers to objects, not to functions. If both operands are pointers, the results are not meaningful unless both are pointers to objects in the same array.

Additive operators take operands of arithmetic, integral, and scalar types. These are defined in the following table.

Types Used with Additive Operators

Type Meaning

arithmetic

Integral and floating types are collectively called "arithmetic" types.

integral

Types char and int of all sizes (long, short) and enumerations are "integral" types.

scalar

Scalar operands are operands of either arithmetic or pointer type.

The legal combinations for these operators are:

arithmetic + arithmetic

scalar + integral

integral + scalar

arithmeticarithmetic

scalarscalar

Note that addition and subtraction are not equivalent operations.

If both operands are of arithmetic type, the conversions covered in Arithmetic Conversions are applied to the operands, and the result is of the converted type.

Example

// 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;
}

See Also

Reference

Expressions with Binary Operators
C++ Operators
Operator Precedence and Associativity
Addition of Pointer Types
Subtraction of Pointer Types
C Additive Operators