Introduction to Programming - (C Language) - Unit : 1 – OPERATORS
OPERATIONS
(OPERATORS)
An operator is simply a symbol that is used
to perform operations. There can be many types of operations like arithmetic,
logical, bitwise etc.
There are following types of operators to
perform different types of operations in C language.
·
Arithmetic Operators
·
Relational Operators
·
Shift Operators
·
Logical Operators
·
Bitwise Operators
·
Ternary or Conditional Operators
·
Assignment Operator
·
Miscellaneous Operator
Arithmetic Operators in C
·
C Arithmetic operators are used to perform mathematical
calculations like addition, subtraction, multiplication, division and modulus
in C programs.
S.no |
Arithmetic Operators |
Operation |
Example |
1 |
+ |
Addition |
A+B |
2 |
- |
Subtraction |
A-B |
3 |
* |
multiplication |
A*B |
4 |
/ |
Division |
A/B |
5 |
% |
Modulus |
A%B |
Example programs for C arithmetic operators
Program-1
/*program on arithmetic operators model-1*/
#include<stdio.h>
#include<conio.h>
void main()
{
int
n_1, n_2, add, sub, mul, m_div;
float
div;
clrscr();
printf("Enter
any two numbers:");
scanf("%d%d",
&n_1, &n2);
add
= n_1 + n_2;
sub
= n_1 – n_2;
mul
= n_1 * n_2;
div
= n_1 / n_2;
m_div
= n_1 % n_2;
printf("Addition
is : %d \nSubtraction is : %d \nMultipication is : %d \nDivision is:%f \nModular
division is : %d", add, sub, mul, div, m_div);
getch();
}
Output:
Enter any two numbers: 22
10
Addition is: 32
Subtraction is: 12
Multiplication is: 220
Division is: 2.000000
Modular division: 2
Program - 2
/*program on arithmetic operators model-2*/
#include<stdio.h>
#include<conio.h>
void main()
{
int
n_1, n_2, res;
clrscr();
printf("Enter
any two numbers:");
scanf("%d%d",
&n_1, &n_2);
res
= n_1 + n_2;
printf("Addition
is : %d", res);
res
= n_1 – n_2;
printf("\nSubtraction
is: %d", res);
res
= n_1 * n_2;
printf("\nMultiplication
is: %d", res);
res
= n_1 / n_2;
printf("\nDivision
is: %d",res);
res
= n_1 % n_2;
printf("\nModular
division is : %d", res);
getch();
}
Output:
Enter
any two numbers: 20
10
Addition
is:30
Subtraction
is:10
Multiplication
is:200
Division
is:2
Modular
division:0
Program-3
/*program on arithmetic operators model-3*/
#include<stdio.h>
#include<conio.h>
void main()
{
int
n_1, n_2;
clrscr();
printf("Enter
any two numbers:");
scanf("%d%d",
&n_1, &n_2);
printf("Addition
is: %d", n_1 + n_2);
printf("\nSubtraction
is: %d",n_1 – n_2);
printf("\nMultiplication
is: %d",n_1 * n_2);
printf("\nDivision
is: %d",n_1 / n_2);
printf("\nModular
division is: %d",n_1 % n_2);
getch();
}
Output:
Enter any two numbers: 5
2
Addition is:7
Subtraction is:3
Multiplication is:10
Division is:2
Modular division:1
Assignment operators in C
·
In C programs, values for the variables are assigned using
assignment operators.
·
For example, if the value “10″ is to be assigned for the
variable “sum”, it can be assigned as “sum = 10;”
·
Other assignment operators in C language are given below.
Operator |
Description |
Example |
= |
assigns values from right side operands to left side
operand |
a=b |
+= |
adds right operand to the left operand and assign the
result to left |
a+=b is same as a=a+b |
-= |
subtracts right operand from the left operand and assign
the result to left operand |
a-=b is same as a=a-b |
*= |
mutiply left operand with the right operand and assign the
result to left operand |
a*=b is same as a=a*b |
/= |
divides left operand with the right operand and assign the
result to left operand |
a/=b is same as a=a/b |
%= |
calculate modulus using two operands and assign the result
to left operand |
a%=b is same as a=a%b |
Example program for C assignment operators
/*program to swap and print two numbers
using assignment operators*/
#include<stdio.h>
#include<conio.h>
void main()
{
int
n_1, n_2 , tmp;
clrscr();
printf("Enter
any two values:");
scanf("%d%d",
&n_1, &n_2);
tmp
= n_1;
n_1
= n_2;
n_2
= tmp;
printf("n_1
= %d \n n_2 = %d", n_1, n_2);
getch();
}
Output:
Enter
any two values: 10
20
n_1
= 20
n_2
= 10
Relational operators in C
·
Relational operators are used to find the relation between
two variables. i.e. to compare the values of two variables in a C program.
S.no |
Operators |
Example |
Description |
1 |
> |
x > y |
x is greater than y |
2 |
< |
x < y |
x is less than y |
3 |
>= |
x >= y |
x is greater than or equal to y |
4 |
<= |
x <= y |
x is less than or equal to y |
5 |
== |
x == y |
x is equal to y |
6 |
!= |
x != y |
x is not equal to y |
Example program for relational operators in
C
/*program on using relational operators*/
#include<stdio.h>
#include<conio.h>
void main()
{
int
n1, n2;
clrscr();
printf("Enter
two numbers:");
scanf("%d%d",&n1,&n2);
printf("==
-%d",n1==n2);
printf("\n!=
-%d",n1!=n2);
printf("\n>=
-%d",n1>=n2);
printf("\n<=
-%d",n1<=n2);
printf("\n> -%d",n1>n2);
printf("\n< -%d",n1<n2);
getch();
}
Output:
Enter
two numbers:20
5
==
-0
!=
-1
>=
-1
<=
-0
> -1
< -0
Logical operators in C
·
These operators are used to perform logical operations on the
given expressions.
·
There are 3 logical operators in C language. They are,
logical AND (&&), logical OR (||) and logical NOT (!).
S.no |
Operators |
Name |
Example |
Description |
1 |
&& |
logical AND |
(x>5)&&(y<5) |
It returns true when both conditions are
true |
2 |
|| |
logical OR |
(x>=10)||(y>=10) |
It returns true when at-least one of the
condition is true |
3 |
! |
logical NOT |
!((x>5)&&(y<5)) |
It reverses the state of the operand
“((x>5) && (y<5))” If “((x>5) && (y<5))” is
true, logical NOT operator makes it false |
Truth table of Logical Operator
C1 |
C2 |
C1&C2 |
C1||C2 |
!C1 |
!C2 |
T |
T |
T |
T |
F |
F |
T |
F |
F |
T |
F |
T |
F |
T |
F |
T |
T |
F |
F |
F |
F |
F |
T |
T |
Example program for logical operators in C
/*program on using logical operators*/
#include<stdio.h>
#include<conio.h>
void main()
{
int
n1,n2,n3;
clrscr();
printf("enter
any three numbers:");
scanf("%d%d%d",&n1,&n2,&n3);
printf("%d",(n1!=n2)&&(n2<n3)&&(n1>n3));
printf("\n%d",(n1!=n2)||(n2<n3)||(n1>n3));
printf("\n%d",!(n1>n3));
getch();
}
Output:
Enter
any three numbers:20
30
10
0
1
0
Increment/Decrement Operators in C
· Increment operators are
used to increase the value of the variable by one and decrement operators are
used to decrease the value of the variable by one in C programs.
·
Syntax:
Increment operator :
++var_name; (or) var_name ++;
Decrement operator :
– - var_name; (or) var_name
– -;
·
Example:
Increment
operator: ++ i ; i ++ ;
Decrement
operator: – - i ; i – - ;
Difference between pre/post increment &
decrement operators in C:
Below table will explain the difference
between pre/post increment and decrement operators in C.
S.no |
Operator type |
Operator |
Description |
1 |
Pre increment |
++i |
Value of i is incremented before assigning it to
variable i. |
2 |
Post-increment |
i++ |
Value of i is incremented after assigning it to
variable i. |
3 |
Pre decrement |
– –i |
Value of i is decremented before assigning it to
variable i. |
4 |
Post_decrement |
i– – |
Value of i is decremented after assigning it to
variable i. |
Example program for increment and decrement operators
in C
/*program on using ++ and -- operators*/
#include<stdio.h>
#include<conio.h>
void main()
{
int
n;
clrscr();
printf("enter
any number:");
scanf("%d",&n);
printf("%d",n++);
printf("\n%d",++n);
printf("\n%d",n--);
printf("\n%d",--n);
getch();
}
Output:
Enter any number:10
10
12
12
10
Bitwise operators
l Bitwise operators perform
manipulations of data at bit level. These operators also
perform shifting of bits from right to left.
l Bitwise operators are not
applied to float or double.
Operator |
Description |
& |
Bitwise AND |
| |
Bitwise OR |
^ |
Bitwise exclusive OR |
<< |
left shift |
>> |
right shift |
Now lets see truth table for bitwise &, | and ^
a |
b |
a & b |
a | b |
a ^ b |
0 |
0 |
0 |
0 |
0 |
0 |
1 |
0 |
1 |
1 |
1 |
0 |
0 |
1 |
1 |
1 |
1 |
1 |
1 |
0 |
The bitwise shift operator, shifts
the bit value. The left operand specifies the value to be shifted and the right
operand specifies the number of positions that the bits in the value have to be
shifted. Both operands have the same precedence.
Example :
a = 0001000
b = 2
a << b = 0100000
a >> b = 0000010
Example program for bitwise operators in C
/*program on using bitwise operators*/
#include<stdio.h>
#include<conio.h>
void main()
{
int
a,b;
clrscr();
printf("enter
any two values:");
scanf("%d%d",&a,&b);
printf("%d",a&b);
printf("\n%d",a|b);
printf("\n%d",~a);
printf("\n%d",a^b);
printf("\n%d",a>>2);
printf("\n%d",a<<2);
getch();
}
Output:
enter any two values:13
6
4
15
-14
11
3
52
Conditional operator
The conditional operators in C language are known by
two more names
1. Ternary Operator
2. ? : Operator
It is actually the if condition
that we use in C language decision making, but using conditional operator, we
turn the if condition statement
into a short and simple operator.
The syntax of a conditional operator is :
expression 1 ? expression 2: expression 3
Explanation:
·
The question mark "?" in the
syntax represents the if part.
·
The first expression (expression 1) generally returns either
true or false, based on which it is decided whether (expression 2) will be
executed or (expression 3)
·
If (expression 1) returns true then the expression on the
left side of " : " i.e (expression 2) is executed.
·
If (expression 1) returns false then the expression on the
right side of " : " i.e (expression 3) is executed.
Example program for conditional operators in C
/*program on using conditional operators*/
#include<stdio.h>
#include<conio.h>
void main()
{
int
n;
clrscr();
printf("enter
any number:");
scanf("%d",&n);
(n>0)?printf("positive
no."):printf("negative no.");
getch();
}
Output:
enter any number:5
Positive no.
Special operators
Operator |
Description |
Example |
sizeof |
Returns the size of an variable |
sizeof(x) return size of the variable x |
& |
Returns the address of an variable |
&x ; return address of the variable x |
* |
Pointer to a variable |
*x ; will be pointer to a variable x |
Example program for special operators in C
/*program on using other operators*/
#include<stdio.h>
#include<conio.h>
void main()
{
int
a;
float
f;
char
ch;
char
n[20];
double
d;
long
int la;
clrscr();
printf("%d",sizeof(a));
printf("\n%d",sizeof(f));
printf("\n%d",sizeof(ch));
printf("\n%d",sizeof(n));
printf("\n%d",sizeof(d));
printf("\n%d",sizeof(la));
getch();
}
Output:
2
4
1
20
8
4
Comments
Post a Comment