0% found this document useful (0 votes)
16 views17 pages

2022-23 r20 I-I Cse PPSC Unit I Part 4 Notes

Uploaded by

Supriya Priya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views17 pages

2022-23 r20 I-I Cse PPSC Unit I Part 4 Notes

Uploaded by

Supriya Priya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Operators in C

An expression is a sequence of operands and operators that reduces to a single value.


Expressions can be simple or complex. An operator is a syntactical token(symbol) that
requires an action be taken. An operand is an object on which an operation is performed; it
receives an operator’s action.

Primary are- directly names or literals (values) or expressions in paranthesis


Postfix Expressions – operator after operand

Prefix Expressions – operator before operand

C language supports a lot of operators to be used in expressions.


Based on number of operands used:
Based on number of operands used the operators are categorized into the following
Unary Operators: Uses only one Operand
Binary Operators: Uses two Operands
Ternary Operator: Uses three operands and only one operator under this category- conditional
operator

Based on type of operation, these operators can be categorized into the following groups:

S.no Types of Operators Description


1 Arithmetic operators These are used to perform mathematical
calculations like addition, subtraction,
multiplication, division and modulus

2 Relational operators These operators are used to compare the value of


two variables.
3 Logical operators These operators are used to perform logical
operations on the given two variables.

4 Conditional (ternary) operator Conditional operator checks whether given condition


is true or false and executes corresponding
statements respectively.
5 Increment/decrement operators These operators are used to either increase or
decrease the value of the variable by one.
6 Bit wise operators These operators are used to perform operations on
individual bits rather than on the whole variables.
7 Assignment operators These are used to assign the values for the variables
in C programs.
8 Special operators &, *, sizeof( ) ,‘,’ etc operators.
Arithmetic operators:
 These are used to perform mathematical operations.
 These are unary as well as binary operators.
 Unary operate on single operand , example:+x,-y
 Binary operate on two operands at a time.
 They can be applied to any integers, floating-point number or characters.
 C supports 5 arithmetic operators. They are +, -, *, /, %.

The operators used for performing these arithmetic operations are as follows

Operator Meaning Example Example


+ Addition (results sum) A+B 2+9=11
- Subtraction (results difference) A-B 9-2=7
* Multiplication (results product) A*B 2*9=18
/ Division (results quotient) A/B 9/3=3
% Modulo division (results remainder) A%B 9%2=1

The modulo division gives the remainder of an integer division.


The modulo division cannot be used with real operands.
If the numerator is less than denominator then the remainder is the numerator itself.
The sign of the remainder is the sign of the numerator itself.

A B A%B
7 4 3
-7 4 -3
7 -4 3
-7 -4 -3

The following table shows how the division operator operates on various data types.
Operation Result Example
int/int (integer division) int 2/5=0
real/int (mixed division) real 5.0/2=2.5
int/real (mixed division) real 5/2.0=2.5
real/real (real division) real 5.0/2.0=2.5
In the above table first example the integer division truncates the fractional part.
Integer arithmetic: It requires both operands are integer values for arithmetic operation
Ex: a=5,b=4
Expression result
a+b 9
a-b 1
Floating point arithmetic(real arthimetic): It requires both operands are float type for
arithmetic operation.
Ex: a=6.5,b=3.5
Expression result
a+b 10.0
a-b 3.0
Mixed arithmetic: One of the operands are real and the other one is integer. The integer
is also upgraded to real and the result will become a real arithmetic.
An Example „C‟ program that illustrates the usage of arithmetic operators.

#include<stdio.h>
int main()
{
int a=9,b=3;
printf("%d+%d=%d\n",a,b,a+b);
printf("%d-%d=%d\n",a,b,a-b);
printf("%d*%d=%d\n",a,b,a*b);
printf("%d/%d=%d\n",a,b,a/b);
printf("%d%%%d=%d\n",a,b,a%b);
return 0;
}
Output
9+3=12
9-3=6
9*3=27
9/3=3
9%3=0

Relational operators

 A relational operator, also known as a comparison operator, is an operator that compares two
operands.
 The operands can be variables, constants or expressions.
 Relational operators always return either true or false depending on whether the conditional
relationship between the two operands holds or not.
 Zero is considered as false and non-zero value is considered as true.
 When you try print a true and false value,(i.e result of relational operation) it will
printed as 1 for true and 0 for false.
Syntax: op1 relational operator op2
op1,op2 are variables or constants or expressions
Ex: 9>2 this is true so ‘1’ is returned
C has six relational operators. The following table shows these operators along with their
meanings
Operator Example Description Example Result
> x>y x is greater than y 5>5 0( false)
< x<y x is less than y 5<5 0
>= x >= y x is greater than or equal to y 5>=5 1(true)
<= x <= y x is less than or equal to y 5<=5 1
== x == y x is equal to y 5==5 1
!= x != y x is not equal to y 5!=5 0
An example program that illustrates the use of arithmetic operators.
#include<stdio.h>
int main()
{
int a=9,b=3;
printf("%d>%d=%d\n",a,b,a>b);
printf("%d>=%d=%d\n",a,b,a>=b);
printf("%d<%d=%d\n",a,b,a<b);
printf("%d<=%d=%d\n",a,b,a<=b);
printf("%d==%d=%d\n",a,b,a==b);
printf("%d!=%d=%d\n",a,b,a!=b);
return 0;
}

Output
9>3=1
9 >= 3 = 1
9<3=0
9 <= 3 = 0
9= =3 = 0
9 != 3 = 1

Logical operators

Operators which are used to combine two or more relational expressions are known as
logical operators. C language supports three logical operators
Operator meaning
&& logical AND
|| logical OR
! logical NOT
Logical&& and logical || are binary operators whereas logical ! is an unary operator.

All of these operators when applied to expressions yield either integer value 0 (false) or an integer
value 1(true).

&&: This operator return true i.e,’1’ when both the conditions are true otherwise it
returns ‘0’.
||: This operator returns true if at least one of the conditions combined is true
!: This operator reverses the value of the expression it operates on
The working of these operations are illustrated for possible input values through the
below tables
Truth tables for logical AND
While performing the logical AND, if the first part of the expression(here A) is resulting
false, then the result is false and the remaining part of the expression is not processed.
Truth tables for logical OR
While performing the logical OR, if the first part of the expression(here A) is resulting true,
then the result is true and the remaining part of the expression is not processed.
Truth tables for logical OR

Example: (3<7)&&(7<5)
 (true)1&& (false)0
 0(false)
 result is false
Example: program to demonstrate use of logical operators
#include<stdio.h>
#include<conio.h>
void main()
{
int c1,c2,c3;
clrscr();
printf(“enter values of c1,c2,c3\n”);
scanf(“%d%d%d”,&c1,&c2,&c3);
printf(“\n(c1<c2)&&(c2<c3) is %d”,(c1<c2)&&(c2<c3));
printf(“\n(c1<c2)||(c2<c3) is %d” ,(c1<c2)||(c2<c3));
printf(“\n!(c1<c2) is %d”,!(c1<c2));
getch();
}
Output:
enter values of c1,c2 c3
315
(c1<c2)&&(c2<c3) is 0
(c1<c2)||(c2<c3) is 1
!(c1<c2) is 1

Logical AND
It is used to simultaneously evaluate two conditions or expressions with relational
operators. If expressions on the both sides (left and right side) of logical operators is true then
the whole expression is true otherwise false. The truth table of logical AND operator is given
below:

A B A && B
True True True
True False False
False True False
False False False
Logical OR
It is used to simultaneously evaluate two conditions or expressions with relational
operators. If one or both the expressions on the left side and right side of logical operators is
true then the whole expression is true otherwise false. The truth table of logical OR operator
is given below:
A B A || B
True True True
True False True
False True True
False False False

Logical NOT
It takes single expression and negates the value of expression. The truth table of logical NOT
operator is given below

A !A
True False
False True

For example: x and y are two variables. The following equations explain the use of logical
operators.
z1 = x&&y …1
z2 = x||y …2
z3=!x….3

Equation1 indicates that z1 is true if both x and y is true. Equation2 indicates z2 is true when
x or y or both true. Equation3 indicates z3 is true when x is not true.

An example „C‟ program that illustrates the use of logical


operators
#include<stdio.h>
int main()
{

int z1,z2,z3;
z1=7>5 && 10>15;
z2=7>5 || 10>15;
z3=!(7>5);
printf(" z1=%d\n z2=%d\n z3=%d
\n",z1,z2,z3); return 0;
}
Output z1=0 z2=1 z3=0

IMPORTANT PROGRAM – THIS CAN BE LEARNT AS EXAMPLE FOR


RELATIONAL, LOGICAL AND CONDITIONAL OPERATORS
CHECK whether given year is leap year or not

Leap year is an year which is if it is not century year, it divisible by 4 and if it is century, it is divisible
by 400
Increment (++) and decrement (− −) operators
The increment operator is unary operator that increases value of its operand by 1.
Similarly, the decrement operator decreases value of its operand by 1. These operators are
unique in that they work only on variables not on constants. These are used in loops like
while, do-while and for statements.
There are two ways to use increment or decrement operators in expressions. If you put
the operator in front of the operand (prefix), it returns the new value of the operand
(incremented or decremented). If you put the operator after the operand (postfix), it returns
the original value of the operand (before the increment or decrement).
Syntax:
Increment operator:
Pre increment: ++var_name ;
Post increment: var_name++;
Decrement operator:
Pre decrement: -- var_name ;
Post decrement: var_name – -;

Operation Type Operator Description


Pre increment ++x Value of i is incremented before assigning it to variable i.
Post increment x++ Value of i is incremented after assigning it to variable i.
Pre decrement --x Value of i is decremented before assigning it to variable i.
Post decrement x-- Value of i is decremented after assigning it to variable i.
x++; //post increment ,first do the operation and then increment
Example:
a=x++;
1.a=x (value of expression is x before increment)
2.x=x+1(value of x is incremented by 1)
++x; //pre increment ,first increment and then do the the operation
Example:
a=++x;
1.x=x+1(value of x is incremented by 1)
2.a=x(value of expression is x after increment)
--x; //pre decrement,first decrements and then do the operation
Example:
a=--x;
1.x=x-1(value of x is decremented by 1)
2.a=x(value of expression is x after decrement)
x--; //post decrement,first do the operation and then decrement
Example:
a=x--;
1.a=x(value of expression is x before decrement)
2:x=x-1(value of x is decremented by 1)
Note:We cannot increment or decrement constant and expressions.
5++; // this is invalid

An example program that illustrates the use of unary operators


#include<stdio.h>
int main()
{
int x=5;
printf("x=%d\n", x++);
printf("x=%d\n",++x);
printf("x=%d\n",x--);
printf("x=%d\n",--x);
printf("x=%d\n",-x);
return 0;
}
Output
x=5
x=7
x=7
x=5
x=-5

Bitwise operators
As the name suggests, the bitwise operators operate on bits. These operations include:
1. bitwise AND(&)
2. bitwise OR(|)
3. bitwise X-OR(^)
4. bitwise NOT(~)
5. shift left(<<)
6. shift right(>>)

Bitwise AND (&)


When we use the bitwise AND operator, the bit in the first operand is ANDed with
the corresponding bit in the second operand. If both bits are 1, the corresponding bit in the
result is 1 and 0 otherwise. For example:
In a C program, the & operator is used as follows.
int a=4,b=2,c;
c=a&b;
printf(“%d”,c); //prints 0

100 (binary equivalent of decimal 4)


&010 (binary equivalent of decimal 2)
000
Bitwise OR (|)
When we use the bitwise OR operator, the bit in the first operand is ORed with the
corresponding bit in the second operand. If one or both bits are 1, the corresponding bit in the
result is 1 and 0 otherwise. For example:
In a C program, the | operator is used as follows.
int a=4,b=2,c;
c=a|b;
printf(“%d”,c); //prints 6

100 (binary equivalent of decimal 4)


|0 1 0 (binary equivalent of decimal 2)
110

Bitwise XOR (^)


When we use the bitwise XOR operator, the bit in the first operand is XORed with the
corresponding bit in the second operand. If both bits are different, the corresponding bit in the
result is 1 and 0 otherwise. The

For example:
In a C program, the | operator is used as follows.
int a=4,b=2,c;
c=a^b;
printf(“%d”,c); //prints 6

100 (binary equivalent of decimal 4)


^010 (binary equivalent of decimal 2)
110

Bitwise NOT (~)


One‟s complement operator (Bitwise NOT) is used to convert each 1 to 0 and 0 to 1,
in the given binary pattern. It is a unary operator i.e. it takes only one operand.

For example, In a C program, the ~ operator is used as follows.


int a=4,c;
c=~a;
printf(“%d”,c); //prints -5

~100 (binary equivalent of decimal 4)


101

A B A&B A|B A^B ~A

1 1 1 1 0 0
1 0 0 1 1 0
0 1 0 1 1 1
0 0 0 0 0 1

Shift operators
C supports two bitwise shift operators. They are shift-left (<<) and shift-right (>>).
These operations are simple and are responsible for shifting bits either to left or to the right.
The syntax for shift operation can be given as:

operand operator num


where the bits in operand are shifted left or right depending on the operator (<<, >>) by the
number of places denoted by num.
Left shift operator
When we apply left-shift, every bit in x is shifted to left by one place. So, the MSB
(Most Significant Bit) of x is lost, the LSB (Least Significant Bit) of x is set to 0.

Let us consider int x=4;


Now shifting the bits towards left for 1 time, will give the following result
MSB LSB
3 2 1 0
2 2 2 2
8 4 2 1
x=4 0 1 0 0
1 0 0
x<<1 = 1 0 0 0 the result of x<<1 is 8

Left-shift is equals to multiplication by 2.

Right shift operator


When we apply right-shift, every bit in x is shifted to right by one place. So, the LSB
(Least Significant Bit) of x is lost, the MSB (Most Significant Bit) of x is set to 0. Let us
consider int x=4;
Now shifting the bits towards right for 1 time, will give the following result.
3 2 1 0
2 2 2 2
8 4 2 1
x=4 0 1 0 0
0 1 0
x>>1 = 0 0 1 0 the result of x>>1 is 2

Right-shift is equals to division by 2.

An example „C‟ program that illustrates the use of bitwise operators


#include <stdio.h>
int main()
{
int a=4,b=2;
printf("%d | %d = %d\n",a,b,a|b);
printf("%d & %d = %d\n",a,b,a&b);
printf("%d ^ %d = %d\n",a,b,a^b);
printf("~%d = %d\n",a,~a);
printf("%d<<1 = %d\n",a,a<<1);
printf("%d>>1 = %d\n",a,a>>1);
return 0;
}
Output:
4 |2 = 6
4&2=0
4^2=6
~4 = -5
4 << 1 = 8
4 >> 1 = 2
Assignment operators
Assignment operator (=)
In C, the assignment operator (=) is responsible for assigning values to variables.
When equal sign is encountered in in an expression, the compiler processes the statement on
the right side of the sign and assigns the result to variable on the left side. For example,

int x;
x=10;
Assigns the value 10 to variable x. if we have,
int x=2,y=3,sum=0;
sum = x + y;
then sum=5.
The assignment has right-to-left associativity, so the
expression int a=b=c=10;
is evaluated as
(a=(b=(c=10)))
Consider the following set of examples

a=5; // value of variable “a” becomes 5


a = 5+10; // value of variable „a‟ becomes 15
a = 5 + b; // value of variable „a‟ becomes 5 + value of b
a = b + c; // value of variable „a‟ becomes value of b + value of c
a=5+10; // value of variable „a‟ becomes 15
a = 5 + b; // value of variable „a‟ becomes 5 + value of b
a = b + c; // value of variable „a‟ becomes value of b + value of c

Short hand/Compound assignment operators


The assignment operator can be combined with the major arithmetic operators such
operators are called compound assignment operators. C language supports a set of
compound assignment operators of the form:
variable op = expression;
where op is binary arithmetic operator.

The following table lists the assignment operators supported by the C:


Op Description Example
Add AND assignment operator. It adds the right C += A is equivalent to
+= operand to the left operand and assign the result to C = C + A
the left operand.
Subtract AND assignment operator. It subtracts the C −= A is equivalent to
−= right operand from the left operand and assigns the C = C − A
result to the left operand.
Multiply AND assignment operator. It multiplies C *= A is equivalent to
*= the right operand with the left operand and assigns C = C * A
the result to the left operand.
Divide AND assignment operator. It divides the C /= A is equivalent to
/= left operand with the right operand and assigns the C = C / A
result to the left operand.
Modulus AND assignment operator. It takes C %= A is equivalent to
%= modulus using two operands and assigns the result C = C % A
to the left operand.
<<= Left shift AND assignment operator. C <<= 2 is same as C = C << 2
>>= Right shift AND assignment operator. C >>= 2 is same as C = C >> 2
&= Bitwise AND assignment operator. C &= 2 is same as C = C & 2
^= Bitwise exclusive OR and assignment operator. C ^= 2 is same as C = C ^ 2
|= Bitwise inclusive OR and assignment operator. C |= 2 is same as C = C | 2
An example „C‟ program that illustrates the use of assignment operators

#include <stdio.h>
int main()
{
int a = 21,c;
c = a;
printf("Operator is = and c = %d\n", c );
c += a;
printf("Operator is += and c=%d\n",c);
c −= a;
printf("Operator is −= and c=%d\n",c);
c *= a;
printf("Operator is *= and c=%d\n",c);
c /= a;
printf("Operator is /= and c=%d\n", c);
c = 200;
c %= a;
printf("Operator is %= and c=%d\n",c);
c <<= 2;
printf("Operator is <<= and c=%d\n",c);
c >>= 2;
printf("Operator is >>= and c=%d\n",c);
c &= 2;
printf("Operator is &= and c = %d\n", c );
c ^= 2;
printf("Operator is ^= and c = %d\n", c );
c |= 2;
printf("Operator is |= and c = %d\n", c );
return 0;
}

Output
Operator is = and c = 21
Operator is += and c = 42
Operator is −= and c = 21
Operator is *= and c = 441
Operator is /= and c = 21
Operator is %= and c = 11
Operator is <<= and c = 44
Operator is >>= and c = 11
Operator is &= and c = 2
Operator is ^= and c = 0
Operator is |= and c = 2
Advantages:
1. Short hand expressions are easier to write.
2. The statement involving short hand operators are easier to read as they are more
concise.
3. The statement involving short hand operators are more efficient and easy to
understand.
Conditional operator (? : )
It is also called ternary operator because it takes three operands. It has the general form:
variable = expression1 ? expression2 : expression3;
If the expression1 is evaluated totrue then it evaluates expression2 and its value is
assigned to variable, otherwise it evaluates expression3 and its value is assigned to variable.

It is an alternative to simple if..else statement


For example
#include<stdio.h>
int main()
{
int a=5,b=6,big;
big = a>b ? a : b;
printf("%d is big\n", big);
return 0;
}
Output
6 is big

Special operators: Comma operator (,)


This operator allows the evaluation of multiple expressions, separated by the comma,
from left to right in the order and the evaluated value of the rightmost expression is accepted
as the final result. The general form of an expression using a comma operator is
expressionM= ( expression1, expression2, expressionN);
example:
int a=2, b=3, x=0;
x = (++a, b+=a);
Now, the value of x = 6.
sizeof Operator
The operator sizeof is a unary operator used calculates the size of data types and
variables. The operator returns the size of the variable, data type in bytes. i.e. the sizeof
operator is used to determine the amount of memory space that the variable/data type will
take. The outcome is totally machine-dependent. For example:

#include<stdio.h>
int main()
{
printf("char occupies %d bytes\n",sizeof(char));
printf("int occupies %d bytes\n",sizeof(int));
printf("float occupies %d bytes\n",sizeof(float));
printf("double occupies %d bytes\n",sizeof(double));
printf("long double occupies %d bytes\n",sizeof(long double));
return 0;

}
Output
char occupies 1 bytes
int occupies 2 bytes
float occupies 4 bytes
double occupies 8 bytes
long double occupies 10 bytes
Other special operators , are
‘&’ to get address of variable
‘*’ to get the value stored at an address
‘.’ to access members of structure variable, period or member operator
‘→’ to access members of structure pointer
‘(type)’ to convert one type of data to another type
Expressions
In C programming, an expression is any legal combination of operators and operands
that evaluated to produce a value.Every expression consists of at least one operand and can
have one or more operators. Operands are either variables or values, whereas operators are
symbols that represent particular actions.
In the expression x + 5; x and 5 are operands, and + is an operator.

In C programming, there are mainly two types of expressions are available. They are as
follows:
1. Simple expression
2. Complex expression

Simple expression: In which contains one operator and two operands or constants.
Example: x+y; 3+5; a*b; x-y etc.
Complex expression: In which contains two or more operators and operands or constants.
Example: x+y-z; a+b-c*d; 2+5-3*4; x=6-4+5*2 etc.

Operators provided mainly two types of properties. They are as follows:


1. Precedence and
2. Associativity
Operator Precedence
It definesthe order in which operators in an expression are evaluated depends on
their
st relative precedence. Example: Let us see x=2+2*2
1 pass- 2+2*2
2 pass- 2+4
rd
3 pass- 6 that is x=6.
Associativity defines the order in which operators with the same order of precedence are
evaluated. Let us see x=2/2*2
st
1 pass-- 2/2*2
2 pass-- 1*2
rd
3 2that is x=2
An example „C‟ program that illustrates the use of expressions
#include<stdio.h>
int main()
{
int a, b=4,c=8,d=2,e=4,f=2;
a=b+c/d+e*f; // expression1
printf(" The value of a is%d\n", a);
a=(b+c)/d+e*f; // expression2
printf("The value of a is%d\n", a);
a=b+c/((d+e)*f); // expression3
printf("The value of a is%d\n", a);
return 0;
}
Output:
The value of a is 16
The value of a is 14
The value of a is 6
The below table lists C operators in order of precedence (highest to lowest) and their
associativity indicates in what order operators of equal precedence in an expression are
applied.

Operator Description Associativity

() Parentheses (function call) left-to-right


[] Brackets (array subscript)
. Member selection via object name
-> Member selection via pointer
++ -- Prefix increment/decrement Postfix increment/decrement right-to-left
+- Unary plus/minus
! ~ Logical negation bitwise complement
(type) Cast (convert value to temporary value of type)
*& Dereference, Address (of operand)
Sizeof Determine size in bytes on this implementation
* / % Multiplication division modulus left-to-right

+ - Addition subtraction left-to-right

<< >> Bitwise shift left, Bitwise shift right left-to-right

< <= > >= Relational less than/ less than or equal to Relational left-to-right
greater than/ greater than or equal to
== != Relational is equal to /is not equal to left-to-right

& Bitwise AND left-to-right


^ Bitwise exclusive OR left-to-right
| Bitwise inclusive OR left-to-right
&& Logical AND left-to-right
|| Logical OR left-to-right
?: Ternary conditional right-to-left
= += -= *= /= All Assignment right-to-left
%= &= ^= |=
<<= >>=
, Comma (separate expressions) left-to-right

Type Conversion
Type casting: Type casting is a way to convert a variable from one data type to another
data type. It can be of two types:
1. Implicit type casting
2. Explicit type casting
Implicit type casting/type conversion
When the type conversion is performed automatically by the compiler without
programmer‟s intervention, such type of conversion is known as implicit type conversion or
type promotion. In this, all the lower data types are converted to its next higher data type.

If the both operands are of the same type, promotion is not needed. If they are not,
promotion follows these rules:

float operands are converted to double.

char or short are converted to int.
If anyone operand is double, the other operand is also converted to double, and that is the type of result.
or

If anyone operand is long, the other operand is also converted to long, and that is the type of
result.

If anyone operand is unsigned, the other operand is also converted to unsigned, and
that is the type of result.
The smallest to the largest data types with respect to size are given as follows:

For example:

Fig. Conversion of types in a mixed extresssion

A Sample ‘C’ program that illustares the use implicit type


conversion #include<stdio.h>
int main()
{
int sum, num=17;
char ch='A';
sum=num+ch;
printf("The value of sum=%d\n", sum);
return 0;
}
Output:
The value of sum= 82 i.e. sum=num+ch=>17+65 (ASCII value of „A‟
Explicit typecasting/casting: Which is intentionally performed by the programmer for his
requirement in a C program?The explicit type conversion is also known as type casting.We
can convert the values from one type to another explicitly using the cast operator as follows:

Syntax
(data_type) expression;

Where, data_type is any valid C data type, and expression may be constant, variable or
expression.

The following rules have to be followed while converting the expression from one type to
another to avoid the loss of information:
1. All integer types to be converted to float.
2. All float types to be converted to double.
3. All character types to be converted to integer.

Let us look at some examples of type casting.

 res = ( int ) 9.5;


9.5 is converted to 9 by truncation then assigned to res.
 res = ( int ) 12.3 / ( int ) 4.2 ;
It evaluated as 12/4 and the value 3 is assigned to res.
 res = ( double ) total / n;
total is converted to double and then division is done in float point mode.
 res = ( int )(a + b);
The value of (a + b) is converted to integer and then assigned to res.
 res = ( int )a + b;
a is converted to int and then added with b.

You might also like