1<html lang="en"> 2<head> 3<title>Include Operation - The C Preprocessor</title> 4<meta http-equiv="Content-Type" content="text/html"> 5<meta name="description" content="The C Preprocessor"> 6<meta name="generator" content="makeinfo 4.13"> 7<link title="Top" rel="start" href="index.html#Top"> 8<link rel="up" href="Header-Files.html#Header-Files" title="Header Files"> 9<link rel="prev" href="Include-Syntax.html#Include-Syntax" title="Include Syntax"> 10<link rel="next" href="Search-Path.html#Search-Path" title="Search Path"> 11<link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage"> 12<!-- 13Copyright (C) 1987, 1989, 1991, 1992, 1993, 1994, 1995, 1996, 141997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 152008, 2009, 2010, 2011 16Free Software Foundation, Inc. 17 18Permission is granted to copy, distribute and/or modify this document 19under the terms of the GNU Free Documentation License, Version 1.3 or 20any later version published by the Free Software Foundation. A copy of 21the license is included in the 22section entitled ``GNU Free Documentation License''. 23 24This manual contains no Invariant Sections. The Front-Cover Texts are 25(a) (see below), and the Back-Cover Texts are (b) (see below). 26 27(a) The FSF's Front-Cover Text is: 28 29 A GNU Manual 30 31(b) The FSF's Back-Cover Text is: 32 33 You have freedom to copy and modify this GNU Manual, like GNU 34 software. Copies published by the Free Software Foundation raise 35 funds for GNU development. 36--> 37<meta http-equiv="Content-Style-Type" content="text/css"> 38<style type="text/css"><!-- 39 pre.display { font-family:inherit } 40 pre.format { font-family:inherit } 41 pre.smalldisplay { font-family:inherit; font-size:smaller } 42 pre.smallformat { font-family:inherit; font-size:smaller } 43 pre.smallexample { font-size:smaller } 44 pre.smalllisp { font-size:smaller } 45 span.sc { font-variant:small-caps } 46 span.roman { font-family:serif; font-weight:normal; } 47 span.sansserif { font-family:sans-serif; font-weight:normal; } 48--></style> 49<link rel="stylesheet" type="text/css" href="../cs.css"> 50</head> 51<body> 52<div class="node"> 53<a name="Include-Operation"></a> 54<p> 55Next: <a rel="next" accesskey="n" href="Search-Path.html#Search-Path">Search Path</a>, 56Previous: <a rel="previous" accesskey="p" href="Include-Syntax.html#Include-Syntax">Include Syntax</a>, 57Up: <a rel="up" accesskey="u" href="Header-Files.html#Header-Files">Header Files</a> 58<hr> 59</div> 60 61<h3 class="section">2.2 Include Operation</h3> 62 63<p>The ‘<samp><span class="samp">#include</span></samp>’ directive works by directing the C preprocessor to 64scan the specified file as input before continuing with the rest of the 65current file. The output from the preprocessor contains the output 66already generated, followed by the output resulting from the included 67file, followed by the output that comes from the text after the 68‘<samp><span class="samp">#include</span></samp>’ directive. For example, if you have a header file 69<samp><span class="file">header.h</span></samp> as follows, 70 71<pre class="smallexample"> char *test (void); 72</pre> 73 <p class="noindent">and a main program called <samp><span class="file">program.c</span></samp> that uses the header file, 74like this, 75 76<pre class="smallexample"> int x; 77 #include "header.h" 78 79 int 80 main (void) 81 { 82 puts (test ()); 83 } 84</pre> 85 <p class="noindent">the compiler will see the same token stream as it would if 86<samp><span class="file">program.c</span></samp> read 87 88<pre class="smallexample"> int x; 89 char *test (void); 90 91 int 92 main (void) 93 { 94 puts (test ()); 95 } 96</pre> 97 <p>Included files are not limited to declarations and macro definitions; 98those are merely the typical uses. Any fragment of a C program can be 99included from another file. The include file could even contain the 100beginning of a statement that is concluded in the containing file, or 101the end of a statement that was started in the including file. However, 102an included file must consist of complete tokens. Comments and string 103literals which have not been closed by the end of an included file are 104invalid. For error recovery, they are considered to end at the end of 105the file. 106 107 <p>To avoid confusion, it is best if header files contain only complete 108syntactic units—function declarations or definitions, type 109declarations, etc. 110 111 <p>The line following the ‘<samp><span class="samp">#include</span></samp>’ directive is always treated as a 112separate line by the C preprocessor, even if the included file lacks a 113final newline. 114 115 </body></html> 116 117