Pages

Sunday, December 19, 2010

Macros - File Inclusion

The most common use of a macro is in including "external" files in our program. We use this kind of macro in almost every program when we include a header file. This directive simply "adds" an existing file to another file. The "include file" directive offers the following benefits.

1. If there are certain functions that we use quite frequently, for example in almost every program, creating them again and again would not make sense. What we can do is, store all such functions in a file and "include" them in every program they are required. You can say that this is like creating your own header file. Try it, put some commonly used functions in a new file, like adding 2 numbers, calculating square etc. Save this file in the "include" folder of your C installation,with any name of your choice(don't put spaces in between), but type the extension as ".h",for example "sample.h". Create another simple program file, and include the header file you just created using the include directive. Of course, the program will have the standard header files(stdio,conio) included also. Inside main(), try to call a function that you declared in "sample.h". You will see that the program will call the function from your header file, just like it does for predefined header files.

Example
// SAMPLE.H (SAVE IT IN THE "INCLUDE" FOLDER)
#include
int square(int n)
{
    return n*n;
}

// INCLUDING THE HEADER FILE
#include // pre defined header file
#include // user defined header file
void main()
{
    int num=5,r;
    r=square(num); // calling the function of sample.h
    printf("\nSquare is %d",r);
}


2. This technique can also make a large program small. If you have a large program which you are finding difficult to manage, you can break it into small manageable units. These units can then be "included" in the main program from where they will be called. In other words, if there is file handling program in which we have created separate functions to add,search,view, delete the records, we can save all the functions in a different files and call them from a main file.

For example
#include
#include
#include



How to use the "include" directive.

There are two methods of using the include directive:
  • Option 1 - #include
This statement will include the file "stdio.h" in the current file. This statement in fact is loading the "stdio.h" file in RAM so that the function given in it can be accessed.This command will search for the file stdio.h in the specified list of directories only. This "list of directories" is specified in different compilers differently. If you are using "Turbo C", you can specify the path by selecting "Options-> Directories" menu. When you will select "Directories" menu item, a dialog box titled "Directories" will be displayed. The first box in this dialog box will let you specify the directories in which a function is to be searched. The default path written here will be "c:\tc\include". This means that when you write "#include" in a program, the file is included from the "c:\tc\include" folder. If you want to specify more than 1 directory, you can write something like "c:\tc\include;c:\tc\myheader;c:\tc\headers;"
We can also specify multiple include paths separated by ‘;’ (semicolon) as shown above. Now any file that you include in the program will be first searched in "c:\tc\include", if found the file will be added, if not the same will be searched in the second directory "c:\tc\myheader", and the process will continue till the file is found. If the specified file is not found in any of the directories an error will be shown.
  • Option 2 - #include "stdio.h"
This command would look for the file "stdio.h" in the current directory as well as the specified list of directories as mentioned in the include search path that might have been set up.

0 comments:

Post a Comment