D A R K - C O D E R

Loading

C++ Operators | A Comprehensive Guide to C++ Operators: Types, Examples, and Precedence

Operators are essential elements of any programming language, and C++ is no exception. In C++, operators are symbols or keywords that are used to perform operations on operands. In this article, we will explore the different types of operators available in C++, including arithmetic, relational, logical, bitwise, and assignment operators.

Arithmetic Operators:

Arithmetic operators are used to perform arithmetic operations on numeric operands. The available arithmetic operators in C++ are:

  • Addition (+)
  • Subtraction (-)
  • Multiplication (*)
  • Division (/)
  • Modulus (%)

Example:

int x = 10;
int y = 5;
int sum = x + y; // 15
int difference = x - y; // 5
int product = x * y; // 50
int quotient = x / y; // 2
int remainder = x % y; // 0

Relational Operators:

Relational operators are used to compare two operands and return a Boolean value. The available relational operators in C++ are:

  • Equal to (==)
  • Not equal to (!=)
  • Greater than (>)
  • Less than (=)
  • Less than or equal to ( y); // true bool lessThan = (x < y); // false bool greaterThanOrEqual = (x >= y); // true bool lessThanOrEqual = (x 1; // 0010 in binary (2 in decimal)

    Assignment Operators:

    Assignment operators are used to assign a value to a variable. The available assignment operators in C++ are:

    • Simple assignment (=)
    • Addition assignment (+=)
    • Subtraction assignment (-=)
    • Multiplication assignment (*=)
    • Division assignment (/=)
    • Modulus assignment (%=)
    • Bitwise AND assignment (&=)
    • Bitwise OR assignment (|=)
    • Bitwise XOR assignment (^=)
    • Left shift assignment (=)

    Example:

    int x = 5;
    x += 3; // equivalent to x = x + 3, x is now 8
    x -= 2; // equivalent to x = x - 2, x is now 6
    x *= 2; // equivalent to x = x * 2, x is now 12
    x /= 3; // equivalent to x = x / 3, x is now 4
    x %= 2; // equivalent to x = x % 2, x is now 0
    x &= 3; // equivalent to x = x & 3, x is now 0
    x |= 3; // equivalent to x = x | 3, x is now 3
    x ^= 2; // equivalent to x = x ^ 2, x is now 1
    x > 1, x is now 1
    

    Operator Precedence:

    Operator precedence determines the order in which operators are evaluated in an expression. In C++, operators with higher precedence are evaluated first. If two operators have the same precedence, the evaluation order is determined by the associativity of the operators. The table below shows the operator precedence and associativity in C++:

    Precedence Operator Associativity
    1 () [] -> . Left to right
    2 ++ — + – ! ~ * & sizeof Right to left
    3 * / % Left to right
    4 + – Left to right
    5 > Left to right
    6 < >= Left to right
    7 == != Left to right
    8 & Left to right
    9 ^ Left to right
    10 | Left to right
    11 && Left to right
    12 || Left to right
    13 ?: Right to left
    14 = += -= *= /= %= &= ^= |= = Right to left
    15 , Left to right

    Example:

    int x = 5;
    int y = 3;
    int z = 2;
    
    int result = x + y * z; // result is 11 (y * z is evaluated first due to higher precedence)
    
    bool boolResult = x > y || y < z; // boolResult is true ((x > y) is evaluated first due to higher precedence)
    
    

    Conclusion:

    In conclusion, operators are a fundamental part of C++ programming, and an understanding of their types and functions is essential for writing efficient code. In this article, we have explored the different types of operators available in C++, including arithmetic, relational, logical, bitwise, and assignment operators, as well as operator precedence. By mastering these concepts, you will be well-equipped to write robust and efficient C++ code.