Operators in C++
0 3032
C++ operators are a special type of functions or symbols that take one or more than one argument and create a new value or in other words used to perform various logical and mathematical manipulations or calculations.
They act as the foundation of C++ programming, languages such as C or C++ is incomplete without operators.
C++ consists of a list of operators that are divided into groups such as:
- Arithmetic Operators (+,-,*, /, %)
- Unary operator (++,-)
- Assignment Operator (=,+=)
- Relational Operators (<,>,>=, <=,! = ,==)
- Ternary operators (?:)
- Bitwise Operators (&,<<,>>,~,^,|)
- Logical Operators (&&, ||)
- Misc Operator (sizeof(),*,?:,&,)
- Ternary Operator
List of Operators present in C++
Operators | Syntax |
Assignment operators | P=r |
Subtraction | P-r |
Addition | P+r |
Division | P\r |
Multiplication | P*r |
Unary plus | +r |
Modulo | --r,r-- |
The decrement operator (pre, post) | ++r,r++ |
!ERROR! unexpected operator '=' | p==r |
Not equal to | p != r |
Less than | < |
Greater than | > |
Less than equal to | <= |
Greater than equal to | >= |
Logical or | p || r |
Logical not | !p |
Addition assignment | p += q |
Scope Resolution operators | p :: q |
Division assignment | p \= q |
Logical and | p&&q |
Comma | p,r |
Following is the simple program on operators:
#include<iostream>
// Main Function
using namespace std;
int main ()
{
// Variable Declaration
int p = 38;
int r = 20;
int z = 40;
int q = 60;
int result;
cout << "Operators Example Program \n";
result = p - r; // subtraction (Subtraction or unary minus Arithmetic Operator)
cout <<"\np - r = "<<result;
result = r * z; // multiplication (Multiplication Arithmetic Operator)
cout <<"\nr * z ="<< result;
result = p / z; // division (Division Arithmetic Operator)
cout <<"\na / c = "<< result;
result = p + r * z; // precedence (Addition or unary plus Arithmetic Operator)
cout <<"\np + r * z = "<< result;
cout <<"\np * r + z * q = "<< p * r + z * q; // Mixed
return 0;
}
Result:
Operators Example Program
p - r = 18
r * z = 800
a / c = 0
p + r * z = 838
p * r + z * q = 3160
Share:
Comments
Waiting for your comments