C Programming Operators
An
operator is a symbol that operates on a
value or a variable.
For example + is an
operator to perform addition.
Fig: Operators In C |
C
has a wide range of operators to perform
various operations.
1. Arithmetical Operator
2. Relation Operator
3. Increment / Decrement Operator
4. Logical Operation
5. Sizeof Operator
6. Assignment Operator
7. Comma operator
1. Arithmetical Operator
An arithmetical operator perform mathematical
operations such as addition,
subtraction, multiplication, division etc on a numerical values(constants and
variables).
Example:
I.
a+b=addition
II.
a-b=subtraction
III.
a*b=multiplication
IV.
a/b=division
V.
a%b=modulo
division
Program of Arithmetical Operator
#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(“Reminder
when a divided by b=%d \n”,c);
return
0;
}
Output:
a+b=13
a-b=5
a*b=36
a/b=2
Remainder
of a divided by b=1
2.
Relational Operator
A relational operator check the relationship between
two operands. If the relation is true, it return 1; if the relation is false,
it returns value 0.
Example:
I.
== Equal to 5==3 is evaluated to
0
II.
> greater than 5>3 is evaluated to 1
III.
< less than 5<3 is evaluated
to 0
IV.
!= Not equal to 5!=3 is evaluated to 1
V.
>= greater than or equal to 5==3 is evaluated to 1
VI.
<= less than
or equal to 5==3 is
evaluated to 0
Program of Relational Operator
#include<stdio.h>
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);
Output:
5==5
is 1
5==10
is 0
5>5
is 0
5>10
is 0
5<5
is 0
5<10
is 1
5
!=5 is 0
5
!=10 is 1
5>=5
is 1
5>=10
is 0
5<=5
is 1
5<=10
is 1
3.
Increment / Decrement Operators
C programming has two operators increment ++ and
decrement – to change the value of and operand by 1.
Increment ++
increase the value by 1 when decrement -- decrease the value by 1.
These two
operators are unary operators, meaning they only operate on a single value.
Program of Increment / Decrement Operator
#include<stdio.h>
int main()
{
int
a=10,b=100;
printf(“++a=%d \n”,++a);
printf(“--b =%d \n”,--b);
return0;
}
Output:
++a=11
--b=99
4.Logical Operator
An
expression containing logical operator return either 0 or 1 depending upon
whether expression result true or false.
Example:
I.
&& AND
II.
|| OR
III.
! NOT
Program of Logical Operator
#include<stdio.h>
int
main()
{
int a=5,
b=5, c=10, result;
result=(a==b)&&(c>b);
printf(“(a==b)&&(c>b)
is %d \n “, result);
result=(a==b)&&(c<b);
printf(“(a==b)&&(c<b)
is %d \n “, result);
result=(a==b)
|| (c<b);
printf(“(a==b)
|| (c<b) is %d \n “, result);
result=(a
!=b) || (c<b);
printf(“(a!=b)
|| (c<b) is %d \n “, result);
result=!(a!=b);
printf(“!(a==b)
is %d \n”,result);
result=!(a==b);
printf(“!(a==b)
is %d \n”,result);
return
0;
}
Output:
(a==b)
&& (c>b) is 1
(a==b)
&&(c<b) is 0
(a==b)
|| (c<b) is 1
(a!=b)
|| (c<b) is 0
!(a!=b)
is 1
!(a==b)
is 0
5.Sizeof Operator
The
sizeof is a unary operator that return the size of data.
Program of Sizeof Operator
#include<stdio.h>
int
main()
{
int a;
float b;
double c;
char d;
printf(“size
of int=%lu byte\n”,sizeof(a));
printf(“size
of float=%lu byte\n”,sizeof(b));
printf(“size
of double=%lu byte\n”,sizeof(c));
printf(“size
of char=%lu byte\n”,sizeof(d));
return
0;
}
Output:
Size
of int=4 byte
Size
of float=4 byte
Size
of double=8 byte
Size
of char=1 byte
6. Assignment Operator
An
assignment operator is used for assigning a value to a variable. The most common
assignment operator is =.
Example:
I.
= a=b a=b
II.
+= a+=b a= a+b
III.
-= a-=b a=a-b
IV.
*= a*=b a=a*b
V.
/= a/=b a=a/b
VI.
%= a%=b a=a%b
Program of Assignment Operator
#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
8.
Comma Operator
Comma operator are used to link related expression
together.
For Example:
int a, c=5,
d;
No comments:
Post a Comment