C Tutorial (7) : Preprocessor directives & Header files

Two types of lines you see in many C programs are not C commands at all. They are preprocessor directives. A preprocessor directive always begins with a pound sign (#). Preprocessor directives don’t cause anything to happen at runtime (when you run your program). Instead, they work during the compiling of your program.

These preprocessor directives are used most often:

  • #include
  • #define

Including Files

#include has two formats, which are almost identical:

#include <filename>
#include "filename"

Figure shows what #include does. It’s nothing more than a file merge command. Right before your program is compiled, the #include statement is replaced with the contents of the filename specified after #include.


#include inserts a disk file into the middle of another file.


When you install your compiler, the installation program sets up a separate location on your disk (in a directory) for various #include files that come with your compiler. When you want to use one of these built-in #include files, use the #include format with the angled brackets, < and >.

The #include file for printf() is stdio.h (which stands for standard I/O), and the #include file for the strcpy() function is string.h.

Note : 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.

Placing #include Directives

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().

Defining Constants

The #define preprocessor directive defines constants. A C constant is really the same thing as a literal. A literal is a data value that doesn’t change, like the number 4 or the string “C programming”. The #define preprocessor directive lets you give names to literals. When you give a name to a literal, the named literal is known in C terminology as a named constant or a defined constant.

Note : Constants that you define with #define are not variables, even though they sometimes look like variables when they are used.

Here is the format of the #define directive:

#define CONSTANT constantDefinition

#define AGELIMIT 21
#define MYNAME "Anukul Verma" 
#define PI 3.14159

In a nutshell, here’s what #define tells C: Every place in the program that the CONSTANT appears, replace it with the constantDefinition. The first #define just shown instructs C to find every occurrence of the word AGELIMIT and replace it with a 21. Therefore, if this statement appeared somewhere in the program after the #define:

if (employeeAge < AGELIMIT)

the compiler acts as if you typed this:

if (employeeAge < 21)

even though you didn’t.

Tip : Use uppercase letters for the defined constant name. This is the one exception in C when uppercase is not only used, but recommended. Because defined constants are not variables, the uppercase lets you glance through a program and tell at a glance what is a variable and what is a constant.

Assuming that you have previously defined the constant PI, the uppercase letters help keep you from doing something like this in the middle of the program:

PI = 544.34;               /* Not allowed */

As long as you keep defined constant names in upper case, you will know not to change them because they are constants.

Defined constants are good for naming values that might need to be changed between program runs. For example, if you didn’t use a defined constant for AGELIMIT, but instead used an actual age limit value such as 21 throughout a program, if that age limit changed, finding and changing every single 21 would be difficult. If you had used a defined constant at the top of the program and the age limit changed, you’d only need to change the #define statement to something like this:

#define AGELIMIT 18

PI is easier to remember (and helps eliminate typing mistakes) and is clearer to the purpose of the constant.

The scope of named constant is across functions. You can define it any where is whole program before using it.

If you define some constant more then once, though compilation will give warning at runtime it overwrites the previous definitions.

When you type a header file and then save it, you need to add the .h to the end of the file to make it clear to your compiler that it is a header file, not a program.

Strings < Prev                                                                                                     Next > Scanf



  1. I am no longer certain the place you’re getting your info, however great topic. I must spend some time learning much more or understanding more. Thanks for magnificent information I used to be in search of this information for my mission.


  2. you’re really a just right webmaster. The web site loading speed is amazing. It sort of feels that you’re doing any distinctive trick. Also, The contents are masterwork. you have performed a excellent activity in this subject!


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s