operators in c programming

 Operator 

Operator is symbol that operates on a literal or variable.It can be used to perform mathematical calculation,logical manipulation,Comparison etc.It is used in program for manipulation of data and variable.There are different types of operator in C programming.
 
    Arithmetic Operator

 It is used for mathematical calculation like Addition,Multiplication,Division etc.We can Perform all these operation on either literal or variable.

1]+(Addition): This operator(+) is used to perform addition between numbers.

2]-(Subtraction): This operator(-) is used to perform subtraction between numbers.

3]*(Multiplication): This operator(*) is used to perform Multiplication between numbers .

4] /(Divison): This operator(/) is used to perform Division between numbers.

5]%(Modulo): This operator(%) is used to find modulo or remainder of number .  

 

 


                                    

 
In above Program Important to notice is divison is 2 instead 2.25 its because we are storing the result on int type variable and int type variable truncate all the value after decimal hence the divison is 2.When we want the result of divison  accurate for eg 2.25 for 9/4 then we should use float type variable to store the result.In above eg if we want accurate result then make c variable as float type variable.
Click here to Learn more about Data type.
 
Code:
 
#include <stdio.h>  
 int main()  
 {  
   int a = 5, c;  
   c = a;       
 printf("c = %d\n", c);  
   c += a;     
   printf("c = %d\n", c);  
   c -= a;     
   printf("c = %d\n", c);  
   c *= a;     
   printf("c = %d\n", c);  
   c /= a;     
   printf("c = %d\n", c);  
   c %= a;    
   printf("c = %d\n", c);  
   return 0;  
 }  
 
 

Output:

 = 5   
= 10   
 c = 5   
 c = 25   
 c = 5   
 c = 0  
 
 
Assignment Operator 
This Operator is used for assigning value to variable or constant.This Operator put the value from the right hand side to the left hand side of Assignment(=) Operator.Right hand side value can be either in the form of variable or literal.The left hand should be variable only because we need some memory to store value and We know that when variable is declared it occupies some amount of memory.
Eg 
int a=5;
                                                  

 1]Assignment Operator:It is same as we have done in mathematics.I don't Think So it is required to explain.If Anyone want explanation on this then comment on this post.
2]Shorthand Operation:It is similar to Assignment Operator the only difference between them is format of writing.This is introduced in programming to reduce the code.Professionals use shorthand operator instead of Assignment Operator.
 
Eg of shorthand operation
a+=4 is equivalent to a=a+4 
a*=6 is equivalent to a=a*6
 

If you any have doubt,suggestion regarding this post or website then feel free to share with us.
 
If you have learned something new from this post then share this post with your family and
friends.
 
Happy Learning :)😎   

 

 

Previous Next

2 Comments

Previous Post Next Post