Operators In C

Types Of Operator :-

Classification 1 -

1. Unary operators - These operators act upon a single operand to produce a new value.
     Example-     x++;
2. Binary operators - These operators act upon two  operands to produce a new value.
     Example-  x=x+1;
3. Ternary operators - These operators act upon  three operands to produce a new value.
     Example- x= 10 > 5 ? 1 : 0 ; 

Classification 2 -

1. Arithmetic operators 
2. Relational operators
3. Logical operators
4. Bit-wise operators
5. Assignment  operators and compound assignment operators  
6. Conditional operators 

Rules Of  Operators :-

1. Precedence - It decides which operators would be performed first in an expression.
2. Associativity - It decided the order of evaluation if more than one operator enjoys the same precedence. There for three different type of associativity, there are:-
 (i)   left-to-right
(ii)   right-to-left
(iii)  right-left / left-right  


List of Operators:-


Pre-Fix And Post-Fix Operator  :-

Example . 1:-

   
#include<stdio.h>

#include<conio.h>

main()

{

  int i=1;

  int a[5]={2, 3, 7, 8, 9};

  int x;

  clrscr();

  printf("Before Decrement > i=%d\n", i);

  x=5*2+a[i--]; /* (First operate with a[i] then decrement 1. Due                       to post-fix Decrement Operator.)*/

  printf("x= %d\n",x);

  printf("After Decrement > i= %d\n", i);

  getch();

}



Output:-

  Before Decrement > i= 1
  x= 13
  After Decrement > i= 0


Example . 2:-

#include <stdio.h>
#include <conio.h>
main()
{
  int k, l,  x=5;
  clrscr();
  k = x++ + x++ + x++;
  printf("x= %d\n",x);
  printf("k= %d",k);
  getch();
}

Output:-

x= 8
k= 15

Note:☺️  Here the value of  x  is same in every case of addition. But x value is changed  
   in the expression which printf  function  only print the value of x. Here the resin is 
   x is increment by the post-fix format. ( Post-Fix :-  Use the variable first before  
   increment.)

Example : 2.1 :-

#include <stdio.h>
#include <conio.h>
main()
{
  int k, l,  x=5;
  clrscr();
  k = ++x + ++x + ++x;
  printf("x= %d\n",x);
  printf("k= %d",k);
  getch();
}

Output:-

x= 8
k= 24

Note:☺️  As the above program value of x is same as Example-2, But in case of addition
     it is different the value of k is different from  Example-2, Due to the pre-fix format.
     ( Pre-Fix :-  Increment the variable first before  use.)

Example . 2.2 :-

#include <stdio.h>
#include <conio.h>
main()
{
 int x=7;
 clrscr();
  if (++x == x++)
    {
      printf("Both ++x and x++ are same.\n");
    }
   else
      printf("Both ++x and x++ are different.\n");
  getch();
}

Output:-

Both ++x and x++ are same.

Note:☺️  As  ++x and x++ is always same in this case. But different in other cases. 

Example . 2.3 :-

#include <stdio.h>
#include <conio.h>
 main()
{
  int x=5;
  int k= x++ + ++x + x++ ;
  printf("k= %d\n", k);
  getch();
}

Output:-

  k = 19 

Note:☺️   Here in the example 2.3,  x++ + ++x is evaluated from right-to-left and ++x value become 6, then it added to x++ (x++ value is 6) and the result become 12. Then finally 12 is added to x++ (which value is 7, due to the post-fix x++ at first position) and it makes the k value to 19 .
                 The output may be different  between gcc compiler & Turbo C  compiler. 

Relational Operators:

There are many relational operators, such as >, <, >=, !=, ==, !. All relational operator return a value which is always either 0 or 1. 

Example . 3 :-


#include <stdio.h>
#include <conio.h>

main()

{

     int x=!70;

  clrscr();

     printf("%d", x);

  getch();

}



Output:-

  0

Note:☺️ The '!' relational operator is used to convert zero value to 1 and non-zero value to  0. It is the seventh priority operator.

Example . 3.1 :-

#include <stdio.h>
#include <conio.h>
main()
{
  int x=5<9;   // This expression is true, it return 1
  int y=7<3;   // This expression is false, it return 0
  clrscr();
  printf("%d\n", x);
  printf("%d", y);
  getch();
}

Output:-

  1
  0

Note:☺️   If the expression (5<9)   is true then it return 1. Otherwise it return 0. 

Example . 3.2 :-

#include <stdio.h>
#include <conio.h>
main()
{
  int x, y, z;
  clrscr();
  printf("Enter a no:>");
  scanf("%d", &x);
  printf("\nEnter another no:>");
  scanf("%d", &y);
  z=x>y;
  if(z==1)
        printf("x is biggest value\n");
  else
        printf("y is biggest value\n");
  getch();
}

Output:-

  Enter a no:> 7

 Enter another no:> 9

y is biggest value

Note:☺️ By using relational operator and conditional operator we can manipulate our output.
  

Bit-wise operator :-

There are  many bit-wise operators such &, |, ^, >>, << and ~. All bit-wise operators only work with integer type. For example ~ (ls) complement which converts 0 to 1 and 1 to 0 in memory of integer type.

Example . 4 :-

#include <stdio.h>
#include <conio.h>
main()
{
  short int  x= ~5;
  clrscr();
  printf("%d", x)
  getch();

}


Output:-

-6

Note:☺️   

Behind the program  

  5 = 0000000 000000101
~5 = 1111111 111111010

Indirection and Address of Operators (*, &) 


Both operators are are work with pointer . If the indirection (*) operator comes before a variable name, then it will be capable to store the address of a variable. For example we declare int *k Here k is a pointer variable which is able to store address of another variable . 

Example . 4 :-


#include <stdio.h>
#include <conio.h>
main()
{
  int x=70;
  int *p=&x;

  int z=&x;

  clrscr();

  printf("x= %d\n", *p);

  printf("Address of x= %d\n",p);

  printf("Address of x= %d\n", &x);

  printf("Address by z= %d", z);
  getch();
}

Output:-



x= 70
Address of x= 70
Address of x= -12
Address by z= -12

Note:☺️   Here *p represent the value of x.
                        represent the address of x, which is store by &x in to *p.
                        &x is directly represent the address of x.
                        z is represent the address of x, but can't represent the value of x.

   

Sizeof Operator :-


The sizeof operator is a text type operator which is used to measure the size of a variable or its type. It appears in two different flavours.

  • Function  like: sizeof
  • Object like : sizeof

    

Example . 4.1 :-


#include <stdio.h>
#include <conio.h>
main()
{

  int x;

  char z;

  clrscr();

  printf("size of x = %d\n", sizeof x);

  printf("size of int = %d\n", sizeof(int));

  printf("size of z = %d\n", sizeof z);

  printf("size of char = %d\n", sizeof(char));

  printf("size of double = %d\n", sizeof(double));
  printf("size of float = %d\n", sizeof(float));
  getch();
}

     

Size of variables in C :-

      
   
      

Example . 4.2 :-

#include <stdio.h>
#include <conio.h>
main()
{
int x;
int i=1;
clrscr();
x= 5*i + sizeof(--i) +2 -8/i;
printf("x= %d\n", x);
printf("size of (--i)= %d", sizeof(--i));
getch();

}
  


Output:-

x=1
sizeof(--i)= 2

       Note:☺️   In Linux size of an integer is 4 bytes.   But in Turbo C size of an integer is 2 bytes. So the output can be vary. 

Type Casting Operators:-


The type casting operator is used to convert one data type to another data type in c.

Example . 5 :-

#include <stdio.h>
#include <conio.h>
main()
{
int x=5, z=2;
float y;
clrscr();
y=x/2;
printf("y= %f\n", y);
getch();
}
    

Output:-

y= 2.00000

Third Priority Operators  ( / , % , * ) :-

Division(/): Extract the quotient value after division.
Modulus(%): Extract the reminder value after division.
Multiplication(*): Multiply the two numbers.
     

Example . 6 :-


#include <stdio.h>
#include <conio.h>
main()
{
int s=0;
int x=342;
clrscr(); 

s=s+ x%10;
x=x/10;
printf("s1= %d\n", s);

s=s+ x%10;
x=x/10;
printf("s2= %d\n",s);

s=s+x%10;
x=x/10;
printf("s3= %d", s);
getch();
}


Output:-

s1= 2

s2= 6

s3= 9

Fourth Priority Operators (+/-)  [left-to-right]

Example . 7 :-

#include<stdio.h>
#include<conio.h>
int main()
{
  int a, b, c;
  clrscr();
  a=3;
  b=7;
  c=a+b;
  printf("sum=%d",c);
  getch();
}

Fifth Priority Operators (>> / <<)   [left-to-right]

Both left shift(<<) and right shift(>>) operators are used to shift bits of an integer variable.
As many numbers of bits are shifted from the left side or right side of an integer variable, that may numbers of 0 are append on its opposite side.

The left-shift operator increases the value, in order of x*2n  where n is the number of bits shifted.
The right shift decreases the value, in order of  x/2 where n is the number of bits shifted.

Example . 7 :-

#include<stdio.h>
#include<conio.h>
main()
{
 char x=10;
 x=x>>1;
 printf("%d",x);
 getch();
}


// Output: 5

Example . 7.1 :-

#include<stdio.h>
#include<conio.h>
main()
{
 char x=10;
 x=x<<1;
 printf("%d",x);
 getch();
}

//Output: 20

Example . 7.2 :-


#include<stdio.h>

#include<conio.h>

main()

{

 int x = 20 >> 3 << 1 >> 2 << 1;

 printf("%d",x);
 getch();

//Output: 2



Sixth Priority Operators(> , <, >=, <=)   [left-to-right]

 The above relational operators , such as >, <, >=, <= are evaluate from left to right and their return value is always 0 or 1. Test the bellow example.



Example . 8 :-


#include <stdio.h>
#include <conio.h>
int main()
{
    int x = 5 > 4 > 3 < 1;
    printf("%d", x);
}

// output: 1

Here in the above program 5>4 returns 1, then 1>3 returns 0 and 0<1 returns 1.
So the output is 1 .

Seventh Priority Operators (!=, ==) [left-to-right]

The above relational operators, != and == are evaluate from left to right and their return value is always 0 or 1.

Example . 9 :-

#include <stdio.h>
#include <conio.h>
int main()
{
    int a = 5, b = 5, c = 5;
    if (a == b == c)
        printf("Hello Welcome");
    else
        printf("Hi");
}


// output: Hi

Here in the above example when the expression a==b is compiled, it returns 1, and
then the return 1 is compared with c which means 1==c and it returns 0, so that
the if condition became false and the else part is evaluated.

Bitwise Operators 

Some Bitwise operators 
   & : It is 8th priority operator , It used to check a bit is ON or OFF
   ^  : It is 9th priority operator , It used to make a bit ON or OFF
    |  : It  is 10th priority operator , It used to make a bit ON

Image of integer 10, Represent in bit format.






Example . 10 :-

Operator : Check the bit is ON or OFF

#include <stdio.h>
#include <conio.h>
int main()
{
    char x = 10;
    if (x & 1)
    {
        printf("OFF");
    }
    else
    {
        printf("ON");
    }
}

output// ON

Note: As the above example:10, the value of x is 10, 10 is convert to binary bit
as the above image. If the bit contain 1 then it is ON if the bit value is 0
then it OFF. You check the bit value by the index number.

Example . 10.1:-

^ Operator: To make a bit OFF/ On Lets fallow the program bellow.

//To make a second bit OFF
#include <stdio.h>
#include <conio.h>
int main()
{
    int x = 10 ^ 2;
    if (x & 2)
    {
        printf("ON");
    }
    else
    {
        printf("OFF");
    }
}

// Output: OFF
// Output in graphical format given bellow






Note: As you can see the second bit is 0.

Example 10.2:-

| Operator : Make a bit ON


//To make a third bit ON
#include <stdio.h>
#include <conio.h>
int main()
{
    int x = 10 | 4;
    if (x & 4)
    {
        printf("ON");
    }
    else
    {
        printf("OFF");
    }
}

// Out put : ON
out put in graphical format given bellow





Note: As you can see the third bit is 1.

Logical Operator :

|| : 11th priority operator

&& : 12th priority operator

Both logical operator | | and & & return either 1 or 0. and the code is evaluate from left to right.

Example 11:-


#include <stdio.h>
#include <conio.h>
int main()
{
    int x;
    int a = 5;
    x = 20 || --a;
    printf("%d %d", x, a);
}
// Output: 1 5

20 is a non zero value which is true, so the control never goes to the
second part. And --a is not evaluate. And the value of a doesn't change.

Example 11.1:-

#include <stdio.h>
#include <conio.h>
int main()
{
    int x;
    int a = 5;
    x = 0 && --a;
    printf("%d %d", x, a);
}

// Output: 0 5

Note: Here 0 is false value in c, which evaluates false. So the control never
goes to the second part.And the value of a is not changed.
Note: The logical operator || have higher priority than the && operator.
Show the bellow example 11.2 given billow.

Example 11.2:-

#include <stdio.h>
#include <conio.h>
int main()
{
    int x;
    int a = 1, b = 5, c = 10;
    x = --a && 5 * 3 + ++b * (3 - 2) || --c + 2;
    printf("%d %d %d %d ", x, a, b, c);
}


// Output: 1 0 5 9

Explanation:- In the above expression || operator have higher priority than the
&& operator. so first left sight of || is evaluate first. After that due to &&
operator (--a) is evaluate first in the total expiation. And then the right side
of && operator will be evaluate. After all of this right side of || operator will
be execute.

Thirteenth Priority Operator

Conditional operator :- ?:

Example 12:-

(Using if...else)

#include<stdio.h>
#include<conio.h>
int main()
{
    if(5>2)
    printf("Hello");
    else
    printf("Hi");
}

// Output: Hello

Note: When the if conditions is true then the if statement will be execute.
other wise else condition will be execute.

Example 12.1:-

(Using ? :operator )

#include <stdio.h>
#include <conio.h>
int main()
{
    5 > 2 ? printf("Hello") : printf("Hi");
}

// Output: Hello
Note: If the given condition is true then the part before (:) operator
is execute, other wise after the (:) operator will be execute.

Example:12.2:-

(Using if...else)


#include <stdio.h>
#include <conio.h>
int main()
{
    int a;
    if (5 > 2)
        a = 10;
    else
        a = 90;
    printf("a= %d", a);
}

// Output: a= 10


Example:12.3:-

(using ? : operator )

#include <stdio.h>
#include <conio.h>
int main()
{
    int a;
    a = 5 > 7 ? 10 : 90;
    printf("a= %d", a);
}

// Output: a= 90

Note: Value assignment through the conditional operator.


Example:12.4:-

(Using if...else)

#include <stdio.h>
#include <conio.h>
int main()
{
    if (5 > 2)
        return 1;
    else
        return 0;
}
Note: return statement by using if else statements


Example:12.5:-

(using ? : operator )

#include<stdio.h>
#include<conio.h>
int main()
{
    return 5>2 ? 1:0;
}

Note: return statement using conditional operator.








Post a Comment

0 Comments