|| JAI SRI GURUDEV ||
SRI ADICHUNCHANAGIRI SHIKSHANA TRUST ®
ADICHUNCHANAGIRI INSTITUTE OF TECHNOLOGY
(An ISO 9001 : 2008 Certified)
Affiliated to Visvesvaraya Technological University, Belagavi ,
Approved by AICTE and Accredited by NAAC, New Delhi
Jyothinagar, Chikkamagaluru – 577 102.
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
ONLINE CLASS USING ZOOM APPLICATION
Subject: ‘C’ Programming for Problem Solving
Subject Code: 18CPS23
Presented By: Semester and Section : 2nd and ‘B’
Mr. GOPINATH C. B., B.E., M.Tech., Class # : 2
Assistant
7 April 2020 Professor Date : 03-04-2020 (11:00AM To 12:00PM)
Type Conversion
When variables and constants of different types are
combined in an expression then they are converted onto
same data type .
For Example:
int a, float b, double c;
c = a + b;
Definition of Type Conversion:
The process of converting an expression of a given data
type (one data type) to another data type is called Type
Conversion.
Type Conversion (CONTD….)
There are TWO types of Type Conversions
Implicit Type Conversion
Explicit Type Conversion
Implicit Type Conversion
When the two or more operands in an expression are
different, C automatically converts one data type to
another data type. This process is called Implicit Type
Conversion.
That is, Implicit Type Conversion is performed
automatically by compiler without programmers
intervention.
Implicit Type Conversion (CONTD….)
Conversion Hierarchy:
Note that, C uses the rule that, in all expressions except
assignments, any implicit type conversions are made from
a lower size type to a higher size type as shown below:
Implicit Type Conversion (CONTD….)
Given below is the sequence of rules that are applied
while evaluating expressions.
All short and char are automatically converted to int.
If one of the operands is long double, the other will
be converted to long double and the result will be
long double.
Else, if one of the operands is double, the other will be
converted to double and the result will be double.
Else, if one of the operands is float , the other will be
converted to float and the result will be float.
Implicit Type Conversion (CONTD….)
Else, if one of the operands is unsigned long int,
the other will be converted to unsinged long int
and the result will be unsigned long int.
Else, if one of the operands is long int, the other will
be converted to long int and the result will be long
int.
Else, if one of the operands is unsigned int, the
other will be converted to unsinged int and the
result will be unsigned int.
Implicit Type Conversion (CONTD….)
Note: During evaluation it adheres to very strict rules of
type conversion. If the operands are of different
types, the lower type is automatically converted
to the higher type before the operation proceeds.
The result is of the higher type.
Implicit Type Conversion (CONTD….)
A typical type conversion process is illustrated in below example
int i,x; float f; double d; long int l;
x = l / i + i * f - d
long int float
long int float
float
float double
double
int double
Implicit Type Conversion (CONTD….)
Example Program 1: Because here „b‟ is
#include<stdio.h> character type, „a‟ is
integer type so C language
void main()
provides implicit type
{
conversion of „b‟ (char
char b = „A‟; type) into integer
int a; type.
a = b;
ASCII value of A is 65, so it
printf(“%d”,a);
displays 65
}
Output : 65
Implicit Type Conversion (CONTD….)
Example Program 2:
#include<stdio.h>
void main()
{
double d = 123.456;
int a;
a = d;
printf(“%d”,a);
}
Output : 123
Explicit Type Conversion
The explicit type conversion performed by the
programmer by posting the data type of the expression
of specific type.
That is, when the two operands in an expression are
different, user explicitly changes the data type, this is
known as Explicit Type Conversion. This is also known
as Type Casting.
It is done using the following syntax
(dat_type) expression
Explicit Type Conversion (CONTD….)
Example Program :
#include<stdio.h> This #include<stdio.h>
program
void main() can be void main()
changed as
{ {
int a = 4; Here „a‟ is int a = 4;
float b; explicitly float b;
changed from
b = 1/a; b = 1/(float)a;
int data
printf(“%d”,b); printf(“%d”,b);
type to
} float data }
type
Output : 0.000000 Output : 0.250000
Explicit Type Conversion (CONTD….)
Example Action
7.5 is converted to integer by
x = (int)7.5
truncation
Evaluated as 21/4 and the result
a = (int)21.3/(int)4.5
would be 5
Division is done in floating
b = (double)sum/n
point mode
The result of a+b is converted to
y = (int)(a+b)
integer
„a‟ is converted to integer and
z = (int)a+b
then added to „b‟
Converts „x‟ to double before
p = cos((double)x)
using it
Explicit Type Conversion (CONTD….)
Example:
Consider the following statement:
x = (int)(y+0.5); if y = 27.6
What is the value of x?
The value of x = 28
y+0.5 is 28.1 and on casting, the result becomes 28.
THANK YOU