C Programming - Operators

C Programming - Operators


OPERATOR

A symbol that instructs C to perform some operation on one or more operands is called operator.
The data on which operator are performed is called Operand.
For example: p=3+4; P, 3 and 4 are operands where as = and + are operator and p=7 is the result.



The operators provided by C language are as follows:

1. Arithmetic Operator:
Operators which are used in mathematical expression are called arithmetical operators. The various arithmetic operators are:

Operators
Name
Example
+
Addition
a=2+3=5
-
Subtraction
a=5-1=4
*
Multiplication
a=3*4=12
/
Division
a=12/2=6
%
Modulus(Remainder)
a=12%2=0

2. Logical Operator
These operators are generally used in conditional expression. The three logical operators in C are as follows:
Operators
Name
Example
&&
Logical AND
(a>b)&&<a>c)
||
Logical OR
(a>b)||(a>c)
!
Logical NOT
!(a==b)

3. Assignment Operator
The assignment operator calculates the expression on the right side and gives the values of left side variables.
Example: -   a=2+3=5
C has a set of “shorthand” assignment operator of the form
V op = exp;
Where, V is variable, op is arithmetic operator and exp is an expression. 
Example:  a+ = 1   i.e. a = a+1
Here, at first the arithmetic operation is performed than only assignment operation is performed.
4. Relational Operator
It is used to compare the values between operands and gives a result whether it is true or false. It is also called comparison operator due to its comparing characteristics. There are 6 relational operators are:


Operators
Name
Example
< 
Less than
a<b
< =
Less than equal to
a<=b
> 
Greater than
a>b
>=
Greater than equal to
a>=b
= =
Equal to
a==b
! =
Not equal to
a!=b

5. Ternary Operator (? :)
C has only ternary operator of this operators result in terse (shorten) and compact mode.
Syntax:
<condition> ? <expression-1> : <expression-2>
If the condition is true, expression-1 will be executed; otherwise, expression-2 will be executed. The ? : is similar to if -else statement.
Example:
int x,a,b;
a=6;
b=10;
x= (a<b)? a:b;
printf("%d",x);
Here, the value of x is 10.

6. Comma Operator
The comma operator can be used to link the related expressions together. A comma-linked list of expressions is evaluated left to right and the value of right-most expression is the value of the combined expression. For examples: The statement
                             F= (a=2, b=3, a+b)
First assigns the value 2 to a, then assign 3 to b and finally assigns 5 to F.