The Preprocessor : We may consider the C
preprocessor as a separate program that runs before the actual compiler. It is
automatically executed when we compile a program, so we need not explicitly
invoke it. The preprocessor has instructions for the compiler and these
instructions are known as preprocessor commands or directives. Each directive
begins with the character ‘#’ (pound sign or Hash). Although, these directives
are not part of the C language, but they expand the scope of the C programming
environment. Unlike C statements, a preprocessor directive ends with a newline,
not a semicolon. We shall discuss the preprocessor in detail in unit-V. Here,
we take a close look at the # include directive, already mentioned in
connection with header file, and a new preprocessor command called # define
directive. 28 Programming in C
(i)
# include directive : The preprocessor # include
directive instructs the compiler to read source text from another file as well
as the file it is currently compiling. This enables us to insert the contents
of one file into another file before compilation begins, although the original
file is not actually altered. The # include command has two forms : # include and
# include ′′filename′′ If the filename is surrounded by angle brackets, the
preprocessor looks in a special place designated by the operating system. This
is where all system include files, such as the header files for the runtime
library, are kept. If the filename is surrounded by double quotes, the
preprocessor looks in the directory containing the source file. If it can not
find the include file there, it searches for the file as if it had been
enclosed in angle brackets. By convention, the names of include files usually
end with .h extension. When we compile the program, the preprocessor replaces
the # include directive with the contents of the specified file
(ii)
(ii) # define directive : Just as it is possible to associate a name
with a memory location by declaring a variable, it is also possible to
associate a name with a constant. We do this by using a preprocessor directive
called # define. To avoid confusion between constants and variables, it is
common practice to use all uppercase letters for constant names and lowercase
letters for variable names. The general form of # define directive is # define
identifier string where identifier is the name used for constant and string is
a character string that is substituted for the identifier each time it is
encountered in the source file. The identifier is called a macroname and the
replacement process is called macrosubstitution. Naming constants has two
important benefits. First, it enables us to give descriptive name to a
non-descriptive number. For example, let us consider # define MAX_PAGE_WIDTH 70
In our program, we can use MAX_PAGE_WIDTH, which means something, instead of
‘70’ which does not tell us much. Creative naming of constants can make a
program much easier to read. The other advantage of constant names is that they
make a program easier to change. For example, the maximum page width parameter
might appear many times in a large text formatting program. Suppose we want to
change the maximum width from 70 to 80. If, instead of using a constant name,
we used the constant 70, we will need to change 70 to 80 wherever it appears
and hope that we are changing the right 70’s. If we use a constant name, we
need only change the definition as follows # define MAX_PAGE_WIDTH 80 and
recompile.
No comments:
Post a Comment