MENU

Thursday, 7 April 2022

C++ Operators

 C++ Operators


In this tutorial, we will learn about the different types of operators in C++ with the help of examples. In programming, an operator is a symbol that operates on a value or a variable.

Operators are symbols that perform operations on variables and values. For example, + is an operator used for addition, while - is an operator used for subtraction.

Operators in C++ can be classified into 6 types:

1. Arithmetic Operators

2 .Assignment Operators

3 .Relational Operators

4. Logical Operators

5.  Bitwise Operators

6. Other Operators


1. C++ Arithmetic Operators

Arithmetic operators are used to perform arithmetic operations on variables and data. For example,
Here, the + operator is used to add two variables a and b. Similarly there are various other arithmetic operators in C++.
a + b;
OperatorOperation
+Addition
-Subtraction
*Multiplication
/Division
%Modulo Operation (Remainder after division)

2. C++ Assignment Operators

In C++, assignment operators are used to assign values to variables. For example,
Here, we have assigned a value of 5 to the variable a.
// assign 5 to a
a = 5;
OperatorExampleEquivalent to
=a = b;a = b;
+=a += b;a = a + b;
-=a -= b;a = a - b;
*=a *= b;a = a * b;
/=a /= b;a = a / b;
%=a %= b;a = a % b;

 3. C++ Relational Operators

A relational operator is used to check the relationship between two operands. For example,// checks if a is greater than
a > b;
OperatorMeaningExample
==Is Equal To3 == 5 gives us false
!=Not Equal To3 != 5 gives us true
>Greater Than3 > 5 gives us false
<Less Than3 < 5 gives us true
>=Greater Than or Equal To3 >= 5 give us false
<=Less Than or Equal To3 <= 5 gives us true

 

4. C++ Logical Operators

Logical operators are used to check whether an expression is true or false. If the expression is true, it returns 1 whereas if the expression is false, it returns 0.

OperatorExampleMeaning
&&expression1 && expression2Logical AND.
True only if all the operands are true.
||expression1 || expression2Logical OR.
True if at least one of the operands is true.
!!expressionLogical NOT.
True only if the operand is false.

5. C++ Bitwise Operators

In C++, bitwise operators are used to perform operations on individual bits. They can only be used alongside char and int data types.

OperatorDescription
&Binary AND
|Binary OR
^Binary XOR
~Binary One's Complement
<<Binary Shift Left
>>Binary Shift Right



6. Other C++ Operators

Here's a list of some other common operators available in C++. We will learn about them in later tutorials.

OperatorDescriptionExample
sizeofreturns the size of data typesizeof(int); // 4
?: returns value based on the conditionstring result = (5 > 0) ? "even" : "odd"; // "even"
&represents memory address of the operand&num; // address of num
.accesses members of struct variables or class objectss1.marks = 92;
->used with pointers to access the class or struct variablesptr->marks = 92;
<<prints the output valuecout << 5;
>>gets the input valuecin >> num;

0 comments:

Post a Comment

LATEST

PROGRAMMING LANGUAGES

WEB TECHNOLOGIES

DBMS

C

C++

PREPARATION

LATAST POSTS

Top