In this tutorial, you will come to know about the various operator in the C programming language.
Operators are the base of any programming language and in C also the language is incomplete without the functionality of the operators.
If you want to know more about elements of C then read "Elements of C" .
What is an Operator in C?
An
operator is a symbol that operates between two values (operands ).
In C programming language we have a variety of operators to make our program easy.
In C programming language we have a variety of operators to make our program easy.
Arithmetic operator:-
Table for arithmetic operator and their functions:-
Operator | Functions |
---|---|
+ | adds two operands |
- | subtract second operands from first |
* | multiply two operand |
/ | divide numerator by denominator |
% | remainder of division |
++ | Increment operator - increases integer value by one |
-- | Decrement operator - decreases integer value by one |
To know about how to run a program in C and top Compiler for C read this post " Top Ide for C and C++ developers - 2019"
Let's do a C program for performing all the basic arithmetic operations:-
#include <stdio.h>
#include <conio.h>
void main()
{
int n1,n2,a,s,m,d,md;
clrscr();
printf(“enter two integer number\n”);
scanf(“%d%d”,&n1,&n2);
a=n1+n2;
s=n1-n2;
m=n1*n2;
d=n1/n2;
md=n1%n2;
printf(“addition value=%d\n”,a);
printf(“subtraction value=%d\n”,s);
printf(“multiplication value=%d\n”,m);
printf(“division value=%d\n”,d);
printf(“modulo division value=%d\n”,md);
getch();
}
Logical Operators:-
Table for the logical operator and their functions:-
Operator | Functions | Example |
---|---|---|
&& | Logical AND | (a && b) is false |
|| | Logical OR | (a || b) is true |
! | Logical NOT | (!a) is false |
Let's do a C program for performing all the basic logical operations:-
#include <stdio.h>
#include
int main()
{
int a = 5, b = 5, c = 10;
printf("%d == %d is %d \n", a, b, a == b);
printf("%d == %d is %d \n", a, c, a == c);
printf("%d > %d is %d \n", a, b, a > b);
printf("%d > %d is %d \n", a, c, a > c);
printf("%d < %d is %d \n", a, b, a < b);
printf("%d < %d is %d \n", a, c, a < c);
printf("%d != %d is %d \n", a, b, a != b);
printf("%d != %d is %d \n", a, c, a != c);
printf("%d >= %d is %d \n", a, b, a >= b);
printf("%d >= %d is %d \n", a, c, a >= c);
printf("%d <= %d is %d \n", a, b, a <= b);
printf("%d <= %d is %d \n", a, c, a <= c);
return 0;
}
Relational Operators:-
It is used to compare two or more operands using different symbols like >, <, ==, =!.
Table for the Relational operator and their functions:-
Operator | Functions |
---|---|
== | Check if two operand are equal |
!= | Check if two operand are not equal. |
> | Check if operand on the left is greater than operand on the right |
< | Check operand on the left is smaller than right operand |
>= | check left operand is greater than or equal to right operand |
<= | Check if operand on left is smaller than or equal to right operand |
Let's do a C program for performing all the basic relational operations:
#include <stdio.h>
#include
int main()
{
int a = 5, b = 5, c = 10;
printf("%d == %d is %d \n", a, b, a == b);
printf("%d == %d is %d \n", a, c, a == c);
printf("%d > %d is %d \n", a, b, a > b);
printf("%d > %d is %d \n", a, c, a > c);
printf("%d < %d is %d \n", a, b, a < b);
printf("%d < %d is %d \n", a, c, a < c);
printf("%d != %d is %d \n", a, b, a != b);
printf("%d != %d is %d \n", a, c, a != c);
printf("%d >= %d is %d \n", a, b, a >= b);
printf("%d >= %d is %d \n", a, c, a >= c);
printf("%d <= %d is %d \n", a, b, a <= b);
printf("%d <= %d is %d \n", a, c, a <= c);
return 0;
}
Assignment Operators :-
It is used to assign a value to the variable. it is of two types.
i. simple assignment(=)
ex: int a=5;
ii. compound assignment(+=,-=,*=,/=) ex: sum+=i;
Let see the program of assignment operator effectively:-
#include <stdio.h>
int main()
{
int a = 5, c;
c = a; // c is 5
printf("c = %d\n", c);
c += a; // c is 10
printf("c = %d\n", c);
c -= a; // c is 5
printf("c = %d\n", c);
c *= a; // c is 25
printf("c = %d\n", c);
c /= a; // c is 5
printf("c = %d\n", c);
c %= a; // c = 0
printf("c = %d\n", c);
return 0;
}
Special Operators :-
i. comma:- used to concatenate string with variable part. ex: printf(“addition value=%d”,c);
used to separate constants, variable and expressions.
Ex:-
constant separation int
a[5]={1,2,3,4,5}; variable separation
int a,b,c; expression
separation int a=5,b=4
ii. semicolon:- used to terminate a line of code.
ex: int a=9;
Bitwise Operators:-
The operator which is used to perform bit operation over the operand or variable as 0 and 1 is called bitwise operator.
types:-
i.
bitwise and(&)
ii.
bitwise or(|)
iii.
bitwise xor(^)
iv.
bitwise complement(~)
v.
bitwise left shift(<<)
vi.
bitwise right shift(>>)
Unary Operators:-
The operator which is used to perform over a single operand is called unary operator.
ex:-
a++
Binary Operators:-
The operator which is use to operate over two operand is called binary operator.
ex:-
a+b
Ternary Operators:-
The operator which is used to
operate over more than two operands are called ternary
operator.
syn:- (condition)? print statement1:print statement2;
To know more about basic of C Programming language you can refer this article "Introduction to C Programming Language - Introduction , Basic and History "
You can comment us if you think anything for the above topic is inappropriate or you want to add more information .
To know more about basic of C Programming language you can refer this article "Introduction to C Programming Language - Introduction , Basic and History "
You can comment us if you think anything for the above topic is inappropriate or you want to add more information .
What are operators in c ?
Reviewed by Digital Roy
on
January 08, 2020
Rating:
No comments: