Sunteți pe pagina 1din 18

E3062/6/1

Preprocessor and Header Files

UNIT 6

Preprocessor and Header Files

OBJECTIVES

General Objective : To Understand the function of preprocessor directive and header file.

Specific Objectives : At the end of the unit you will be able to :

 define the preprocessor and header file.


 explain the function of preprocessor and header file.
 write the preprocessor statements using #include and #define.
 define and use header file stdio.h, ctype.h, math.h, conio.h and
iostream.h in C++ program.
E3062/6/2

Preprocessor and Header Files

INPUT

6.0 Introduction To Preprocessor and Header Files

Preprocessor directive and header file were


talked about right from the beginning unit of
this module. However, it is necessary to
cover in more details as to see the various
header file available for C++

Preprocessor directive are orders that we include within the code of our programs that
are not instructions for the program itself but for the preprocessor. The preprocessor is
executed automatically by the compiler when we compile a program in C++ and is the one in
charge to make first verification and digestions of the program code.

Header files are basically pre-written function that help programmer to write new
code faster. Since header files are all tested and free from bugs, hence the new programs
written can also be said as less error prone programs.
E3062/6/3

Preprocessor and Header Files

Definition:
a. Preprocessor
Preprocessor is the directive to enables the program to use certain function
contained in the external file such as iostream.h.
b. File Header
Header files contain specific functions that we can use to accomplish the
programming tasks. Example of header files are ctype.h and math.h.

6.1 Preprocessor Statement

Before we can use any of the functions contained in a header file, we must include the
header file in the program. We can do this by using the #include and # define preprocessor
directive placed before the function main ( ).

6.1.1 #include
When preprocessor find an #include directive it replaces it by the whole
content of specified file. There are two ways to specify a file to be included as in
example below:

# include “ file.h ”

or

# include <file.h>

The difference between both expressions in the example above, is in the directories in
which the compiler is going to look for the file. In the first case in that the file is
specified between quotes the file is looked for from the same directory in which the
file that includes the directive is, and only in case that is not there the compiler
E3062/6/4

Preprocessor and Header Files

looks for in the default directories where it is configured to look for the standard
header files.

In case that the file name is included enclosed between < > the file is directly
looked for in the default directories where the compiler is configured to look for
standard header files.

6.1.2 # define

The processor directive, #define, serves to generate what we call defined


constants or macros and whose form is the following example below.

#define name value

Its function is to define a macro called name that whatever it is found in some
point of the code is replaced by value . For example:

#define MAX_ WIDTH 100


Char str1[ MAX_WIDTH ] ;
Char str2 [ MAX_WIDTH ] ;
E3062/6/5

Preprocessor and Header Files

It defines two strings to score up to 100 characters. An example of macro


function using #define is shown in program below

1: #include <iostream>

2: using namespace std;

3. #define getmax(a,b) (a>b ? a : b)

5:

6: void main ( )

7: {

8: int x=5, y:

9: y = getmax (x,8);

10: cout<<y;

11: }

On line no. 7, after substitution , it will look like this:

Y= getmax (x>8 ? x:8);

And the output of program will be the bigger value, in this case:
8
E3062/6/6

Preprocessor and Header Files

Activity 6A

TEST YOUR UNDERSTANDING BEFORE YOU CONTINUE WITH THE


NEXT INPUT…!

6.1 Choose the most accurate statement regarding #define


A. It is a preprocessor directive.
B. Serves to generate defined constants or macros
C. It defines two strings to store up to 100 characters.
D. All the above

6.2 Which directive creates macros and symbolic constants?


A. #undef
B. #ifdef
C. #define
D. #include

6.3 The #define functions to define a macro called name where at some point of
the code is replaced by___________________
a. address
b. Value
c. String
d. Type

6.4 There are two ways to specify a file to be included, which are
___________ or ________________.
E3062/6/7

Preprocessor and Header Files

Feedback To Activity 6A

6.1 D. All the above

6.2 C. #define

6.3 B. Value

6.4. #include “file” , #include <file>.


E3062/6/8

Preprocessor and Header Files

INPUT

6.2 Header files ‘ .h’

Header files contain numerous frequently-used functions that programmers can use
without having to write codes for them. In addition, programmers can also write their own
declarations and functions and store them in header files which they can include in any C++
program. We will discuss the functions contained in a standard header files.

6.2.1 stdio.h
This header file is used for declaring standard I/O streams. Here are some
functions from this header file.
The function printf ( ) sends formatted output to the screen. It has the form

int printf(const char *format[ ,argument,….])

For example, to display the phrase “Learning C++ is Fun” to the screen, just type

Printf(“Learning C++ is Fun”)

The function scanf() reads formatted input from the keyboard. It has the form
int scanf(const char *format[ , …..])
E3062/6/9

Preprocessor and Header Files

Special conversion characters can be used to describe the data you want to print in
printf function or input in scanf function as shown in table 6.1.
For printf the statement
printf(“I am %d years old and I make $%f a month”, 18, 2200.50);

will produce the following output on the screen:

I am 18 years old and I make 2200.50 a month.

For scanf the statement, to read a person’s name,


Scanf(“%s”, name);

Table 6.1: printf() and scanf() conversion character


.Conversion character Output
%s String of character
%s Character
%s Decimal integer
%s Floating-point number
%s Unsigned integer
%s Hexadecimal integer
%s Percent sign (%)

(Ref. C++ Through Examples-PSellapan)

6.2.2 ctype.h
Table 6.2 gives a partial list of function contained in the character type
header file. These functions come in handy when you want to
determine whether a character data is alphanumeric, alphabetic, digital,
uppercase, lowercase and so on. They are often used inside an if or
while statement.
E3062/6/10

Preprocessor and Header Files

Table 6.2: List of functions in ctype.h


Function Return value
salnum (c) Non- zero if ( c ) is alphanumeric; 0 otherwise
isalpha (c) Non- zero if ( c ) is alphabetic; 0 otherwise
isascii (c) Non- zero if ( c ) is ASCII character; 0 otherwise
iscntrl (c) Non- zero if ( c ) is control character ; 0 otherwise
isdigit (c) Non- zero if ( c ) is a digit; 0 otherwise
isgraph(c) Non- zero if ( c ) is a graphic character(including space); 0
otherwise
islower (c) Non- zero if ( c ) is lowercase letter ; 0 otherwise
isprint (c) Non- zero if ( c ) is printable character ( including space ) ; 0
otherwise
ispunct(c) Non- zero if ( c ) is punctuation character ; 0 otherwise
isspace (c) Non- zero if ( c ) is a space, tab, formfeed or new line
character; 0 otherwise
isupper (c) Non- zero if ( c ) is uppercase letter; 0 otherwise
isxdigit (c) Non- zero if ( c ) is hexadecimal digit ; 0 otherwise
toascii(c) ASCII equavalent
tolower (c) Lowercase equivalent of c if it is latter; unchanged otherwise
toupper (c) Uppercase equivalent of c if it is latter; unchanged otherwise

(Ref. C++ Through Examples-PSellapan)


E3062/6/11

Preprocessor and Header Files

6.2.3 math.h
This header file contains several mathematical functions. Table 6.3 shows
functions contained in this file. The argument (d) for each of these functions is
of type double.

Table 6.3: List of functions in math.h


Function Return value
acos(d) Arc cosine of d
asin(d) Arc sine d
atan(d) Arc sine d
atan2(d1,d2) Arc tangent of d1/d2
ceil(d) Smallest integer greater than d
cos(d) Cosine of d
exp(d) e(i.e.,2.718…) raised to the power d
fabs(d) Absolute value of d
floor(d) Largest integer smaller than d
fmod(d1,d2) Remainder of d1/d2
log(d) Natural logarithm of (d>0)
log10(d) Base logarithm of d (d>0)
pow(d1,d2) D1 raised to the power of d2
sin(d) Sine of d
sinh(d) Hyperbolic sine of d
sqrt(d) Square root of d
tan(d) Tangent of d
tanh(d) Hyperbolic tangent of d

(Ref. C++ Through Examples-PSellapan)


E3062/6/12

Preprocessor and Header Files

To illustrate the use of some of the functions, consider program 6.2.3

Program 6.2.3

//program that uses mathematical functions


#include <math.h>
#include <iostream.h>
main( )
{
double d,d1,d2;
d=100.0; d1=100.5; d2=2.0;
cout<<”\nSquare root of “<<d<<” = “<<sqrt(d);
cout<<”\nlog (to base 10 of “<<d<<” = “<<log10(d);
cout<<”\n “<<d<<” raised to the power of “<<d2;
cout<<” = “<<pow(d,d2);
cout<<”\nThe ceiling value of “<<d1<<” = “<<ceil(d1);
return 0;
}

The program outputs the following:

Square root of 100 = 10


Log (to base 10) of 100 = 2
100 raised to the power of 2 = 10000
The ceiling value of 100.5 = 101
E3062/6/13

Preprocessor and Header Files

6.2.4 conio.h
This header file has several screen handling functions suchas the function
gotoxy( ) which position the cursor in the text window. It has the form

void gotoxy ( int x, int y )


An example of this function is given in program 6.2.4

Program 6.2.4

// Program that uses gotoxy ( ) function


#include < conio.h >
#include < stdio.h > //Header file containing function getch( )
main( )
{
clrscr ( ); // Clears the screen
gotoxy(35,12) // Place cursor at column 35 , row 12
printf ( ‘Hello world” );
return o;
}

The program will display “ Hello world” at column 35, row 12

6.2.5 iostream.h
The name iostream.h stand for input / output stream header . The basic input /
output ( I / O ) function contained in this header file are cin for console input
( keyboard) and Cout for console output ( the screen). The function cin is used
with redirection operator >> while the function cout is used with redirection
operator <<.
E3062/6/14

Preprocessor and Header Files

Activity 6B

TEST YOUR UNDERSTANDING BEFORE YOU CONTINUE WITH THE


NEXT INPUT…!

1. Which header file is required for the following functions:


a. sqrt( )
b. sin( )
c. ceil( )
d. isgraph( )
e. gets( )
f. scanf( )
g. islower( )
h. log10( )
i. atof( )
j. isspace( )
E3062/6/15

Preprocessor and Header Files

Feedback To Activity 6B

1
a. sqrt( ) – math.h
b. sin( ) - math.h
c. ceil( ) - math.h
d. isgraph( ) – ctype.h
e. gets( ) – stdio.h
f. scanf( ) – stdio.h
g. islower( ) – ctype.h
h. log10( ) – math.h
i. atof( ) - math.h
j. isspace( ) – ctype.h
E3062/6/16

Preprocessor and Header Files

KEY FACTS

1. Preprocessor is the directive to enables the program to use certain function contained
in the external file that we include within the code of our programs that are not
instructions for the program but for the preprocessor
2. Header files contain specific functions that we can use to accomplish the
programming tasks which are basically pre-written function that help programmer to
write new code faster.
E3062/6/17

Preprocessor and Header Files

SELF-ASSESSMENT

You are approaching success. Try all the questions in this self-assessment section
and check your answers with those given in the Feedback on Self-Assessment 2 given
on the next page. If you face any problems, discuss it with your lecturer. Good luck.

Question 6-1
Write the outputs for the following for the following functions
#include <math.h>
#include <iostream.h>
main ()
{
double d,d1,d2;
d=100.0; d1=100.5; d2=2.0;
cout<<”\nSquare root of “<<d<<” = “<<sqrt(d);
cout<<”\nLog (to base 10) “<<d<<” = “<<log10(d);
cout<<”\n“<<d<<” raised to the power of “<<d2;
cout<<” = “<<pow(d,d2);
return 0;
}

Question 6-2
Write program segments to output the following:
a. The square, square root, natural logarithm of the number 20.
b. The remainder of 11 divided by 6
E3062/6/18

Preprocessor and Header Files

Feedback To Self-Assessment

Have you tried the question????? If “YES”, check your answer now:

Question 6.1

Square root of 100 = 10


Log (to base 10) of 100 = 2
100 raised to the power of 2 = 10000

Question 6.2

#include <math.h>
#include <iostream.h>
main ()
{ CONGRATULATIONS!!!
cout<<”\nThe square of 20 is “<<pow(20,2); !…..May success be with
you always….
cout<<”\nThe square root of 20 is “<<sqrt(20,0);
cout<<”\nThe natural logarithm of 20 is “<<log(20,0);
return 0;
}

S-ar putea să vă placă și