You are on page 1of 69

2

3
 An operator is a symbol which helps the user
to command the computer to do a certain
mathematical or logical manipulations.
 An operand is a data item on which an
operand acts.
 Example: length * Breadth
Operands: length, breadth
Operator :-“*‟

4
5
 Operators that act upon a single operand to
produce a new value.
 Most common unary operation is unary minus
 Unary operators:
 Increments ++(increase by one)
 Decrements –(decrease by one)

6
The operator can be written before the identifier as a prefix
(++a) or after the identifier as a suffix/postfix (a++)

7
“m” is incremented
before assigning it to
“t”

“m” is assigned to “t”


and then
incremented

8
C Operation Operator Example Explanation
Positive + a=+3 Assigns positive
value “3” to a

Negative - b=-4 Assigns the


negative value
of “-4” to b

Increment ++ i++ Equivalent to


i=i+1
Decrement -- i-- Equivalent
to i =i-1

9
#include<stdio.h>
main()
{
inti=1;
printf("i=%d\t",i);
printf(“\”prefix\” i=%d \t",++i);
printf("i=%d\t",i);
}
Output: i=1 i=2 i=2

10
#include<stdio.h>
main()
{
inti=1;
printf("i=%d\n",i);
printf(“\”postfix\”i=%d \n",i++);
printf("i=%d\n",i);
}
Output:
i=1
i=1
i=2
11
Assuming the following variable declaration:
IntA=5,count=5;

Prefix & postfix


++ or -- Equivalent A Count
Statement Statement
A=count++; A=count; 5 6
count=count+1
A=++count; count=count+1; 6 6
A=count;
A=count--; A=count; 5 4
count=count-1;
A=--count; count=count-1; 4 4
A=count;

12
• Can only use modulus (%) operation on integer
variables/integer division

13
14
#include<stdio.h>
main()
{
inta,b,c;
i. a=4, b=2, c= 0
printf("Value for a and b \n"); ii. a=5, b=2, c=1
iii. a=-4, b=2, c=0
scanf("%d %d",&a,&b); iv. a=4, b=-3, c=1
c=a%b; v. a=-5, b=2, c=-1

printf("the value of c is %d \n",c);


}

• If a=2 and b=5, c=?


• If a=3 and b=5, c=?

15
#include<stdio.h>
main()
{
inta=5,b=2, c;
c= a/b;
printf("value of c is %d",c);
}
Output:
c=2

16
17
#include<stdio.h>
main()
{
float a=5,b=2;
float c;
c= a/b;
printf("the value of c is %f",c);
}
Output:
c= 2.500

18
If we want exact result we can make one of the operand
float type.

19
#include<stdio.h>
main()
{
inta=5,b=2;
float c;
c= a/b;
printf("the value of c is %f",c);
}
Output:
Is it exact result ?
c= 2.000

20
If we want exact result we can make one of the operand
float type.

#include<stdio.h>
main()
{
inta=5 ;
float b=2, c;
c= a/b;
printf(“ the value of c is %f", c);
}
Output:
c = 2.500000

21
#include<stdio.h>
main()
{
inta=5, b=2;
float c;
c= (float)a/b;
printf(“ the value of c is %f", c);
}
Output:
c = 2.500000

22
 Type casting is a means of temporarily treating one
data type as if it were another
 The operands of a binary operator must have the
same type and the result is also of the same type.
 Integer division:
c = (9 / 5)*(f -32)
 The operands of the division are both “int” and
hence the result also would beint. For correct results,
one may write
c = (9.0 / 5.0)*(f -32)
23
 In case the two operands of a binary operator are
different, but compatible, then they are converted to
the same type by the compiler . The mechanism (set
of rules) is called Automatic Type Casting.
c = (9.0 / 5)*(f -32)
 It is possible to force a conversion of an operand.
This is called Explicit Type casting.
c = ((float) 9 / 5)*(f -32)

24
Example:
#include<stdio.h>
main( )
{
inta=7 , rem;
float b=8.5;
rem= (a+b)%4;
printf("rem: %d",rem);
}
Output:
Error

25
Example:
#include<stdio.h>
main()
{
inta=7, rem ;
float b=8.5;
rem= ((int) (a+b))%4;
//converted to different data type
printf("\n rem: %d",rem);
}
Output:
Rem=3

26
Example:
#include<stdio.h>
main()
{
inta;
float b=8.5;
a= (int)b;
printf("value of a is %d and value of b is %d",a,b);
}
Output:
a=8
b=0

27
Example:
#include<stdio.h>
main()
{
inta;
float b=8.5;
a= (int)b;
printf("value of a is %d and value of b is %f",a,b);
}
Output:
a=8
b = 8.50000

28
Write a program to convert centigrade
to Fahrenheit. [F = 9/5 * C + 32]

29
30
#include<stdio.h>
main()
{
inta=9;
printf("value of a+=4 = %d,value of a-=4 =%d,
value of a*=4 =%d, value of a/=4 =%d", a+=4
,a-=2,a*=3,a/=3);
}

31
C=9

32
Operations with a higher precedence are carried
out before operations having a lower precedence

33
Precedence Rules:
 Precedence rules come into play when there is a
mixed of arithmetic operators in one statement.
 Precedence defines the priority of an operator while
associativityindicates which variable(s) an operator
is associated with or applied to.
 In the same expression, if two operators of the
same precedence are found, they are evaluated
from left to right, except for increment and
decrement operators which are evaluated from
right to left.

34
35
36
a=2, b=4

37
a=2, b=4

Is this Correct ?

38
#include<stdio.h>
main()
{
float a=1, b=2, c=3, d=4, e ;
e = a-b/c*d;
printf("value of e: %f",e);
}
Output:
e= -1.666667

39
#include<stdio.h>
main()
{
float a=1, b=2, c=3, d=4, e ;
e = (a-b)/(c*d);
printf("value of e: %f",e);
}
Output:
e= -0.083333

40
operator returns the size of its operand, in bytes
#include<stdio.h>
main()
{
inta;
float b;
char c;
printf("Size of integer: %d\n",sizeofa);
printf("Size of floating value: %d \n",sizeofb);
printf("Size of character: %d\n",sizeofc);
}
Output: integer: 2 , float: 4 and char: 1

41
#include<stdio.h>
main()
{
inta;
float b;
char c;
printf("Size of integer: %d\n",sizeof(int));
printf("Size of floating value: %d \n",sizeof(float));
printf("Size of character: %d\n",sizeof(char));
}
Output: integer: 2 , float: 4 and char: 1

42
• Assignment operator (=) and equality operator(==) are entirely different.
• Assignment operator is used for assigning values while equality operator is used
to compare two expression.

43
44
45
46
47
48
49
50
51
#include<stdio.h>
main()
{
intmin, a,b;
printf("Value of a and b");
scanf("%d%d",&a,&b);
min=((a<b)?a:b);
printf("Minimum value is %d",min);
}
Input Output
a = 5, b = 10 min = 5
a =15, b = 3 min = 3

52
Accept any two numbers, if the first number is greater
than second then print the sum of these two numbers,
otherwise print their difference. Write this program
using ternary operator.

53
54
#include<stdio.h>
main()
{
inta,b, c;
printf("Give value for a and b\n");
scanf("%d%d",&a,&b);
c=a&b;
printf("Value of c:%d",c);

55
56
57
#include<stdio.h>
main()
{
inta,b, c;
printf("Give value for a and b\n");
scanf("%d%d",&a,&b);
c=a|b;
printf("Value of c:%d",c);

58
#include<stdio.h>
main()
{
inta,b, c;
printf("Give value for a and
b\n");
scanf("%d%d",&a,&b);
c=a^b;
printf("Value of c:%d",c);
}

59
60
<< is a binary operator
• add zeros to the right bit, a << b returns
a*2b
c=a<<3
• a is shifted to the left by three bit positions
a = 13
13 0000 1101
0110 1000
• a is shifted to the left by three bit positions
Shift by three i.e. 2*2*2=8
13 * 8 = 104

61
#include<stdio.h>
main()
{
inta,b,c;
printf("Give value for a and c\n");
scanf("%d%d",&a,&c);
b=a<<c;
printf("Value of b:%d",b);
}
Input Output
a=13, c=3 b=104
a=10, c=2 b=40

62
>> is a binary operator
• takes away bit on the right, a >> b returns
a/2b
c=a>>2
• a is shifted to the left by three bit positions
a= 13
0000 1101
0000 0011
i.e. 13/4 = 3 (3.25)

63
#include<stdio.h>
main()
{
inta,b;
printf("Give value for a\n");
scanf("%d",&a);
b=~a;
printf("Value of b:%d",b);
}

64
Decimal value: 9
Binary Value: 0000 0000 0000 1001
1‟s complement : 1111 1111 1111 0110
2‟s complement : 1111 1111 1111 0111

65
 Carry out various commonly used operations
and calculations.
 Library funtions: tolower(c), toupper(c)
sqrt(d), pow(d1,d2),printf(), scanf(),toascii(c)

66
67
68
#include <stdio.h>
float pi = 3.14;
intmain()
{
float rad;
printf( “Enter the radius “ );
scanf(“%f” , &rad);
if ( rad> 0.0 )
{
float area = pi * rad * rad;
float peri= 2 * pi * rad;
printf( “Area = %f\n” , area );
printf( “Peri= %f\n” , peri);
}
else
{
printf( “Negative radius\n”);
printf( “Area = %f\n” , area );
}
return 0;
} 69
#include <stdio.h>
float pi = 3.14;
intmain(){
float rad;
printf( “Enter the radius “ );
scanf(“%f” , &rad);
if ( rad> 0.0 )
{
float area = pi * rad* rad;
float peri= 2 * pi * rad;
printf( “Area = %f\n” , area );
printf( “Peri= %f\n” , peri);
}
else
{
printf( “Negative radius\n”);
printf( “Area = %f\n” , area );
}
retrun0;
} 70

You might also like