C++ provides a rich set of operators for various programming tasks. These operators enable arithmetic computations, logical evaluations, bitwise manipulations, assignments, and more. Understanding their categories, usage, and precedence is fundamental for effective C++ programming.
Arithmetic Operators
These operators perform mathematical calculations:
+: Addition-: Subtraction*: Multiplication/: Division%: Modulo (remainder)++: Increment--: Decrement
int num1 = 10;
int num2 = 3;
int sum = num1 + num2; // 13
int product = num1 * num2; // 30
int remainder = num1 % num2; // 1
num1++; // num1 becomes 11
Relational Operators
Used for comparing two values:
==: Equal to!=: Not equal to>: Greater than<: Less than>=: Greater than or equal to<=: Less than or equal to
bool isEqual = (num1 == num2); // false
bool isGreater = (num1 > num2); // true
Logical Operators
Combine or negate boolean expressions:
&&: Logical AND||: Logical OR!: Logical NOT
bool condition1 = (num1 > 0); // true
bool condition2 = (num2 < 5); // true
bool combined = condition1 && condition2; // true (if both are true)
bool inverted = !condition1; // false
Bitwise Operators
Operate on the individual bits of integer types:
&: Bitwise AND|: Bitwise OR^: Bitwise XOR~: Bitwise NOT (complement)<<: Left shift>>: Right shift
int bitwiseNum1 = 5; // Binary: 0101
int bitwiseNum2 = 3; // Binary: 0011
int andResult = bitwiseNum1 & bitwiseNum2; // Binary: 0001 (Decimal: 1)
Assignment Operators
Assign values to variables:
=: Simple assignment+=: Add and assign-=: Subtract and assign*=: Multiply and assign/=: Divide and assign%=: Modulo and assign&=,|=,^=,<<=,>>=: Compound bitwise assignment
int value = 5;
value += 3; // value is now 8 (equivalent to value = value + 3)
Conditional (Ternary) Operator
Provides a shorthand for simple conditional assignments:
? :
int age = 20;
std::string status = (age >= 18) ? "Adult" : "Minor"; // status is "Adult"
Member Access Operators
Used to access members of objects and structures:
.: Direct member access->: Member access through a pointer
struct Coordinate {
int x;
int y;
};
Coordinate point = {10, 20};
Coordinate* ptrPoint = &point;
int currentX = point.x; // 10
int currentY = ptrPoint->y; // 20
Pointer and Address Operators
Involve memory addreses:
&: Address-of operator (gets the memory address of a variable)*: Dereference operator (accesses the value pointed to by a pointer)
int data = 42;
int* dataPtr = &data; // dataPtr holds the address of data
int retrievedValue = *dataPtr; // retrievedValue is 42
Type Casting Operators
Explicitly convert values from one type to another:
static_cast<type>(expression)dynamic_cast<type>(expression)reinterpret_cast<type>(expression)const_cast<type>(expression)
double pi = 3.14159;
int intPi = static_cast<int>(pi); // intPi becomes 3
Other Operators
sizeof: Returns the size of a type or object in bytes.new,delete: Allocate and deallocate dynamic memory.new[],delete[]: Allocate and deallocate dynamic arrays.::: Scope resolution operator, used to qualify names in scope.,: Comma operator, evaluates left to right and returns the value of the rightmost operand.
int* dynamicArray = new int[5]; // Allocate an array of 5 integers
delete[] dynamicArray; // Deallocate the array
Operator Precedence and Associativity
Operator precedence determines the order in which operations are performed when multiple operators are present in an expression. Associativity determines the order when operators have the same precedence.
Precedence (High to Low):
::(Scope resolution).,->,[],()(Member access, array subscript, function call)++,--(Postfix and Prefix Increment/Decrement)+,-,!,~,&(unary),*(dereference),sizeof,new,delete, casts*,/,%(Multiplicative)+,-(Additive)<<,>>(Bitwise shift)<,<=,>,>=(Relational)==,!=(Equality)&(Bitwise AND)^(Bitwise XOR)|(Bitwise OR)&&(Logical AND)||(Logical OR)?:(Conditional/Ternary)=,+=,-=, etc. (Assignment),(Comma)
Associativity:
- Most operators are left-associative (e.g.,
a - b + cis evaluated as(a - b) + c). - Operators like assignment (
=,+=) and the conditional operator (?:) are right-associative (e.g.,a = b = 5is evaluated asa = (b = 5)).
Using parentheses () is highly recommended to clarify the order of operations and improve code readability, especially in complex expressions.