C++ Operators

C++ supports a rich set of built-in operators, we have already used several of them like =, &, etc. An operator is a symbol that tells the computer to perform certain mathematical or logical manipulation. operators are used in the program to manipulate data and variables. They usually form mathematical or logical expressions.

Operators in C++ can be classified into following groups:

  1. Arithmetic operators
  2. Assignment operator
  3. Compound assignment
  4. Logical operators
  5. Bitwise operators
  6. Increment and decrement operators
  7. Conditional ternary operator
  8. sizeof()
  9. Other Operators

Arithmetic operators

C++ provides all the basic arithmetic operators. They all work the same as they do in other languages. These can operate on any built-in data type allowed in C++. They are:-

  1. Addition (+)
  2. Subtraction (-)
  3. Multiplication (*)
  4. Divide (/)
  5. Modulo (%)

Output:

Assignment operator (=)

The assignment operator assigns the right side variable value to the left variable.

a=7;

This statement assigns the integer value 7 to the variable a. The assignment operation always takes place from right to left

This statement assigns to variable the value contained in variable b. The value of a at the moment this statement is executed is lost and replaced by the value of b.

Compound assignment

Compound assignment operators modify the current value by performing an operation.

  1. + =
  2. – =
  3. * =
  4. / =

Logical operators

C++ provides three logical operators when we test more than one condition and make decisions.

These are:

  1. && (meaning logical AND)
  2. || (meaning logical OR)
  3. ! (meaning logical NOT)

Bitwise Operators

Bitwise operators are used to perform operations on individual bits. They can only be used alongside char and int data types

Operator Description
& Binary AND
| Binary OR
^ Binary XOR
~ Binary One’s Complement
<< Binary Shift Left
>> Binary Shift Right

Increment and decrement operators (++, –)

C++ allows two very useful operators not generally found in other languages. These are the increment and decrement operators.

The operator ++ adds 1 to the operand, while — subtract 1. Both are unary operators and takes the following form.

We use the increment and decrement statement in the loop extensively.

While ++i and i++ mean the same thing they are form statements independently, they behave differently;y when they are used in the expression on the right-hand side of an assignment statement. Consider the following:-

i=1;

j=++i;

In this case, the value of j and I would be 2. Suppose if we rewrite the above statement as:-

i=1;

j=i++;

then, the value of j would be 1 and i would be 2. A prefix operator first adds 1 to the operand and then the result assigned to the variable on left. on the other hand, a postfix operator first assigns and then increments the operands.

Conditional ternary operator

A ternary operator pair ” ? :” is available in C++ to construct conditional expression form:-

condition ? result_1 : result_2

If the condition is true, the entire expression evaluates to result_1, and otherwise to result_2.

sizeof() operator

The Sizeof is a compile-time operator and when used with an operand, it returns the number of bytes the operation occupies. The operation may be a variable, constants, or a data type qualifier. For example:-

It returns the size of int

Other Operators

  1. & Returns the address of a memory location.
  2. * Pointer to a variable.