Including Header Files

Once you create your own header file, you include it by surrounding the filename with straight double quotation marks in the directive:

#include "filename.h"

If no pathname is indicated, as in the previous code, the compiler will search the current directory, as well as other logical directories for that environment, to find the file. To eliminate this guesswork, you can use relative pathnames when including your own header files. To state that the file is in the same directory as the including (or parent) file, use an initial period followed by a slash:

#include "./filename.h"

If the file is in a subdirectory, begin with that subdirectory's name:

#include "includes/filename.h"

Finally, if the included file is located in a directory above the current directory, use two periods and a slash:

#include "../common/filename.h"

In our next example, we will include and use the header file we created.

To include a header file

1.
Create a new text file in your IDE or text editor.

2.
Begin with your standard comments and the #include directive (Script 8.6):

/* include.c - Script 8.6 */
#include <stdio.h>

Script 8.6. Include your own header file as you would any system file, but use double quotation marks instead of the angle brackets.


3.
Include the my_math.h header file:

#include "./my_math.h"

This syntax of this include assumes that the my_math.h file is located within the same directory as this one. If you placed the header file in a different location, you'd need to change the pathname so that it accurately points to the header file.

4.
Define the main function:

int main (void) {

5.
Create the necessary variables and assign values to them:

float num1, num2;
num1 = 20.0;
num2 = 8.4;

Again, two number variables are declared and initialized with random values.

6.
Print the smaller of the two variables:

printf ("The smaller of %.1f and %.1f
 is %.1f.
", num1, num2,
 MIN(num1,num2));

This code mimics that in the smaller application. The C preprocessor will replace MIN(num1,num2) with the associated code since MIN is defined in my_math.h.

7.
Print the larger and then the average of the two numbers:

printf ("The larger of %.1f and %.1f
 is %.1f.
", num1, num2,
 MAX(num1,num2));

printf ("The average of %.1f and %.1f
 is %.1f.
", num1, num2,
 AVE(num1,num2));

Similar to the code in Step 6, the two number values are printed and then processed by the MAX and AVE macros.

8.
Complete the main function:

     getchar();
     return 0;
}

9.
Save the file as include.c, compile, and debug.

10.
Run the executable (Figure 8.12).

Figure 8.12. By including the my_math.h header file (Script 8.5), this application can run two numbers through three different mathematical macros.


11.
Change the value of the two numbers, recompile, and rerun the application (Figure 8.13).

Figure 8.13. The same application run again, working with different number values.


✓ Tips

  • If you want to include files that are located in one of the system include path subdirectories, you can do so with slashes separating the directories. For example, #include <sys/types.h> includes the file types.h inside the sys directory in one of the standard include file locations.

  • You should avoid absolute pathnames when including files because this makes your code less portable.

  • When you compile a file, you can direct the compiler to search for included files in additional locations as well. The exact syntax for doing so depends upon your compiler and environment, but, for example, you can often use the -I command line argument to gcc.


..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset