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

(Romina) #1

7. Making Your Programs More Powerful with #include and


#define


In This Chapter


  • Including files

  • Placing #include directives

  • Defining constants

  • Building a header file and program


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


Every sample program you have written so far has used #include. This chapter finally takes the
secret out of that mysterious preprocessor directive.


Including Files


#include has two formats, which are almost identical:


#include <filename>

and


#include "filename"

Figure 7.1 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. The filename can be stated in either uppercase or lowercase letters, as
long as your operating system allows for either in filenames. For example, my Windows XP
implementation of Code::Blocks does not distinguish between uppercase and lowercase letters in
filenames, but UNIX does. If your file is named myFile.txt, you might be able to use any of the
following #include directives:


#include "MYFILE.TXT"
#include "myfile.txt"
#include "myFile.txt"

However, UNIX allows only this:


#include "myFile.txt"
Free download pdf