You are on page 1of 10

Use of #include and #define

Introduction to computer programming and


Problem Solving- CSE 101

L Jeganathan
jeganathan.l@vit.ac.in

Lecture 04

September 22, 2010

Introduction to computer programming and Problem solving - CSE


Use of #include and #define

Lines that begin with # are called preprocessing directives.


Using # define, we can define the values of symbolic
constants
For example, #define MAXIMUMMARK 80.
Preprocessor first changes all the occurrences of the
identifier (what is an identifier?) to 80.
You can use this identifier in the code any number of times.
If there is a change in the maximum marks, say, maximum
mark becomes 100, it is enough if you change in the
#defineMAXIMUMMARK 100.
As a convention, capital letters are used for the identifiers
which will be changed by the preprocessor.

Introduction to computer programming and Problem solving - CSE


Use of #include and #define

C AN WE CREATE OUR OWN HEADER FILES ?

#include < stdio.h >


#define AREA 150
#define SQ_MILES_PER_SQ_KILOMETER 0.386102
#define SQ_FEET _PER_SQ_MILE (5280 5280)
#define SQ_INCH_PER_SQ_FOOT 144
#define ACRES_PER_SQ_MILES 640
T
his file is stored in vitcc.h

Introduction to computer programming and Problem solving - CSE


Use of #include and #define

The identifier AREA is assigned 150.


The floating constant 0.386102 is a conversion factor.
There are 5280 feet in a mile; You can understand the third
line.

Introduction to computer programming and Problem solving - CSE


Use of #include and #define

/ Measuring the area of VIT Chennai campus /


#includevitcc.h00
int main(void)
{
const int vitcc = AREA; / insq.kilometers /
double acres, sq_miles, sq_feet, sq_inches;
printf (\n VIT Chennai campus covers an area);
printf (of %d square kilometers.\n00 , vitcc);
sq_miles = SQ_MILES_PER_SQ_KILOMETER vitcc;
sq_feet = SQ_FEET _PER_SO_MILE sq_miles;
sq_inches = SQ_INCHES_PER_SQ_FOOT sq_feet;
acres = ACRES_PER_SQ_MILE sq_miles;
printf (In other units of measure this is : );
printf (%22.7e acres\n, acres);
printf (%22.7e square miles\n, sq_miles);
printf (%22.7e square feet\n, sq_feet);
printf (%22.7e square inches\n\n, sq_inches)
return 0;
} Introduction to computer programming and Problem solving - CSE
Use of #include and #define

#includevitcc.h00

This includevitcc.h00 line is a preprocessing directive. It causes


a copy of the file vitcc.h to be included when the program is
compiled.

Introduction to computer programming and Problem solving - CSE


Use of #include and #define

const int vitcc = AREA; / insq.kilometers /

When compiled , vitcc is assigned the value in the identifier


AREA and is declared as an integer. const informs that we
cannot change the value of vitcc. const is called a type qualifier.

Introduction to computer programming and Problem solving - CSE


Use of #include and #define

printf (%22.7e acres\n, acres);

This statement causes the line 5.7748528e + 05 acres to


be printed.
The number is written in scientific notation and is
interpreted to mean 5.7748528x105 .
Numbers written this way are said to be written in an
e-format.
The conversion specification %e causes the system to print
a floating expression in an e-format with default spacing.
A format of the form %m.ne, where m and n are positive
integers, causes the system to print a floating expression
in an e-format in m spaces total, with n digits to the right of
the decimal point.
This statement causes the line 5.7748528e + 05 acres to
be printed.
Introduction to computer programming and Problem solving - CSE
Use of #include and #define

O UTPUT OF THE PROGRAM

The VIT chennai campus covers an area of 150 square


kilometers. In other units of measure this is:
5.7748528e + 05 acres
9.0232074e + 02 square miles
2.5155256e + 10 square feet
3.6223572e + 12 square inches
Note: The above numbers in the output are dummy numbers.
You run the program and find the actual numbers.

Introduction to computer programming and Problem solving - CSE


Use of #include and #define

T
he above program takes the 150 sq. kilometers as the area of
the vit chennai campus and converts the into other units such
as sq.miles, sq.inches, sq.feet, acres.

C
an you rewrite the program such that the program that takes
150 acres as the area of the vit chennai campus and convert
into other units?

Introduction to computer programming and Problem solving - CSE

You might also like