You are on page 1of 32

Operators in C

2 types of Operators : Unary and Binary

UNARY : means it accepts only 1 operand


Ex: Inc/Dec operators , !(NOT) , ~ , Unary + , Unary - etc
Unary + : Ex: +6 , +67 , +5 etc (positive integer values)
Unary - : Ex: -3 , -6 , -56 etc (negative integer values)

BINARY : 2 operands
Ex: + , - ,* , / , > , < , >= ,<= , && , || etc
Akshata Angadi, Assistant Professor, CSE Dept., GIT
Operators in C
• Arithmetic Operators
• Relational Operators
• Logical Operators
• Bitwise Operators
• Assignment Operators
• Special Operators

Note: Solve examples on all kinds of operators.


Akshata Angadi, Assistant Professor, CSE Dept., GIT
Arithmetic Operators
If A = 20 and B = 35
Operator Description Example
+ Adds two operands. A + B = 55
− Subtracts second operand from the frst. A − B = -15
* Multiplies both operands. A * B =700
/ Divides numerator by de-numerator. B/A=1
Modulus Operator and remainder of after
% B % A = 15
an integer division.
Increment operator increases the A++ (post-inc) ,
++
integer value by one. ++A (pre-inc)
A-- (post-dec) ,
Decrement operator decreases the
-- --A (pre-dec)
integer value by one.

Akshata Angadi, Assistant Professor, CSE Dept., GIT


Back
Relational Operators
If A = 10 and B = 20
Operator Description Example

(A == B)
Checks if the values of two operands are equal or not. If
== is not
yes, then the condition becomes true.
true.
Checks if the values of two operands are equal or not. If
(A != B)
!= the values are not equal, then the condition becomes
is true.
true.
Checks if the value of left operand is greater than the (A > B)
> value of right operand. If yes, then the condition becomes is not
true. true.
Checks if the value of left operand is less than the value (A < B)
<
of right operand. If yes, then the condition becomes true. is true.
Checks if the value of left operand is greater than or (A >= B)
>= equal to the value of right operand. If yes, then the is not
condition becomes true.
Akshata Angadi, Assistant Professor, CSE Dept., GIT true.
Back
Logical Operators
If A = 56 and B = 0
Operator Descripton Example
Called Logical AND operator. If both the
&& operands are non-zero, then the (A && B) is false.
condition becomes true.
Called Logical OR Operator. If any of the
|| two operands is non-zero, then the (A || B) is true.
condition becomes true.
Called Logical NOT Operator. It is used to
reverse the logical state of its operand. If
! !(A && B) is true.
a condition is true, then Logical NOT
operator will make it false.

Back
Akshata Angadi, Assistant Professor, CSE Dept., GIT
Bitwise Operators
Operator Description
Binary AND Operator copies a bit to the result if
&
it exists in both operands.
Binary OR Operator copies a bit if it exists in
|
either operand.
Binary XOR Operator copies the bit if it is set in
^
one operand but not both.
Binary Ones Complement Operator is unary and
~
has the efect of 'fipping' bits.
Binary Left Shift Operator. The left operands
<< value is moved left by the number of bits
specifed by the right operand.
Binary Right Shift Operator. The left operands
>> value is moved right by the number of bits
specifed by the right operand.
Akshata Angadi, Assistant Professor, CSE Dept., GIT
Exercise
• Bitwise AND operation of two integers 12 and 25.
12 -> 0000 1100
25 -> 0001 1001
X Y X&Y
8 -> 0000 1000 (result)
0 0 0

0 1 0

1 0 0
result = 12 & 25
1 1 1

Akshata Angadi, Assistant Professor, CSE Dept., GIT


Exercise
• Bitwise OR operation of two integers 12 and 25.
12 -> 0000 1100
25 -> 0001 1001
X Y X|Y
29 -> 0001 1101
0 0 0

0 1 1

1 0 1

1 1 1

Akshata Angadi, Assistant Professor, CSE Dept., GIT


Exercise
• Bitwise XOR operation of two integers 12 and 25.

12-> 0000 1100


25-> 0001 1001
X Y X|Y
21 -> 0001 0101
0 0 0

0 1 1

1 0 1
Note: Bitwise complement of
1 1 0
any number ~N is -(N+1). 

Akshata Angadi, Assistant Professor, CSE Dept., GIT


Right Shift ( >> )
#include <stdio.h>
main( )
{
int a = 60;
printf("\n Number is Shifted By 1 Bit : %d", a >> 1 );
printf("\n Number is Shifted By 2 Bits : %d", a >> 2 );
printf("\n Number is Shifted By 3 Bits : %d", a >> 3 );
}
Akshata Angadi, Assistant Professor, CSE Dept., GIT
Right Shift ( >> )
A >> 1 , Given A = 60
60- 111100 Binary Equivalent
Shift at right by 1 , last element is 0
011110
=2^4+2^3+2^2+2^1
= 16 + 8 + 4 + 2
= 30

Answer : 30 – ( 011110 )
Akshata Angadi, Assistant Professor, CSE Dept., GIT
Right Shift ( >> )
A >> 3 , Given A = 60
60- 111100 Binary Equivalent
Shift at right by 3 , last element is 0
000111
= 2^2+2^1+2^0
= 4+2+1
= 7

Answer : 7
Akshata Angadi, Assistant Professor, CSE Dept., GIT
Example : A=60, B=13
(A & B) = 12,
i.e., 0000 1100
(A | B) = 61,
i.e., 0011 1101
(A ^ B) = 49,
i.e., 0011 0001
(~A ) = -61,
i.e,. 1100 0011 in 2's complement form.
A << 2 = 240
i.e., 1111 0000
A >> 2 = 15
i.e., 0000 1111
Akshata Angadi, Assistant Professor, CSE Dept., GIT Back
#include <stdio.h>
main( ) { //unsigned means it wont take negative values for a & b
unsigned int a = 60; /* 60 = 0011 1100 */
unsigned int b = 13; /* 13 = 0000 1101 */
int c = 0;
c = a & b; /* 12 = 0000 1100 */
printf("Line 1 - Value of c is %d\n", c );
c = a | b; /* 61 = 0011 1101 */
printf("Line 2 - Value of c is %d\n", c );
c = a ^ b; /* 49 = 0011 0001 */
printf("Line 3 - Value of c is %d\n", c );
c = ~a; /*-61 = 1100 0011 */
printf("Line 4 - Value of c is %d\n", c );
c = a << 2; /* 240 = 1111 0000 */
printf("Line 5 - Value of c is %d\n", c );
c = a >> 2; /* 15 = 0000 1111 */
printf("Line 6 - Value of c is %d\n", c ); Assistant Professor, CSE Dept., GIT
Akshata Angadi,
Assignment Operators
Shorthand Operators
=, += , -= , *= , /= , %= , <<= , >>=
&= , |= (IN-OR) , ^= (EX-OR) ,

Example:
a+=b means a = a + b
x/=g means x = x / g
d&=c means d = d & c
Akshata Angadi, Assistant Professor, CSE Dept., GIT
Misc or special Operators
• Conditional Operator ( ;? )
• Asterisk (*) Operator
• sizeof( ) Operator

Operato
Description Example
r
sizeof(a), where a is integer,
sizeof( ) Returns the size of a variable.
will return 4.
Returns the address of a &a; returns the actual
&
variable. address of the variable.

* Pointer to a variable. *a;

Akshata Angadi, Assistant Professor, CSE Dept., GIT


Conditional Operator(Alternative to
if-else) If(a>b)
Syntax: printf(“a is
larger”);
(test_Condtion) ? Option A for true : Option B for false; else
printf(“b is
larger”);
Ex: Prog. to check a is larger or b and print. OR
If(a>b)
(a>b) ? printf(“a is larger”) : printf(“b is larger”) ;
printf(“%d”,a);
or else
printf(“%d”,b);
(a>b) ? a : b ; // Prints value of a if condition is true else prints b
value

a=10;
What's the Output
(a!=0) ? False : True ;
Akshata Angadi, Assistant Professor, CSE Dept., GIT
Precedence of Operators

Akshata Angadi, Assistant Professor, CSE Dept., GIT


Category Operator Associativity
Postfx () [] -> . ++ -- Left to right
Unary + - ! ~ ++ -- (type)* & sizeof Right to left
Multiplicative */% Left to right
Additive +- Left to right
Shift << >> Left to right
Relational < <= > >= Left to right
Equality == != Left to right
Bitwise AND & Left to right
Bitwise XOR ^ Left to right
Bitwise OR | Left to right
Logical AND && Left to right
Logical OR || Left to right
Conditional ?: Right to left
Assignment = += -= *= /= %=>>= <<= &= ^= |= Right to left
Comma , Left to right
Akshata Angadi, Assistant Professor, CSE Dept., GIT
ASSIGN-2
• C Program to read 2 numbers and print the sum of 2.
• C Program to Calculate Area and Circumference of Circle
• C Program to Calculate Area of Triangle
• C Program to Subtract and Divide.
• Program to solve following expressions: (A=5, B=3, C=6, D=2, E=1, F=9, G=4)
A + B - C / D
 (A + B) * C - (D - E) * (F + G)
• Evaluate all Expressions: (Manually in ur book)
• q && r || s (q=20,r=0,s=66)
• 10-3%8+6/4
• 17-8/4*2+3- ++a (a=2)
• a-- + b++ - ++c (a=2,b=4,c=5)
• x = a – b / 3 + c * 2 – 1; (a=9,b=12,c=13)
• y = a – b / (3 + c) * (2 – 1);
• z = a – ( b / (3 + c) * 2) Akshata
– 1;Angadi, Assistant Professor, CSE Dept., GIT
ASSIGN-2
• Program to solve following expressions: (A=5, B=3, C=6, D=2, E=1, F=9, G=4)
 A + B - C / D Soln : 5
 (A + B) * C - (D - E) * (F + G) Soln : 35
• Evaluate all Expressions: (Manually in ur book)
• q && r || s (q=20,r=0,s=66)
Soln : 1
• 10 - 3%8 + 6/4
Soln : 8 // 3 % 8 = 3 , 6/4 = 1 , 3 + 1 = 4 , 10 – 3 + 1 = 7 + 1 = 8
• 17-8/4*2+3- ++a (a=2)
Soln : 13
• a-- + b++ - ++c + ++d (a=2,b=4,c=5,d=6)
Soln : 7
• x = a – b / 3 + c * 2 – 1; (a=9,b=12,c=13)
• y = a – b / (3 + c) * (2 – 1);
• z = a – ( b / (3 + c) * 2) –Akshata
1;
Angadi, Assistant Professor, CSE Dept., GIT
• Expression: a+ b-- + c++
value = 6
a=1b=1c=4
• Expression: c+ f-- + --a
value = 7
a=0c=3f=5
• Expression: f-- + c-- + d - ++e
value = 4
c=2d=4e=6f=5

Akshata Angadi, Assistant Professor, CSE Dept., GIT


Find the output:
#include<stdio.h> #include<stdio.h>
#include<conio.h> #include<conio.h>
void main() void main()
{ {
int x,i; int x,i;
i= 10; i= 10;
x= ++i; x= i++;
printf("x: %d", x); OUTPUT printf("x: %d", x); OUTPUT
printf("i: %d“, i); x: 11 printf("i: %d", i); x: 10
} i= 11 } i= 11

Akshata Angadi, Assistant Professor, CSE Dept., GIT


Find the output:
#include<stdio.h> #include<stdio.h>
#include<conio.h> #include<conio.h>
void main() void main()
{ {
int x; int x;
x= 10; x= 10;
x= ++x; x= x++;
printf("x: %d", x); printf("x: %d", x);
OUTPUT OUTPUT
} }
x: 11 x: 10

Akshata Angadi, Assistant Professor, CSE Dept., GIT


Mathematical functions
• All math functions are defned in header fle math.h just
like standard input output function is defned in stdio.h.
Math functions
Example: sqrt () , pow() , sin() , cos() , tan() etc..
When ever we use these functions in our program its
necessary to include math.h header fle.

Lets write a program to fnd square root of a number.

Akshata Angadi, Assistant Professor, CSE Dept., GIT


// Program to fnd square root of a number
#include<stdio.h>
#include<math.h> // using sqrt( ) so added this header fle
main( )
{
int num , sqroot; // Declaration stmt
printf(“Enter a positive number”); //Prints a statement
scanf(“%d”, &num) ; //Reads a num

sqroot = sqrt (num) ; /* sqrt( ) is math function that fnds


square root of num */

printf(“squareroot of a number = %d” , sqroot ); //Prints result


} Akshata Angadi, Assistant Professor, CSE Dept., GIT
// Program to fnd square of a number i.e. (num to the power of 2)
#include<stdio.h>
#include<math.h> // using pow( ) so added this header fle
main( )
{
int num , square; // Declaration stmt
printf(“Enter a positive number”); //Prints a statement
scanf(“%d”, &num) ; //Reads a num

square = pow (num , 2) ; /* pow( ) is math function that


fnds square of num */

printf(“squareroot of a number = %d” , square ); //Prints result


} Akshata Angadi, Assistant Professor, CSE Dept., GIT
Write C expressions for following:
1.

Soln : To solve the equaton , it should be writen in C program as :

n = (23 * x ) / (r – h + t )

Akshata Angadi, Assistant Professor, CSE Dept., GIT


Write C expressions for following:

2.

Soln : To solve the equaton , it should be writen in program as :

D = pow ( x , 3) - pow ( y , 5 ) + sqrt ( z * c )

Akshata Angadi, Assistant Professor, CSE Dept., GIT


Write C expressions for following:

3.

Soln : To solve the equaton , it should be writen in program as :

f = sin (z) + exp( x+y-10 ) + log (x/y)

Akshata Angadi, Assistant Professor, CSE Dept., GIT


Exercise Problems
• Write an C expression for the following 6 equatons:

Akshata Angadi, Assistant Professor, CSE Dept., GIT


• WAP to fnd largest of 2 numbers
• WAP to read 2 characters , 2 integers and 3 foating
point numbers.
• WAP , algorithm and fowchart to fnd whether a number
is divisible by 6 and divisible by 2 or not.
• Write a fowchart to solve a following expression:
x=a+b/d–c
• WAP to calculate tax for following cases.
If salary is equal to 40000 -> apply tax of 10% to salary
If salary is greater than 40000 -> apply tax of 40% to
salary
Finally print the Tax payable.
Akshata Angadi, Assistant Professor, CSE Dept., GIT

You might also like