![]() |
Operators and Operands in C Language |
Operators
Operators is a symbol that is used to perform certain mathematical or logical operations on data and variables.
They usually from a part of mathematical or logical expressions. Such as =, +, -, * , /, <, >, & etc.
In C language there are many types of operators:
- Arithmetic operators
- Relational operators
- Logical operators
- Assignment operators
- Increment and decrement operators
- Conditional operators
- Bitwise operators
- Other operators
Arithmetic Operators
In C language provide, all the basic arithmetic Operators, such as +, -, *, /, %.
Example : a+b, a-b ,b/a, a*b, b%a, etc.
Operator and it's Meaning
- + Addition
- - Subtraction
- * Multiplication
- / Division
- % Modulus ( for find remainder ).
Program:
#include<stdio.h>int main(){int a=2,b=3,sum;sum = a+b;//sum = a-b;//sum = a*b;printf("%d",sum);return 0;}
Output: 5
Relational Operators
In c language , we use for compare two elements. Such as < , > , <= , >= , == , !=.
Example: a<b, b>c, a==0, etc.
Operator and it's meaning
- < Less than
- > Greater than
- <= Less than or equal to
- >= Greater than or equal to
- == Is equal to
- != Not equal to
Program:
#include<stdio.h>int main(){int a=2,b=3,sum;if(a<b)printf("b is greater: %d",b);return 0;}
Output : b is greater: 3
Logical operators
In C language, There three types of logical Operators.
Such as && , || , !.
Operators and it's meaning
- && AND
- || OR
- ! NOT
Program:
#include<stdio.h>int main(){int a=2,b=3,c=1,sum;if(a<b&&b>c)printf("b is greater: %d",b);return 0;}
Output: b is greater: 3
Assignment operator
In C language, Assignment operators used for assign the result, Such as =.
Assignment operator also used to shorthand operators.
Simple Assignment shorthand Assignment operators operators
- a = a+1 a+=1
- a = a-1 a-=1
- a = a*(n+1) a*=n+1
- a = a/(n+1) a/=n+1
- a = a%b a%=b
Program:
#include<stdio.h>int main(){int a=2, b=3;a+=1;b-=1;printf("a :%d\nb :%d",a,b);return 0;}
Output: a : 3
b :2
Increment And Decrement Operators
In C language, two very useful operators, the operator ++ use for add 1 to the operand and -- use for subtract 1.
Such as. ++ a; or a++;
-- a; or a--;
Program:
#include<stdio.h>int main(){int a=2, b=3 ,c=4,d=5;a++;++c;b--;--d;printf("a:%d\nc:%d\nb:%d\nd:%d",a,c,b,d);return 0;}
Output: a:3
c:5
b:2
d:4
Conditional operators
In C language, A ternary operator pair " ? : " is available , to construct expression of the form.
Example:- exp1 ? exp2 : exp3.
Program:
#include<stdio.h>int main(){int a=2, b=3,x;x=(a>b) ? a:b;printf("%d",x);return 0;}
Output: 3
Simple program:
#include<stdio.h>int main(){int a=2, b=3,x;if(a>b)x=a;elsex=b;printf("%d",x);return 0;}
Output:3
Bitwise Operators
In C language, A distinction of supporting special operator known as bitwise operators for manipulation of data at bit level.
Such as &, | , ^ ,<<, >>.
Operators and it's meaning
- & Bitwise AND
- | Bitwise OR
- ^ Bitwise XOR
- << Shift left
- >> Shift right
- ~ Compliment
Program
#include<stdio.h>int main(){int a=2, b=3,y,x;x = (~a);y = (a&b);printf("%d %d",x,y);return 0;}
Output: -3 2
Others operator
In C language, supports some special operators such as comma operator, size of operator and pointer operator.
Example "," , " ." " -> " , "#" ,"##", "*".
What is operand?
An operand is a value , which an operator acts in a programming or mathematical expression. Operands are the components that the operator processes. They can be variables, constants, or expressions.
Example:
In the statement a = 3*b, the operands are 3 and b for the * operator.
Types of Operands:
- Constants
- Variables
- Expressions