CodesJava

Easy learning with example program codes

CPP Operators


CPP Operators are used to perform operations on operands. Operands can be a variable or a constant. The operators are divided into various groups on the basis of the basic operations they perform.

 

Some of the basic operators are:

Arithmetic Operators:

The operators which are used to perform the arithmetical operations, are grouped together as Arithmetic operators.

Operators Symbol
Addition +
Subtraction
Multiplication *
Division /
Modulus %

 

Assignment Operators:

The operators which are used to assign values to a variable, are grouped together as Assignment operators.

Operators Symbol
Equals To =
Added Value +=
Subtracted Value -=
Multiplicated Value *=
Divided Value /=
Modulus Value %=

 

Comparison Operators:

The operators which are used to compare two values, are grouped together as Comparison operators.

Operators Symbol
Equal ==
Not equal !=
Greater than >
Less than <
Greater than or equal to >=
Less than or equal to <=

 

Logical Operators:

The operators which are used to perform logical operations, are grouped together as Logical operators.

Operators Symbol
AND &&
OR ||

 

Precedence and Associativity of C Operators:

The precedence and associativity of different types of operators in CPP are listed below.

PRECEDENCE OPERATORS ASSOCIATIVITY
Postfix Operators () [] -> . ++ – – Left to Right
Unary Operators + – ! ~ ++ – – (type)* & sizeof Right to Left
Multiplicative Operators * / % Left to Right
Additive Operators + – Left to Right
Shift Operators << >> Left to Right
Relational Operators < <= > >= Left to Right
Equality == != Left to Right
Bitwise AND & Left to Right
Bitwise XOR ^ Left to Right
Bitwise OR | Left to Right
Logical AND && Left to Right
Logical OR || Left to Right
Conditional Operator ?: Right to Left
Assignment Operators = += -= *= /= %=>>= <<= &= ^= |= Right to Left
Comma , Left to Right

Example:

#include <iostream.h>
using namespace std;
int main()
{
int sum1 = 10+20*10-90*10+700;  
int sum2 = (10+20)*10-(90*10)+700;
cout << sum1 << endl;
cout << sum2;
return 0;
}

Output:

10
100
Please follow and like us:
Posted in CPP   


Copyright © 2019 CodesJava DMCA.com Protection Status SiteMap Reference: Java Wiki