The header file eliminates the labor of finding and

Info icon This preview shows pages 11–14. Sign up to view the full content.

View Full Document Right Arrow Icon
ically use the new version when next recompiled. The header file eliminates the labor of finding and changing all the copies as well as the risk that a failure to find one copy will result in inconsistencies within a program. In C, the usual convention is to give header files names that end with ‘ .h ’. It is most portable to use only letters, digits, dashes, and underscores in header file names, and at most one dot.
Image of page 11

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Chapter 2: Header Files 8 2.1 Include Syntax Both user and system header files are included using the preprocessing directive ‘ #include ’. It has two variants: #include < file > This variant is used for system header files. It searches for a file named file in a standard list of system directories. You can prepend directories to this list with the ‘ -I ’ option (see Chapter 12 [Invocation], page 56 ). #include " file " This variant is used for header files of your own program. It searches for a file named file first in the directory containing the current file, then in the quote directories and then the same directories used for < file > . You can prepend directories to the list of quote directories with the ‘ -iquote ’ option. The argument of ‘ #include ’, whether delimited with quote marks or angle brackets, behaves like a string constant in that comments are not recognized, and macro names are not expanded. Thus, #include <x/*y> specifies inclusion of a system header file named x/*y ’. However, if backslashes occur within file , they are considered ordinary text characters, not escape characters. None of the character escape sequences appropriate to string con- stants in C are processed. Thus, #include "x\n\\y" specifies a filename containing three backslashes. (Some systems interpret ‘ \ ’ as a pathname separator. All of these also interpret / ’ the same way. It is most portable to use only ‘ / ’.) It is an error if there is anything (other than comments) on the line after the file name. 2.2 Include Operation The ‘ #include ’ directive works by directing the C preprocessor to scan the specified file as input before continuing with the rest of the current file. The output from the preprocessor contains the output already generated, followed by the output resulting from the included file, followed by the output that comes from the text after the ‘ #include ’ directive. For example, if you have a header file ‘ header.h ’ as follows, char *test (void); and a main program called ‘ program.c ’ that uses the header file, like this, int x; #include "header.h" int main (void) { puts (test ()); } the compiler will see the same token stream as it would if ‘ program.c ’ read int x; char *test (void); int main (void) {
Image of page 12
Chapter 2: Header Files 9 puts (test ()); } Included files are not limited to declarations and macro definitions; those are merely the typical uses. Any fragment of a C program can be included from another file. The include file could even contain the beginning of a statement that is concluded in the containing file, or the end of a statement that was started in the including file. However, an included file must consist of complete tokens. Comments and string literals which have not been closed
Image of page 13

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 14
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern