C - Operator


C programming support different types of operator. The c programming can be used with variables  and constants to form expressions. Operators are categorized  below-
1.) Arithmetic operators             
2.) Equality operators
3.) Relational operators
4.)  Logical operators
5.) Unary   operators
6.) Conditional operator
7.) Bitwise  operators
8.) Assignment operators
9.) Comma operator
10.)  Sizeof operator
Let us understand one by one.
an be applied to any integer and floating value. It is consisting of five operator, these are  -:


 Operation 
 Operator Symbols
 Syntax Of  Operators 
 Addition
              +    
     a+b
 Subtraction
              -
     a-b
 Multiplication 
              *
     a*b
 Division
               /
     a/b
 Modulus
             %
     a%b

Let us see one by one -:
suppose   a=7,b=2  are declare in the program then

     1.
 calculation
 expression
 result
     2.
 addition
 a+b
      9
     3.
 subtraction
 a-b
     5
     4.
 multiplication
 a*b
     14

 There  are some other concept  in Modulus and division-:
 Let us see regarding division-:
  a / b        =     3
 15 / 7       =     2
-15 / 7       =    -2
 15  /  -7     =    -2
-15  /  -7    =    2

 Notes

1.)keep  in mind without  symbol number taken as positive .
2.)if both side of division symbol positive then result is positive.
3.)if any one side of  division symbol negative  result is negative.
4.)ignore the remainder.
5.)all these four operator are application on the integer values as well as on float values.

 Let us see regarding  modulus-:

   15   %    7      =         1 
  -15   %    7      =        -1
   15    %  -7      =         1
  -15    %  -7      =        -1
    7     %   15    =         7
   
Notes

1.) Keep in mind modulus operator is not applicable on floating values.  
2.)It is applicable on the  integer values.
3.)remainder is taken in the result.
4.)sign of numerator is taken only .
5.)if first value is smaller than second than result is smaller  value.

Let us see some  basic  concept with program example  -:

Integers and  float  conversion 


1. ) An arithmetic operation between  an integer and integer  always give result an integer.
2. ) An arithmetic operation  between an integer and float  always give result an float.
3. ) An arithmetic operation  between  an float  and float always give result an float.

 operation 
 result
 operation
          result
 10/4
     2             
     6/3              
          2
 10.0/4          
     2.5
    6.0/3
2.0        
 10/4.0
     2.5
    6/3.0
         2.0
 10.0/4.0
     2.5
    6.0/3.0
         2.0

1.)  #include<stdio.h>      
      int main()                                        
      {                                                  
      int a;                                            
      a=5/2.0;                                        
      printf("%d",a); 
      return 0;                       
      }   
      Output :  2
      Run this code                                           

2.) #include<stdio.h>
     int  main()
     {
     float a ;
     a=5/2.0;    
     printf("%f",a);
     return 0;
     }
     Output : 2.500000 
     Run this code

In the above first program 5/2.0 after solving  the value will be  2.5 but into the variable a 
value will be assign 2 because of integer data type declaration.But in the second program 5/2.0 after solving the value will be 2.5. And into the variable a value will be assign 2.5 because  float data type declaration.

1.)  #include<stdio.h>       
      int main()                                  
       {                                        
       int a,b,c;                             
       a=4;                                       
       b=6;                                    
       c=a+b;                                
       printf("%d",c);
       return 0;                      
       }
       Output :  10
       Run this code
                          
 2.) #include<stdio.h>
      int main()
      {
      int a,b,c;
      a=4;
      b=6;
      a+b=c;
      printf("%d",c);
      return 0;
      }
      Output : error will come because wrong syntax use a+b=c
      Run this code

In   first program  c=a+b;  is correct way to store sum of  a  and   but  in the second program a+b=c
is not a correct  way to write sum of two numbers. Keep in mind:-
1. ) (/,%,*)  having same  priority nut more than (-,+) and  associativity  left to right.
2. )  (-,+) having same priority but less than (/,%,* ) and associativity left to right .

Let us see another some condition


1. )  a=6*2/5;
In this case question will solve left to right because all these arithmetic symbol (%,/,*) having same priority but associativity left to right.
2. ) z =a*b+c/d;
In this case (+)     having less priority  than   (/,*)  so firstly * and  /   operator will solve after that  (+)   operator solve.,
2. )  z=a+b%c*d/e;
In this example (%,*,/) having more priority than (+) so solve firstly. It (%,*,/) will solve left 
to right because of same associativity example program-:

#include<stdio.h>
 int main()
 {
 int a,b,c,d,e,z;
 a=4;
 b=6;
 c=9;
 d=7;
 e=5;
 z=a+b%c*d/e;
 printf("%d",z);
 return 0;
 }
 Output is=12
 Run this code

With following above  rules give answer of given question 

Find the value of x in each questions :
1. ) x=6%(-5);
2. )  x=8%6*5/4;
3.  ) x=8%5%2*10+2;
4. )  x=8+4-7*5%1;
5. )  x=9%6+3*4/2;    
6.) #include<stdio.h>
    int main()
    {
    float x;
    x=5/2;
    printf("%f",x);
    return 0;
    }

<<Prev                              Next>>