C Programming Absolute Beginner's Guide (3rd Edition)

(Romina) #1

Almost every complete program listing in this book contains the following preprocessor directive:


#include <stdio.h>

That’s because almost every program in this book uses printf(). Chapter 6, “Adding Words to
Your Program,” told you that whenever you use the strcpy() function, you need to include
string.h.


Tip

The file you include is called a header file. That’s why most included files end in the
extension .h.

When you write your own header files, use the second form of the preprocessor directive, the one that
has quotation marks. When you use quotation marks, C first searches the disk directory in which your
program is stored and then searches the built-in #include directory. Because of the search order,
you can write your own header files and give them the same name as those built into C, and yours will
be used instead of C’s.


Warning

If you write your own header files, don’t put them with C’s built-in #include file
directory. Leave C’s supplied header files intact. There is rarely a reason to override
C’s headers, but you might want to add some headers of your own.

You might write your own header files when you have program statements that you frequently use in
many programs. Instead of typing them in every program, you can put them in a file in your program
directory and use the #include directive with the file where you want to use the statements.


Placing #include Directives


The header files you add to your programs with #include are nothing more than text files that
contain C code. You will learn much more about the contents of header files later; for now,
understand that a header file does two things. The built-in header files help C properly execute built-
in functions. The header files you write often contain code that you want to place in more than one
file.


Tip

It’s best to put your #include directives before main().
Free download pdf