what is operator in c++?

C++ Operator :-

Operator is a special symbol. which are used to perform operations on variable. it is used between two operands. such as sum = a + b; here + is the operator and a and b is the operands value.

Example :-

int a = 5;
int b = 10;
int sum = a + b;
here a and b is operands. + is the operator.

Types of operators in C++ :-

  • Arithmetic Operators
  • Relational Operators
  • Logical Operators
  • Bitwise Operators
  • Assignment Operators
  • Ternary or Conditional Operators
  • Misc Operators

  • Arithmetic Operators :-

    Arithmetic operations are mainly utilized to perform mathematical operations (Like (+) addition, (-) subtraction, (*) multiplication, (/) division), % (Modulus Operator), ++ (Increment operator) , -- (Decrement operator)

    Binary Operator :-

    Here A = 10; B = 20;
    Operators Name Descriptions
    + Addition Adds two operands Eg. A+B
    - Subtraction Subtract two operands Eg. A-B
    * multiplication Multiplies two operands Eg. A*B
    / Division Divides numerator by de-numerator. Eg. B/A
    % Modulus Modulus Operator and remainder of after an integer division. Eg. B%A

    Unary Operator :-

    Here A = 5; B = 10;
    Operators Name Descriptions
    ++ Increment increases integer value by one. Eg A++ will give 6
    -- Decrement decreases integer value by one Eg. A-- will give 9

    Example

    #include
    using namespace std;
    int main()
    {
    int x = 10, y = 5;
    int add = x + y;
    int sub = x - y;
    int multipli = x * y;
    int division = x / y;
    int modulus = x % y;
    int increment = ++x;
    int decrement = --x;
    cout << "Addition: " << add << endl; // 10 + 5 = 15
    cout << "Subtraction: " << sub << endl; // 10 - 5 = 5
    cout << "Multiplication: " << multipli << endl; // 10 * 5 = 50
    cout << "Division: " << division << endl; // 10 / 5 = 2 (Quotient)
    cout << "Modulus: " << modulus << endl; // 10 / 5 = 0 (Here, 0 is Remainder)
    cout << "Increment: " << increment < cout << "Decrement: " << decrement < }

    Example

    #include
    using namespace std;
    int main() {
    int x = 15 + 10;
    cout << x;
    return 0;
    }

    Relational Operators :-

    relational operators are used to compare the values of two operands. It gives the result in Boolean values (0 and 1). If the comparison result is true, it returns 1 and if the comparison result is false, it returns 0 value.

    Operator NameOperator SymbolDescription
    Is Equal To operator==It checks both operands are equal or not. If the condition true, it returns true; otherwise, false.
    Greater Thanit check the first operand value is greater than the second operand value. If the condition true, it returns true; otherwise, false.
    Greater Than or Equal To>=it check the first operand value is greater than or equal to the second operand value. If the condition true, it returns true; otherwise, false.
    Less Thanit check the first operand value is lesser than the second operand value. If the condition true, it returns true; otherwise, false.
    Less Than or Equal To<=It checks that the first operand value is lesser than or equal to the second operand value. If the condition matches, it returns true; otherwise, false.
    Not Equal To!=It checks both operand values are not equal. If the condition matches, it returns true; otherwise, false.

    #include
    using namespace std;
    int main() {
    int a = 10;
    int b = 5;
    cout << (a == b) << endl; // 10 == 5 (Condition False, returns 0)
    cout << (a > b) << endl; // 10 > 5 (Condition True, returns 1)
    cout << (a < b) << endl; // 10 < 5 (Condition False, returns 0)
    cout << (a >= b) << endl; // (10 >= 5) (Condition True, returns 1)
    cout << (a <= b) << endl; //10 <= 5 (Condition False, returns 0)
    cout << (a != b) << endl; //(10 != 5) (Condition True, returns 1)
    return 0;
    }

    Logical Operators

    Logical operators perform logical operations on Boolean values. Logical operators are used for combining two or multiple conditions. It provides the result in Boolean values (0 and 1). If the comparison value is true, it returns 1 value. If the comparison value is false, it returns 0 value.

    Operator NameOperator SymbolDescription
    Logical AND&&The logical AND operator returns true only if both operands are true. If any operand is false, the result is false.
    Logical OR||The logical OR operator returns true if at least one of the conditions is true. If both are false, it returns false.
    Logical NOT!The logical NOT operator negates the given condition. If a condition is true, it returns false and vice versa.

    #include
    using namespace std;
    main() {
    int a = 5;
    int b = 20;
    int c ;
    if(a && b) {
    cout << "Line 1 - Condition is true"<< endl ;
    }
    if(a || b) {
    cout << "Line 2 - Condition is true"<< endl ;
    }
    /* Let's change the values of a and b */
    a = 0;
    b = 10;
    if(a && b) {
    cout << "Line 3 - Condition is true"<< endl ;
    } else { cout << "Line 4 - Condition is not true"<< endl ;
    }
    if(!(a && b)) {
    cout << "Line 5 - Condition is true"<< endl ;
    }
    return 0;
    }

    Bitwise Operators

    Bitwise operators are works on bit-level. So, compiler first converted to bit-level and then the calculation is performed on the operands.
  • Binary AND, &, Copies a bit to the result if it exists in both operands
  • Binary OR, |, Copies a bit to the result if it exists in any of the operands
  • Binary XOR, ^, Copies the bit to the result if it is present in either of the operands but not both
  • Left Shift, <<, Shifts the value to the left by the number of bits specified by the right operand
  • Right Shift, >>, Shifts the value to the right by the number of bits specified by the right operand
  • One's Complement, ~, Changes binary digits 1 to 0 and 0 to 1

  • Note: Only char and int data types can be used with Bitwise Operators.

    Example

    #include
    using namespace std;
    int main() {
    int x = 14, y = 8;
    cout << "x & y is " << (x & y) << endl; // Binary AND operator
    cout << "x | y is " << (x | y) << endl; // Binary OR operator
    cout << "x ^ y is " << (x ^ y) << endl; // Binary XOR operator
    cout << "~(x) is " << ~(x) << endl; // One's Complement operator
    cout << "x<<1 is " << (x << 1) << endl; // Left Shift operator
    cout << "x>>1 is " << (x >> 1); // Right Shift operator
    return 0;
    }

    Assignment Operators

    Assignment operator is used to assign a value to a variable. It is denoted by equal sign ( = ) and stores the value of the right-hand expression into the memory location of the left-hand variable.

    Example

    #include
    using namespace std;
    int main() {
    int total = 0;
    // Add multiple values and reassign
    total += 5;
    total += 10;
    total += 15;
    total += 20;
    total += 25;
    cout << total;
    return 0;
    }

    Ternary or Conditional Operators

    Conditional operator returns the value, based on the condition.ternary or conditional operator performs operations on three operands. therefore it is known as a Ternary Operator.
    (Condition x) ? Expression1 : Expression2;

    The Conditional operator ? determines the answer on the basis of the evaluation of Condition.
    If Condition is true, then Expression1 gets evaluated.
    If Condition is false, then Expression2 gets evaluated.

    Example

    #include <iostream>
    using namespace std;
    int main() {
    int a = 5, b = 6;
    // Conditional Operator
    int result = (a < b) ? b : a;
    cout << "The greatest number " "is " << result;
    return 0;
    }

    Misc Operators

    1. Sizeof() Operators
    2. Comma Operator (,)
    3. Arrow and dot Operator
    4. Casting Operator
    5. Addressof Operator (&) and Pointer Operator (*)





    Previous Next


    Trending Tutorials




    Review & Rating

    0.0 / 5

    0 Review

    5
    (0)

    4
    (0)

    3
    (0)

    2
    (0)

    1
    (0)

    Write Review Here


    Ittutorial