WikiGalaxy

Personalize

C++ Arithmetic Operators

Addition Operator (+):

The addition operator is used to add two operands. It is a binary operator and is represented by the symbol '+'.

Subtraction Operator (-):

The subtraction operator is used to subtract the right operand from the left operand. It is represented by the symbol '-'.

Multiplication Operator (*):

This operator multiplies two operands. It is represented by the symbol '*'.

Division Operator (/):

The division operator divides the left operand by the right operand. It is represented by the symbol '/'.

Modulus Operator (%):

The modulus operator returns the remainder of a division operation. It is represented by the symbol '%'.


#include <iostream>
using namespace std;

int main() {
    int a = 10, b = 20;
    cout << "Addition: " << (a + b) << endl;
    cout << "Subtraction: " << (a - b) << endl;
    cout << "Multiplication: " << (a * b) << endl;
    cout << "Division: " << (b / a) << endl;
    cout << "Modulus: " << (b % a) << endl;
    return 0;
}
    

Console Output:

Addition: 30 Subtraction: -10 Multiplication: 200 Division: 2 Modulus: 0

C++ Relational Operators

Equal to Operator (==):

This operator checks whether two operands are equal. It returns true if they are, and false otherwise.

Not Equal to Operator (!=):

This operator checks whether two operands are not equal. It returns true if they are not equal, and false otherwise.

Greater than Operator (>):

This operator checks if the left operand is greater than the right operand.

Less than Operator (<):

This operator checks if the left operand is less than the right operand.

Greater than or Equal to Operator (>=):

This operator checks if the left operand is greater than or equal to the right operand.

Less than or Equal to Operator (<=):

This operator checks if the left operand is less than or equal to the right operand.


#include <iostream>
using namespace std;

int main() {
    int a = 5, b = 10;
    cout << "Equal to: " << (a == b) << endl;
    cout << "Not Equal to: " << (a != b) << endl;
    cout << "Greater than: " << (a > b) << endl;
    cout << "Less than: " << (a < b) << endl;
    cout << "Greater than or Equal to: " << (a >= b) << endl;
    cout << "Less than or Equal to: " << (a <= b) << endl;
    return 0;
}
    

Console Output:

Equal to: 0 Not Equal to: 1 Greater than: 0 Less than: 1 Greater than or Equal to: 0 Less than or Equal to: 1

C++ Logical Operators

Logical AND Operator (&&):

This operator returns true if both operands are true, otherwise it returns false.

Logical OR Operator (||):

This operator returns true if at least one of the operands is true, otherwise it returns false.

Logical NOT Operator (!):

This operator reverses the logical state of its operand. If a condition is true, it becomes false, and vice versa.


#include <iostream>
using namespace std;

int main() {
    bool a = true, b = false;
    cout << "Logical AND: " << (a && b) << endl;
    cout << "Logical OR: " << (a || b) << endl;
    cout << "Logical NOT: " << (!a) << endl;
    return 0;
}
    

Console Output:

Logical AND: 0 Logical OR: 1 Logical NOT: 0

C++ Assignment Operators

Assignment Operator (=):

This operator assigns the value on the right to the operand on the left.

Add and Assign Operator (+=):

This operator adds the right operand to the left operand and assigns the result to the left operand.

Subtract and Assign Operator (-=):

This operator subtracts the right operand from the left operand and assigns the result to the left operand.

Multiply and Assign Operator (*=):

This operator multiplies the right operand with the left operand and assigns the result to the left operand.

Divide and Assign Operator (/=):

This operator divides the left operand by the right operand and assigns the result to the left operand.


#include <iostream>
using namespace std;

int main() {
    int a = 10;
    a += 5;
    cout << "Add and Assign: " << a << endl;
    a -= 3;
    cout << "Subtract and Assign: " << a << endl;
    a *= 2;
    cout << "Multiply and Assign: " << a << endl;
    a /= 4;
    cout << "Divide and Assign: " << a << endl;
    return 0;
}
    

Console Output:

Add and Assign: 15 Subtract and Assign: 12 Multiply and Assign: 24 Divide and Assign: 6

C++ Increment and Decrement Operators

Increment Operator (++):

This operator increases the value of its operand by 1. It can be used as a prefix or postfix.

Decrement Operator (--):

This operator decreases the value of its operand by 1. It can also be used as a prefix or postfix.


#include <iostream>
using namespace std;

int main() {
    int a = 10;
    cout << "Initial: " << a << endl;
    cout << "Post-increment: " << a++ << endl;
    cout << "After Post-increment: " << a << endl;
    cout << "Pre-decrement: " << --a << endl;
    return 0;
}
    

Console Output:

Initial: 10 Post-increment: 10 After Post-increment: 11 Pre-decrement: 10

C++ Bitwise Operators

Bitwise AND Operator (&):

This operator performs a bitwise AND operation on two operands.

Bitwise OR Operator (|):

This operator performs a bitwise OR operation on two operands.

Bitwise XOR Operator (^):

This operator performs a bitwise XOR operation on two operands.

Bitwise NOT Operator (~):

This operator inverts all the bits of its operand.

Bitwise Left Shift Operator (<<):

This operator shifts the bits of the left operand to the left by the number of positions specified by the right operand.

Bitwise Right Shift Operator (>>):

This operator shifts the bits of the left operand to the right by the number of positions specified by the right operand.


#include <iostream>
using namespace std;

int main() {
    int a = 5, b = 9;
    cout << "Bitwise AND: " << (a & b) << endl;
    cout << "Bitwise OR: " << (a | b) << endl;
    cout << "Bitwise XOR: " << (a ^ b) << endl;
    cout << "Bitwise NOT: " << (~a) << endl;
    cout << "Left Shift: " << (a << 1) << endl;
    cout << "Right Shift: " << (b >> 1) << endl;
    return 0;
}
    

Console Output:

Bitwise AND: 1 Bitwise OR: 13 Bitwise XOR: 12 Bitwise NOT: -6 Left Shift: 10 Right Shift: 4

C++ Conditional Operator

Ternary Operator (?:):

This operator is used to evaluate a condition and return one of two values depending on whether the condition is true or false. It is a shorthand for the if-else statement.


#include <iostream>
using namespace std;

int main() {
    int a = 10, b = 20;
    int max = (a > b) ? a : b;
    cout << "Maximum: " << max << endl;
    return 0;
}
    

Console Output:

Maximum: 20

C++ Comma Operator

Comma Operator (,):

The comma operator is used to separate two or more expressions. The expressions are evaluated from left to right, and the value of the last expression is returned.


#include <iostream>
using namespace std;

int main() {
    int a, b;
    a = (b = 3, b + 2);
    cout << "Value of a: " << a << endl;
    return 0;
}
    

Console Output:

Value of a: 5

C++ Sizeof Operator

Sizeof Operator:

The sizeof operator is used to get the size, in bytes, of a data type or a variable. It helps in determining how much memory a variable occupies.


#include <iostream>
using namespace std;

int main() {
    int a;
    cout << "Size of int: " << sizeof(a) << " bytes" << endl;
    cout << "Size of double: " << sizeof(double) << " bytes" << endl;
    return 0;
}
    

Console Output:

Size of int: 4 bytes Size of double: 8 bytes

logo of wikigalaxy

Newsletter

Subscribe to our newsletter for weekly updates and promotions.

Privacy Policy

 • 

Terms of Service

Copyright © WikiGalaxy 2025