C is very rich in built-in operators. An operator is a symbol that tells the compiler to
perform specific mathematical or logical manipulations. There are three general
classes of operators in C: -
 
Arithmetic Operators
Relational and Logical Operators
Assignment Operators 
In addition, C has some special operators to perform particular tasks: -
 
Ternary(Conditional) Operator
Sizeof Operator
Comma Operator
& Operator 
 And   -> Operators 

 Arithmetic Operators  
 
The table below lists the arithmetic operators provided in C++. They can be applied to any built in data_type allowed by C++. The unary minus, in effect multiplies its single operand by -1. Therefore, a number preceded by a minus sign changes its sign. 


The modulus division (%) operation yeilds the remainder of an integer division.
However, % cannot be used on type float or double. 

The following code fragment illustrates the use: -

# include <iostream.h>
void main()
{  
int x, y;
x=10; y=3;
cout<<x / y;     /* displays 3, division of two integers*/
cout<<x % y); /* displays 1, the remainder of the integer division */ 
 x=1;  y=2; 
cout<<x / y);  /* displays 0, integer division 1/2 yeilds 0 */ 
cout<< x % y;  /* displays 1, the remainder of  integer division */
The output is: 3 10 1  

Increment and Decrement Operators
 
C allows two very useful operators not usually found in other languages. These are increment and decrement operators, ++ and --. The operation ++ adds 1 to its single operand, and -- subtracts 1. Therefore, the following are equivalent operations: 
x = x+1; 
is the same as
x++; or ++x; 
and 
x = x - 1; 
is the same as
x--; or --x; 

The increment and decrement operators may either precede or follow the operand. However, there is a difference when they are used in expressions. When an increment or decrement operator precedes its operand, C++ performs the increment or decrement operation prior to using the operand's value. If the operator follows its operand, C uses the value of the operand's value after incrementing or decrementing
it. 

Consider the following example: 
 
x = 10;
y = ++x;
 
In this case, y is set to 11.

However, if the code had been written as 

x = 10;
y = x++;
 
y would have been set to 10. In both the cases, x is set to 11; the difference is, when it
happens.

Let's us see how different kinds of arithmetic operators can be used in a program:- 
  
# include <iostream.h>
main()
 int a, b, c, d, x, y, p, q;
 a=20; b=35; c= a+b; d= b-a; 
cout<<"******Addition & Subraction*****"; 
cout<<"a = "<<"b =\n"<< a<< b; 
cout<<" c = "<<"d = \n"<<c<<d; 
x = b / a;  
y = b % a; 
cout<<"******Division & Modulus******";  
}
cout<<"x =\n"<<x; 
cout<<"y = \n"<<y); 
p=++a; 
q= c + b--; 
cout<<"******Increment & Decrement******"; 
cout<<"a = \n"<<a); 
cout<<"b =\n"<<b); 
cout<<"p = \n"<<p); 
cout<<"q = \n"<<q); 
}

The output is:
 
Addition & Subraction a=20  b = 35  c= 55  d = 15  
Division & Modulus   x= 1  y= 15 
Increment & Decrement a=  21  b= 34  p= 36  q=  71 

Relational and Logical Operators
 
In Relational Operators, the word relational refers to the relationships that the 
values can have with one another. Depending on the relations, the values can be
compared. This comparison results either in true or false. 
 
Example:
10 > 20 (false)
10 < 20 (true) 

Operators like >, <  used to compare values are called relational operators. C
supports six relational operators.

 The operators and their meaning is listed in the table given below: -
In Logical Operators, the word logical refers to the ways relationships can be connected  together using the rules of formal logic. C++ has three logical operators: -

exp1 && exp2  evaluates to true only if both the expressions are true 
x= 10; y = 12;

x < 12 && y > 10  evaluates to true since the individual expression is true

x < 5 && y = = 12 evaluates to false since the first expression is false.

exp1 || exp2  evaluates to true if either of the expression is true
x= 10; y= 12;

x > 12 ||  y > 10 evaluates to true since at least one expression is true 

x = = 20 || y != 12  evaluates to false since neither expression is true.

 ! exp      evaluates to true if its value is not equal to the expression

!x = 10       false since x is 10

!x = 12   true since x is not 12 

&& and | | are used when we want to test more than one condition and make decisions.
 
Example:  

  a > b && c = 12

evaluates to true only if both expressions evaluate to true. 
In  C++, a true value is any value other than 0. A false value is 0. 
Expressions using relational or logical operators return 0 for false and 1 for true.
Therefore, the following code fragment is not only true, but also prints the integer value of true i.e. 1. 

#include <iostream.h>
main()
{
 int x;
 x=100;
 cout<<(x = =100);
The output is :  1 
 
Relational and Logical operators are lower in precedence than the Arithmetic operators. This means that when arithmetic expressions are used on either side of the relational operators, the arithmetic expression is evaluated first and then the results are compared. For example, 10 > 1+ 12 is evaluated only after evaluating 1+12 i.e., 13. Now, the resultant 10 > 13 is evaluated, which comes out to be false. 

Assignment Operator  
 
Assignment operator (=) is used to assign the result of an expression to a variable. It
takes the form as: -  

var_name = expression; 
var_name is the name if the variable which will be assigned the value of expression
by the assignment operator (=).
 
Example:  
 
char name;
int x, y, z;
name = "Ashu";  /* the variable name is assigned the value, 
Ashu */             x = 12; /* x is assigned the value 12 */
y = 98;   /* y is assigned the value 98 */
z = x + y  /* z is assigned the sum of x and y */ 
In addition to this, C++ has a set of 'shorthand' assignment operators of the form:
 
var_name oper= expression; 

oper is a binary arithmetic operator. The operator oper=  is called the 'shorthand' 
assignment operator. The assignment statement var_name oper= expression is
equivalent to
 
var_name = var_name oper expression;
 
Example: 
 
count = count + 1;

is equivalent to  
count+ = 1;

The shorthand operator +=  means ' increment count by 1'. 

Conditional ( :? ) Operator 
 
C++ has a very powerful and convenient operator that can be used to construct conditional expressions. It is termed as conditional operator.  Sometimes, it is also called ternary operator since it operates on three operands. The :? operator takes the form: -    
exp1 ? exp2 : exp3
exp1, exp2, exp3 are expressions.

The ternary  operator works like this: -
exp1 is evaluated. If it is true, exp2 is evaluated and becomes the value of the expression. If exp1 is false, then exp3 is evaluated and its value becomes the value of the expression. 

Example:  
 x = 10; 
y = x > 9 ? 100 : 200; 
In this example, y  will be assigned the value 100. If x had been less than or equal to 9, y would have received the value 200.
The same code, using the if-else statement is written as: - 
 x =10;
 if ( x > 9)
   { 
 y = 100;
}
else
{
 y = 200;

Size of Compile-time Operator 
 
Size of is a unary compile-time operator that returns the length of the operand, in
bytes. The operand can be a variable, a constant or a data type.  
 
Example:  
 
#include <iostream.h>
main()
{
 char ch;
 cout<<sizeof ch;
 cout<<sizeof(int); 

The output is: 12

Comma Operator
 
The comma operator is used to string together several expressions. Essentially, the comma causes a sequence of operations to be performed. When it is used on the right side of the assignment statement, the last expression of the comma-separated list, is the value assigned to the expression. 

Example: 
y= 10;
x = (y- = 5, 25 / y); 

After execution, x will have the value 5 because y's original value is reduced by 5,
and then that value is divided into 25, yielding 5 as the result.  

Precedence of Operators 
  
          Table  lists the precedence of all C++ operators.