Documente Academic
Documente Profesional
Documente Cultură
UNIT 6
OBJECTIVES
General Objective : To Understand the function of preprocessor directive and header file.
INPUT
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
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.
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
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
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:
1: #include <iostream>
5:
6: void main ( )
7: {
8: int x=5, y:
9: y = getmax (x,8);
10: cout<<y;
11: }
And the output of program will be the bigger value, in this case:
8
E3062/6/6
Activity 6A
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
Feedback To Activity 6A
6.2 C. #define
6.3 B. Value
INPUT
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
For example, to display the phrase “Learning C++ is Fun” to the screen, just type
The function scanf() reads formatted input from the keyboard. It has the form
int scanf(const char *format[ , …..])
E3062/6/9
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);
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
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.
Program 6.2.3
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
Program 6.2.4
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
Activity 6B
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
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
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
Feedback To Self-Assessment
Have you tried the question????? If “YES”, check your answer now:
Question 6.1
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;
}