C++ Language: Operators
Photo by Luca Bravo on Unsplash

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

Airthmetic Operators


Examples Of Airthmetic Operations


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.

Assignment Operators


Examples Of Assignment Operators
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.

Relational(Comparison) Operators


Examples Of Relational(Comparison) Operators

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.

Logical Operators
//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.

Bitwise Operators


Examples Of Bitwise Operators

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: Do not worry too much about these operators for now, they will be covered in future articles.


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.

Left-To-Right Associativity


Right To Left Associativiy



Table For Operator Precedence And 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

  1. Use Parentheses for Clarity
  2. Avoid Complex Expressions
  3. Be Careful with Assignment vs Comparison

Modern C++ Considerations

  1. Use const Where Possible
  2. Prefer Member Operators Over Global Functions

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:

https://www.instagram.com/s.talhanehal


Adama ZOUMA

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.

回复
Syed Talha Nehal

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 ??

回复

要查看或添加评论,请登录

Syed Talha Nehal的更多文章

  • C++ Language: Comments

    C++ Language: Comments

    Comments in C++ or any other programming language are explanatory statements you can include in your code. They explain…

    1 条评论
  • C++ Language: Input & Output

    C++ Language: Input & Output

    In this article, we will explore how to take input and how to ouput things in C++ language. Input and output are one of…

    1 条评论
  • C++ Language: Variables & Data Types

    C++ Language: Variables & Data Types

    In this article, we will discuss variables and data types in C++ language. Variables can be thought of as containers or…

    1 条评论
  • Your First C++ Language Program

    Your First C++ Language Program

    In the previous article, we installed and setup our environment for C++ programming. Now, let's take the next step by…

    2 条评论
  • Getting Started With C++ Language

    Getting Started With C++ Language

    One of the first—and most important—decisions you'll make is choosing the right IDE (Integrated Development…

    3 条评论
  • Introduction To Programming in C++ Language

    Introduction To Programming in C++ Language

    C++ was introduced in 1985 by Bjarne Stroustrup as an enhancement to C. It has heavily influenced languages like Java…

    3 条评论