32

In C (or a language based on C), one can happily use this statement:

#include "hello.h";

And voila, every function and variable in hello.h is automagically usable.

But what does it actually do? I looked through compiler docs and tutorials and spent some time searching online, but the only impression I could form about the magical #include command is that it "copy pastes" the contents of hello.h instead of that line. There's gotta be more than that.

Wayne
  • 59,728
  • 15
  • 131
  • 126
Zirak
  • 38,920
  • 13
  • 81
  • 92
  • 2
    If it's any consolation, the preprocessor may also insert implementation-specific annotations so that if/when debug info is generated, it gets the line numbers right. If it literally had the effect of a copy-paste, the debugger wouldn't know the "real" source file and line number. Also `__FILE__` and `__LINE__` have to be substituted as if before the copy-paste. As far as the meaning of the program is concerned, though, copy-paste is it. – Steve Jessop Apr 20 '11 at 19:24

6 Answers6

37

Logically, that copy/paste is exactly what happens. I'm afraid there isn't any more to it. You don't need the ;, though.

Your specific example is covered by the spec, section 6.10.2 Source file inclusion, paragraph 3:

A preprocessing directive of the form

# include "q-char-sequence" new-line

causes the replacement of that directive by the entire contents of the source file identified by the specified sequence between the " delimiters.

Carl Norum
  • 219,201
  • 40
  • 422
  • 469
6

That (copy/paste) is exactly what #include "header.h" does.

Note that it will be different for #include <header.h> or when the compiler can't find the file "header.h" and it tries to #include <header.h> instead.

pmg
  • 106,608
  • 13
  • 126
  • 198
  • 1
    "Different" only in that it usually searches in different places and is mentally associated with (standard) library headers. –  Apr 20 '11 at 19:13
  • @delnan, they don't have to be different at all - all of the searches are performed in an "implementation-defined" manner, according to the spec. The difference is that the `""` form falls back to the `<>` form if it doesn't work. @pmg is totally correct. – Carl Norum Apr 20 '11 at 19:15
  • Also "different" because there need not be a file named `"thing.h"` when you do `#include `. – pmg Apr 20 '11 at 19:17
0

The # include statement "grabs the attention" of the pre-processor (the process that occurs before your program is actually compiled) and "tells" the pre-processor to include whatever follows the # include statement.

While the pre-processor can be told to do quite a bit, in this instance it's being asked to recognize a header file (which is denoted with a .h following the name of that header, indicating that it's a header).

Now, a header is a file containing C declarations and definitions of functions not explicitly defined in your code. What does this mean? Well, if you want to use a function or define a special type of variable, and you know that these functions/definition are defined elsewhere (say, the standard library), you can just include (# include) the header that you know contains what you need. Otherwise, every time you wanted to use a print function (like in your case), you'd have to recreate the print function.

If its not explicitly defined in your code and you don't #include the header file with the function you're using, your compiler will complain saying something like: "Hey! I don't see where this function is defined, so I don't know what to with this undefined function in your code!".

Olivia Stork
  • 4,660
  • 5
  • 27
  • 40
Sidney
  • 1
  • 1
0

Not really, no. The compiler saves the original file descriptor on a stack and opens the #included file; when it reaches the end of that file, it closes it and pops back to the original file descriptor. That way, it can nest #included files almost arbitrarily.

geekosaur
  • 59,309
  • 11
  • 123
  • 114
  • 1
    compiler pass in gcc for example knows nothing about #include, because it's preprocessor pass. – fazo Apr 20 '11 at 19:36
  • @fazo: In most cases, the distinction there is meaningless. If you need pedantry for some reason, I can go dig up an incomplete list of compilers and whether they use preprocessors or not. – geekosaur Apr 20 '11 at 19:38
-1

This is a nice link to answer this question.

http://msdn.microsoft.com/en-us/library/36k2cdd4.aspx

Usually #include and #include "path-name" just differs in the order of the search of the pre processor

sdwaraki
  • 382
  • 1
  • 4
  • 12
-1

It's part of the preprocessor. Have a look at http://en.wikipedia.org/wiki/C_preprocessor#Including_files. And yes, it's just copy and paste.

Achim
  • 15,415
  • 15
  • 80
  • 144