Operators in C:-

In a language like C an Operators is a symbolic code that imposes an operation over either single or a group of operands
C supports the following three type of Operators.

  1. Unary Operator
  2. Binary Operator
  3. Ternary Operator

Unary Operator

Operators that require only one operand are called Unary Operators .
For Example-:
++i; //this statement inccreases the value of i by 1
Here ++ -: is an Operators whereas "i" is an Operand.

Binary Operator

These Operators requires atleast two operands. However, there is no maximum limit of Operands. Most of the C opearators are of binary types.
For example -: Mathematical and Relational Operators.
x=a+b
Here ,a and b are operands and = and + Symbols are Operands

Ternary Operator -:

The only major Operators of ternary type is the conditional Operators requires atleast 3 operands .
Example-: ____?_____:______
Here, ? and : are Operators.


List of Operators in C.

  1. Mathematical Operators
  2. Relational Operators
  3. Logical Operators
  4. Increament and decreament Operators
  5. Conditional Operators
  6. Assignment Operators
  7. Size of Operators
  8. Miscellaneous Operators


Mathematical Operators

We have a total of 5 mathematical Operators that are given in C these are " + , - , * , / , % "
Priority of Mathematical Operators -:
C puts only 3 levels of priority when an expression comes with two or more Operators of different priorities the Operators of high priority will be entertained first. In case of two or more Operators having same priority, the first encountered will be entertained first. For Example -:
2+3*6-7+9/3
2+18-7+9/3
2+18-7+3
20-7+3
13+3
16

Relational Operators

Operators that show relationships between two operands are called relational Operators these are -: <,>,<=,>=,==,!=
For Example-:
int x,y,z;
x=5;
y=6;
z=x>=y;

Note-:
The above expression will return a Boolean value (0 for false and 1 for true).


Logical Operators

Logical Operators introduce binary logic to the C language. It works on boolean values as well. The major use of logical operations is when we have to check two or more conditions at a time curently. These are of three types-:

Logical AND -:

Logical AND is written as (&&) it checks two condition simultaneously and performs the following logical operations.




Here, False represnts 0 whereas True represents 1 .

For Example -:
int x,y,z,a;
x=5;
y=6;a=9;
z=(a>y)&&(y>x)

__true_____true__


The final result will also be True ie 1 .

Logical OR -:

The result is true when any of the conditions being tested together is True LOGICAL OR performs the following operations -:



Here, False represents 0 whereas true represents 1.

Increament and Decreament Operators-:

In C increament and dcreament Operators are used to increase or decrease the value of a variable the increament Operators is (++) and Decreament is (--)
The increament and decreament Operators are further classified in two types-:


Example-:

#include< stdio.h>
#include< conio.h>
void main()
{
int a,b,c,d,x;
x=13;
a=x++;
b=++x;
c=x--;
d=--x;
printf("%d%d%d%d",a,b,c,d);
getch();
}


Conditional Operators-:

Conditional Operators requires three operands the first operand is always a condition .the second operand activates whwn condition is true and third operand activates when condition is false.

For Example-: int a=5,x;
x=(a>2)?(a-1):(a+1);

Asiignment Operators-:

It is a binary Operators hence it requires two operands. The right Operand to this Operators is always transferred to the left operand.
For Example-:
int x;
x=5;

Sizeof Operators-:

Size of Operators reflects the size in bytes of either a variable or a data type or a constant.


For Example-:

int x,y;
y=sizeof(x) or y=sizeof(int)

Miscllaneous Operators:-

C supports some other Operators as well and every Operators has its own usage.

Address Operators-: This Operators reflects the adress of a variable ie stored somewhere in the memory
For Example-:

int x;
x=6;
printf("%d",x);
printf("%u",&x);

Comma Operators-: Comma Operators can be used to distinguish two or more assignment statement. It can also be used while displaying different variables. This Operators is used in variable declaration as well.

Program that adds every digit of a four digit number-:


#include< stdio.h>
#include< conio.h>
void main()
{
int num=1436,a,b,c,d;
a=num%10;
num=num/10;
b=num%10;
num=num/10;
c=num%10;
num=num/10;
d=num%10;
printf("%d",a+b+c+d);
getch();
}

Explanation of the above code-:


1436/10=143
1436/100=14
1436/1000=1

Whereas-:

1436%10=6
1436%100=36
1436%1000=436
1436%10000=1436



Program to reverse a number of four digit-:


#include< stdio.h>
#include< conio.h>
void main()
{
int num=1436,a,b,c,d;
a=num%10;
num=num/10;
b=num%10;
num=num/10;
c=num%10;
num=num/10;
d=num%10;
printf("%d%d%d%d",a,b,c,d);
getch();
}

Program to add 1 to all the digits of a four digit number "3768" and display the new number formed-:


#include< stdio.h>
#include< conio.h>
void main()
{
int num=1436,a,b,c,d;
a=num%10;
num=num/10;
a=a+1;
b=num%10;
num=num/10;
b=b+1;
c=num%10;
num=num/10;
c=c+1;
d=num%10;
d=d+1;
printf("%d%d%d%d",d,c,b,a);
getch();
}

Program to add 1 to all the digits of a four digit number and if 9 occurs in the number convert it to zero-:


#include< stdio.h>
#include< conio.h>
void main()
{
int num=1436,a,b,c,d;
a=num%10;
num=num/10;
a=a+1;
a=a%10;
b=num%10;
num=num/10;
b=b+1;
b=b%10;
c=num%10;
num=num/10;
c=c+1;
c=c%10;
d=num%10;
d=d+1;
d=d%10;
printf("%d%d%d%d",d,c,b,a);
getch();
}