Preprocessor Directives
The C language explicitly defines only a small number of
operations: Many actions that are necessary in a computer program are not
defined directly by C. Instead, every C implementation contains collections of
useful functions and symbols called libraries . The ANSI
(American National Standards Institute) standard for C requires that certain standard libraries be provided in every ANSI C implementation. A C system may expand
the number of operations available by supplying additional libraries; an
individual programmer can also create libraries of functions. Each library has
a standard header file whose name ends with the symbols .h.
/*
* Converts distances from miles to kilometers.
*/
#include /* printf, scanf definitions */
#define KMS_PER_MILE 1.609 /* conversion constant */
int
main(void)
{
double miles, /* distance in miles
kms; /* equivalent distance in kilometers */
/* Get the distance in miles. */
printf("Enter the distance in miles> ");
scanf("%lf", &miles);
/* Convert the distance to kilometers. */
kms = KMS_PER_MILE * miles;
/* Display the distance in kilometers. */
printf("That equals %f kilometers.\n", kms);
return (0);
}
The #include directive gives a
program access to a library. This directive causes the preprocessor to insert
definitions from a standard header file into a program before compilation.
The
directive
#include
/* printf, scanf definitions */
notifies the
preprocessor that some names used in the program (such as scanf and
printf
) are found in the standard header file .
The other
preprocessor directive in Fig. 2.1
#define
KMS_PER_MILE 1.609 /* conversion constant */
associates
the constant macro KMS_PER_MILE with the meaning 1.609 . This directive instructs the
preprocessor to replace each occurrence of KMS_PER_MILE
in the text of the C program by 1.609
before compilation begins. As a result, the line kms = KMS_PER_MILE * miles; would
read kms = 1.609 * miles; by
the time it was sent to the C compiler. Only data values that never change (or
change
very rarely) should be given names using a #define
, because an executing C program cannot change the value of a
name defined as a constant macro. Using the constant macro KMS_PER_MILE in the text of a
program for the value 1.609 makes
it easier to understand and maintain the program. The text on the right of each
preprocessor directive, starting with /*
and ending with */ ,
is a comment .
Comments provide supplementary information making it easier for us to
understand the program, but comments are ignored by the C preprocessor and
compiler.
Posted by 08.40 and have
0
comments
, Published at