Operator
Operator is that which can operate something. For example we want to add two number 10 and 20. We can do this putting ‘+’ between 10 and 20. Here ‘+’ operates 10 and 20 thus produces sum of 10 and 20. So, + is a operator and 10,20 are the operand. If an operator operates only one operand, it will call unar y operator.But if it operates tw0 operands, it will call binary operator. Suppose you write ‘-9’,here ‘-‘ is a unary operator and if you write ‘9-5’, here ‘-‘ will binary operator. ‘>’ and ‘<‘ are always binary operator. In C programming language some operators have different meaning, you must know this meaning. For in mathematics ‘=’ is called equal but in C program ‘=’ means assignment operator and ‘==’ means equal operator. There is another difference to represents the following line in C programming:
2<x<3
In mathematics the above line means the value of ‘x’ is greater than 2 and less than 3. But in C programming it should be written as follows:
x<2 && x<3
Here && means AND so the above line can be read as x>2 AND x<3.
Some operators in C program:
| Operator | Explanation | Example |
|---|---|---|
| + | Add two value |
#include<stdio.h>
int main()
{
int a,b,sum;
printf("Enter two value of a and b:");
scanf("%d%d",&a,&b);
sum=a+b;
printf("n Sum of a and b= %d",sum);
return 0;
}
OUTPUTInput two value of a and b:10 20 |
| – | Subtract two value |
#include<stdio.h>
int main()
{
int a,b,sub;
printf("Enter two value of a and b:");
scanf("%d%d",&a,&b);
sub=a-b;
printf("n Subtraction of a and b= %d",sub);
return 0;
}
OUTPUTInput two value of a and b:40 10 |
| * | Multiply two or more value. |
#include<stdio.h>
int main()
{
int a,b,mul;
printf("Enter two value of a and b:");
scanf("%d%d",&a,&b);
mul=a*b;
printf("n Multiple of a and b= %d",mul);
return 0;
}
OUTPUTEnter two value of a and b:10 3 |
| / | Divide one number by another number. |
#include<stdio.h>
int main()
{
int a,b,div;
printf("Enter two value of a and b:");
scanf("%d%d",&a,&b);
div=a/b;
printf("n Division of a and b= %d",div);
return 0;
}
OUTPUTEnter two value of a and b:60 20 |
| == | It means equal. | x=10 and y=10, then x==y. |
| != | It means not equal. | x=10 and y=15 then x!=y. |
| > | Greater than | x=15 and y=10 than x>y. |
| < | Less than | x=10 and y=15 then x<y. |
| <= | Less than or equal | x=10 and y=5 then x<=y is true. |
| >= | Greater than or equal | x=10 and y=10 then x>=y is true |
| % | It is called modulus operator. It is used to find the reminder of a division result. | Divide 12 by 5. Here the remainder is : 2. In C program you can find this reminder as follows:12%5
#include<stdio.h>
int main()
{
int a,b,c;
a=10;
b=3;
printf("%d",a%b);
printf("n%d",12%5);
return 0;
}
OUTPUT1 |
| = | It is called assignment operator.It is used to assign a value in a variable. |
#include<stdio.h>
int main()
{
int a;
a=10;
printf("Value of a= %d",a);
return 0;
}
OUTPUTValue of a= 10 |
| ++ | It is called increment operator. It is used to increase variable value by one | Suppose x=10 then x++ will increase the value of x by 1 and new value will be 11 and will be assigned to x. x++ is also equivalent to x=x+1
#include<stdio.h>
int main()
{
int x;
x=10;
x++;
printf("%d",x);
return 0;
}
OUTPUT11 |
| — — | It is called decrement operator. It is used to decrease variable value by one | Suppose x=10 then x– –will decrease the value of x by 1 and new value will be 9 and will be assigned to x.x– – is also equivalent to x=x–1
#include<stdio.h>
int main()
{
int x;
x=10;
x--;
printf("%d",x);
return 0;
}
OUTPUT9 |
Next Previous