C++ Language: Operators
C++ Language offers a wide range of operators to manipulate variables and perform operations. Whether you are a beginner or an experienced developer, understanding these operators is essential. In this article, we will explore operators in C++ with examples to help you master them. Let's start by discussing the six different types of operators in C++ Language.
1. Airthmetic Operators
Aithmetic Operators are used to perform airthmetic or mathematical operations on variables and data/values
2. Assignment Operators
Assignment operators are used to assign values to variables. We assign the value of right side operand into left side operand in accordance to which assignment operator we are using.
a = 9
a += b is 13
a -= b is 5
a *= b is 36
a /= b is 2
a %= b is 1
3. Relational (Comparison) Operators
Relational or comparison operators compare two on variables or data/values and return a boolean result true or false.
4. Logical Operators
Logical operators are used to check whether an expression is true or false. They are used to combine two or more conditions/constraints, OR to complement the evaluation of the original condition.
//Examples Logical Operators
Suppose,
a = 5
b = 8
Then,
(a > 3) && (b > 5) retrurns true
(a > 3) && (b < 5) returns false
(a > 3) || (b > 5) returns true
(a > 3) || (b < 5) returns true
(a < 3) || (b < 5) returns false
!(a < 3) returns true
!(a > 3) returns false
5. Bitwise Operators
Bitwise operators are used to perform operations on individual bits. Or it can be said they operate on binary representation of numbers. So, compiler first converts operands to bit-level and then the calculation is performed on the operands.
They can only be used alongside char and int data types.
6. Other Operators
There are operators in C++ language that do not quite fit into a category. I have described them in the table below.
NOTE: The list of operators in some categories is not exhaustive, to avoid tediousness they have not been discussed here.
Operator Precedency and Associativity
When there are multiple operators in a single expression, operator precedency and associativity decide in which order and which part of expression are calculated. Precedency tells which part of expression should be calculate first and associativity tells which direction to solve when same precedency operators are in expression.
Operator Precedency
Multiplication operator has higher precedence than the addition operator ?
For example
x = 7 + 3 * 2 ;
here, x is assigned 13, not 20
because * operator has higher precedence than +,
so it first gets multiplied 3*2 and then adds into 7.
Operator Associativity
Operator associativity says if expression have more than one operator with same precedence then calculation happen in right to left or left to right.
Most operators in C++ (like +, -, *, /, %, <<, >>, &, |, ^, &&, ||, etc.) have left-to-right associativity.
Some operators (like =, +=, -=, *=, /=, %=, ^=, |=, &=, ? :, sizeof, new, delete, etc.) have right-to-left associativity.
Here, operators with the highest precedence appear at the top of the table, those with the lowest appear at the bottom. The right most column mentions the associativity of operators.
Best Practices
Modern C++ Considerations
Mastering C++ operators is essential for any serious C++ developer. By understanding operator precedence, and following best practices, you can write clearer, more maintainable code.
Stay Motivated, Keep Experimenting, and Keep Learning
Happy Coding! ????
Follow Me On Instagram For Updates:
Senior DSP/C++/Algorithm Developer at EXFO
3 周Operators follow consensus: 1. On Mars, MyInt(3) + MyInt(7) = 3 * 7 = 21 2. On Earth, MyInt(3) + MyInt(7) = 3 + 7 = 10 3. On Jupiter, MyInt(3) + MyInt(7) = |3 - 7| = 4 4. Custom types define their own rules for +. 5. Built-in type operators cannot be overloaded.
Software Engineer at Xpert Digital
1 个月What are your thoughts on C++? Have you used it in any of your projects? Let's discuss this in the comments ??