Saturday, May 10, 2008

C preprocessors And macro(Introduction,File inclusion )

C preprocessors And macro



Introduction



The C preprocessor is a tool which filters your source code before it is compiled.
The preprocessor allows constants to be named using the # notation.
The preprocessor provides several other facilities which
will be described here.
It is particularly useful for selecting machine dependent
pieces of code for different computer types, allowing a
single program to be compiled and run on several different computers.
The C preprocessor isn't restricted to use with C programs
only. Programmers who use other languages may also find
it useful, however it is tuned to recognize features
of the C language like comments and strings,
so its use may be restricted in other circumstances.
Set of commonly used Preprocessor Directives and their Functions:

#define defines a macro substitution.
#undef undefined a macro.
#include specifies the files to be include.
#ifdef tests for a macro definition
#ifndef tests whether a macro is not defined.
#if tests a compile-time condition.
#endif specifies the end of #if.
#else specifies alternatives when #if test fails.
These Preprocessor or directives can be divided into three categories:

1. Macro Substitution Directives
2. File Inclusion Directives
3. Compiler Control Directives




File inclusion



The preprocessor directive #include is an instruction
to read in the entire contents of another file at that point.

This is generally used to read in header files for
library functions. Header files contain details
of functions and types used within the library.

It must be included before the program can make
use of the library functions.

Library header file names are enclosed in angle brackets,
< >. These tell the preprocessor to look for the header
file in the standard location for library definitions.

Example: #include

another use for #include for the programmer is where
multi-file programs are being written.

Certain information is required at the beginning of each
program file. This can be put into a file called globals.h
and included in each program file.

Local header file names are usually enclosed by double
quotes, " ". It is conventional to give header files a name
which ends in .h to distinguish them from other types of file.

Our globals.h file would be included by the following
line. #include "globals.h"



0 comments:

Copyright

Copyright © 2008 C Tutorial, All rights are reserved