Search This Blog

Tuesday, June 5, 2012

File Inclusions , Conditional Compilation


File Inclusions 


This directive Causes one file to be included in another.

Eg. #Include “file name”
This causes the whole content of the above file to be inserted into the source code at that point in a program. It is used in two occasions.

a) When there is a very large program, the code in divided into several files. This feature also
enables the modularity in a program.
b) In a situation where some functions or some macros are needed in our programs. In this case
those files can simply be included.

#include <filename.c> in a specified directory
#include ”filename.c”
#include <filename.h> .h says it is a header file and go to head of a c program.


Conditional Compilation


By inserting some preprocessor directives we can skip over part of a source code.
e.g. #ifdef macro name
Statement 1;
Statement 2;

#endif

The block code will be processed as usual if the macro name has been defined. but not otherwise. #ifdef
is used when you want to compile only a part of your program.

It is also used to improve the portability of a program. Suppose you want to install a program in two
different types of computers without modifying the source code, you can isolate the lines of codes that
should be different for each machine using #ifdef and #endif

e.g. #ifdef PENTIUM
code suitable for Pentium
#else
code suitable for MAC
#endif
code suitable for both
e.g # if linux
# include <getopt.h>
#endif
#ifndef can also be used in the same way but behaves opposite to #ifdef

No comments:

Post a Comment