Computation

In C++, computation is expressed in an expression which is composed of operators and operands.

Almost every expression produces a result, the result can then be stored in a variable or used as an operand of another expression.

Arithmetic operators

The arithmetic operators can be used with arithmetic types such as int or double to perform arithmetic:

Operator name Syntax Description
Unary plus +a Returns the positive value of a
Unary minus -a Returns the negative value of a
Addition a + b Returns the value of a plus b
Subtraction a - b Returns the value of a minus b
Multiplication a * b Returns the value of a multiplied by b
Division a / b Returns the quotient value of a divided by b
Modulo a % b For integral types only. Returns the remainder value of a divided by b.

All unary operators require one operand and all binary operators require two operands: typically a left operand and a right operand.

All of the above arithmetic operators create a temporary value, with the same type as the operand(s), as a result. The operands remain constant.

Assignment operators

The following assignment operators maintain the constancy of their right operand while assigning to their left operand, this implies that the left operand must be a variable:

Operator name Syntax Description
Simple assignment a = b Assigns the value of b to a and returns a
Addition assignment a += b Assigns the result of a + b to a and returns a
Subtraction assignment a -= b Assigns the result of a - b to a and returns a
Multiplication assignment a *= b Assigns the result of a * b to a and returns a
Division assignment a /= b Assigns the result of a / b to a and returns a
Modulo assignment a %= b Assigns the result of a % b to a and returns a

a += b yields the same effect as a = a + b, and the same pattern is true of the other arithmetic assignment operators.

Increment/Decrement operators

The following unary operators are the increment/decrement operators and, like the assignment operators, they also assign to their operand:

Operator name Syntax Description
Pre-increment ++a Increment a and return a
Pre-decrement --a Decrement a and return a
Post-increment a++ Increment a and return the original value of a
Post-decrement a-- Decrement a and return the original value of a

The expression ++a is exactly equivalent to a += 1, and the expression --a is exactly equivalent to a -= 1

Precedence and associativity

Every operator has a precedence and associativity which both determine the order in which operators and their operands are analyzed by the compiler (which can be overridden when parenthesis are used to group expressions). An expression such as a = b + c * d is analyzed as (a) = ((b) + ((c) * (d))).

The associativity of an operator determines the direction in which that operator's operands are analyzed. An expression like a + b has Left-to-right associativity while a = b has Right-to-left associativity.

The precedence of an operator is a particular ranking of that operator, determining when that operator's operands become bound to it.

Most of this is common-sense, when it can be derived that:

  1. Post-increment, Post-decrement and function call expressions are analyzed first
  2. Pre-increment, Pre-decrement, unary minus, and unary plus expressions are analyzed second
  3. Multiplication, division, and modulo expressions are analyzed third
  4. Addition and subtraction expressions are analyzed fourth
  5. Simple assignment and compound assignment expressions are analyzed fifth

Note: This list was corrected on 4/25/18, sorry for any inconveniences.

From these rules, the statements: int x = 3; cout << --x + 2 * -5; print -8.

One interesting fact to note about C++ is that there are no negative literals, only positive literals with the unary minus operator applied to make the resulting value negative.

The function call operator

The arithmetic operators are not the only means of performing computation, C++ provides a collection of predefined math functions, most of them require only 1 argument, some require 2.

The function call operator takes the form of F( A1, A2, A3, ... ) where F is the name of a function and A1, A2, A3, ... is a possibly empty list of expressions to be used as arguments for that function. The number of arguments and the type of each argument value must be compatible with that function's parameter list; in other words, one must know how to properly use that function.

The following table shows possible expressions that use some of the common mathematical functions of C++:

Expression Return value
abs(x) Absolute value of x
exp(x) e raised to the power of x
log(x) Natural logarithm of x
log10(x) Base-10 logarithm of x
sqrt(x) Square root of x
sin(x) Sine of x
cos(x) Cosine of x
tan(x) Tangent of x
asin(x) Arc sine of x
acos(x) Arc cosine of x
atan(x) Arc tangent of x
pow(x, y) x raised to the power of y
4 * atan(1) Pi constant
exp(1) E constant

With the exception of abs, the above functions will return a value of type double when the supplied argument(s) are of type int or double.