Professional Documents
Culture Documents
CM304
CM304
ANDHRA PRADESH
Name : T.Sudhakar
Designation : Lecturer in Computer Engg.
Branch : CM
Institute : S.S.Govt. Polytechnic, Zaheerabad
Year/Semester : III Semester
Subject : UNIX & C
Sub Code : CM-304
Major Topic : Programming Constructs
Sub-Topic : Type Conversion Techniques and Macro Definitions
CM304.37TO38 2
Objectives
On completion of this period, you would be able to
know
CM304.37TO38 3
Type conversion Techniques
CM304.37TO38 4
Type conversion Techniques
4. Conversion by Assignment
5. Type conversion in Expression
6. Using Cast operator
CM304.37TO38 5
1. Conversion by Assignment
If the types of values at the right and left in the
assignment statement are different.
The type of value of the expression at the right is
automatically converted into the type of the
variable at left.
When the type at left is larger than right, the
process cause no problems.
When the type at left is smaller than the right,
data loss may occur.
CM304.37TO38 6
Example: Contd..
main( )
{
int x = 5;
float y = 2.5;
x = y;
printf(“ Value of x is %d”, x);
}
Output: Value of x is 2
Float value of y is assigned to x. The value of y is
truncated
CM304.37TO38 7
2. Type Conversion in Expression
CM304.37TO38 8
Contd..
2. Type Conversion in Expression
CM304.37TO38 9
Example:
main( )
{
short int a = 5;
int b = 2500;
float c = 250.13;
float d = 100.07;
float x;
x=a+b+c+d;
printf(“the value of x is %f”, x) ;
}
Output: the value of x is 2855.200
CM304.37TO38 10
3. Using Cast operator
The value of an expression can be converted
into another data type by using a cast operator.
The cast operator format is
(cast_data type) expression;
float x = 20.3;
y = (int)x % 3;
Here, (int)x % 3 forces the first operand to be an
integer resulting in remainder 2.
CM304.37TO38 11
Example:
main( )
{
int a = 100;
int b = 24;
printf(“%f\n”, (float)x/y) ;
printf(“%f\n”, (int)x/y) ;
}
Output:
4.16667
4
CM304.37TO38 12
Use of cast operator
Example Action
x is converted to float and division
y = (float)x/3
is done in real mode.
‘a’ is converted into integer and
c = (int)a + b;
added to b.
The result of 19.2/4 i.e., 4.8 is
r =(int)(19.2/4)
converted to integer value 4.
CM304.37TO38 13
Preprocessor
CM304.37TO38 14
Preprocessor
Preprocessor directives are placed in source
program before main() line.
Before the source code passes through the
compiler, it is examined by the preprocessor
for any preprocessor directives and
appropriate actions are taken.
Preprocessor directives begin with the symbol
#.
CM304.37TO38 15
Preprocessor directives are divided into
Macro substitution Directives
#define
#undef
CM304.37TO38 16
Macro Substitution Directives (Macros)
CM304.37TO38 17
Macro Substitution Directives (Macros)
CM304.37TO38 18
Examples
#define COUNT 100
#define PI 3.14
#define CAPITAL “DELHI”
#define TEST if(x>y)
#define PRINT printf(“Very good”);
TEST PRINT
The Preprocessor would translate this line to
if(x>y) printf(“Very good”);
CM304.37TO38 19
Advantages of using Macros
CM304.37TO38 20
Summary
a) 25 / 3 % 2 b) 7.5 % 3
c) 21 % (int)4.5 d) (5/3) * 3 + 5 % 3
CM304.37TO38 22
Quiz
a) 8 b) 8.7
c) 6 d) 8.6
CM304.37TO38 24
Quiz
a) 8 b) 8.7
c) 6 d) 8.6
CM304.37TO38 25
Quiz
a) 8 b) 8.7
c) 6 d) 8.6
CM304.37TO38 26
Quiz
a) 8 b) 8.7
c) 6 d) 8.6
CM304.37TO38 27
Quiz
a) #include b) #define
c) #if d) #else
CM304.37TO38 28
Quiz
a) #include b) #define
c) #if d) #else
CM304.37TO38 29
Frequently Asked Questions
CM304.37TO38 30