Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 30

DEPARTMENT OF TECHNICAL EDUCATION

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

Duration : 100 Min


Teaching Aids : PPTs. CM304.37TO38 1
Recap

 What are input function?


 List the output functions.
 How to read a single character from the KB.
 How to print a single character?

CM304.37TO38 2
Objectives
On completion of this period, you would be able to
know

 Type Conversion Techniques


 Preprocessor Directives
 Macro Definitions

CM304.37TO38 3
Type conversion Techniques

 Type casting is used to convert the type of value


of an expression or variable to another data type.

 This type conversion is useful in situations, where


we declare some variable as an integer but
sometimes we may require result in floating point
number.

CM304.37TO38 4
Type conversion Techniques

In C, we can convert one type to another type


in 3 ways

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

When constants and variables of different types are


mixed in an expression, they are converted to the
same type.

This is done by the following type conversion rules:

 If a binary operator has operands of different types,


the compiler converts lower type to higher type

CM304.37TO38 8
Contd..
2. Type Conversion in Expression

 All char and short int are converted into int.


 All floats are converted to doubles.
 For all operand pairs, if one of the operand is
a long double, the other operand is converted
to long double.
 If one of the operand is double , other is
converted into double.

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

 Preprocessor is a program that process the source


code before it passes through the compiler.

 It operates under the control of preprocessor


command lines or Preprocessor directives.

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

 File inclusion Directives


#include

 Compiler control Directives


#if
#else

CM304.37TO38 16
Macro Substitution Directives (Macros)

 It is a process where an identifier in a program


is replaced by a predefined string.

 The preprocessor does this task under the


direction of #define statement.

 This statement is called a macro definition or


simply Macro.

CM304.37TO38 17
Macro Substitution Directives (Macros)

 The general form of Macro is


#define identifier string
 If this statement is included at the beginning,
then the preprocessor replaces every
occurrence of the identifier in the source
program by the string.

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

 Easy to read and write.


 Easy to check the string constants.
 Easy to debug the errors.
 Gives good look to the program.
 Easy to transfer from one machine to another
machine.

CM304.37TO38 20
Summary

In this class, you have learnt


 Type conversion techniques
 Macros
 In C we can convert one type to another in 3
ways
1. Conversion by assignment
2. Type conversion in an expression
3. Using type cast operator
CM304.37TO38 21
Quiz

1. Which of the following expressions are valid? If


valid, give the value of the expression

a) 25 / 3 % 2 b) 7.5 % 3

c) 21 % (int)4.5 d) (5/3) * 3 + 5 % 3

CM304.37TO38 22
Quiz

1. Which of the following expressions are valid? If


valid, give the value of the expression
a) 25 / 3 % 2 b) 7.5 % 3
c) 21 % (int)4.5 d) (5/3) * 3 + 5 % 3
a) Valid 0
b) Invalid
c) Valid 1
d) Valid 5
CM304.37TO38 23
Quiz

2. Find the value x


int x;
x = 5.5 / 5 * 7 + (int)25.2 % 4;

a) 8 b) 8.7

c) 6 d) 8.6

CM304.37TO38 24
Quiz

2. Find the value x


int x;
x = 5.5 / 5 * 7 + (int)25.2 % 4;

a) 8 b) 8.7

c) 6 d) 8.6

CM304.37TO38 25
Quiz

3. Find the value x


float x;
x = 5.5 / 5 * 7 + (int)25.2 % 4;

a) 8 b) 8.7

c) 6 d) 8.6

CM304.37TO38 26
Quiz

3. Find the value x


float x;
x = 5.5 / 5 * 7 + (int)25.2 % 4;

a) 8 b) 8.7

c) 6 d) 8.6

CM304.37TO38 27
Quiz

4. Identify the Macro

a) #include b) #define

c) #if d) #else

CM304.37TO38 28
Quiz

4. Identify the Macro

a) #include b) #define

c) #if d) #else

CM304.37TO38 29
Frequently Asked Questions

1. Explain various Type Conversion Techniques


with examples.

2. Explain Type casting.

3. Define Macro with example.

4. List the advantages of macros.

CM304.37TO38 30

You might also like