Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does #include actually do?

Tags:

c

include

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.

like image 812
Zirak Avatar asked Apr 20 '11 19:04

Zirak


People also ask

What the Fox say Meaning?

Speaking of the meaning of the song, Vegard characterizes it as coming from "a genuine wonder of what the fox says, because we didn't know". Although interpreted by some commentators as a reference to the furry fandom, the brothers have stated they did not know about its existence when producing "The Fox".

What is this song Google?

Ask Google Assistant to name a song On your phone, touch and hold the Home button or say "Hey Google." Ask "What's this song?" Play a song or hum, whistle, or sing the melody of a song. Hum, whistle, or sing: Google Assistant will identify potential matches for the song.

What does the fox say for real?

One of the most common fox vocalizations is a raspy bark. Scientists believe foxes use this barking sound to identify themselves and communicate with other foxes. Another eerie fox vocalization is a type of high-pitched howl that's almost like a scream.


2 Answers

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.

like image 121
Carl Norum Avatar answered Oct 16 '22 00:10

Carl Norum


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.

like image 6
pmg Avatar answered Oct 16 '22 01:10

pmg