Operators in programming-I
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 .
// Working of arithmetic operators
#include <stdio.h>
int main()
{
int a = 9,b = 4, c;
c = a+b;
printf("a+b = %d \n",c);
c = a-b;
printf("a-b = %d \n",c);
c = a*b;
printf("a*b = %d \n",c);
c = a/b;
printf("a/b = %d \n",c);
c = a%b;
printf("Remainder when a divided by b = %d \n",c);
return 0;
}
Output
a+b=13
a-b=5
a*b=36
a/b=2
Remainder when a divided by b=1
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.
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
#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
c = 5
c = 10
c = 5
c = 25
c = 5
c = 0
C Programming Course from Scratch:https://bit.ly/3rd8eq2