TIL: #include literally copy-paste the content of it

May 01, 2026

I mean I already knew about this point already but I was still thinking that there will be some magic in-between. It can't be that simple - copy-paste! But, the I watched this video by antirez.

---

Create a file with name print.c and write the following code - yes, just a printf() statement.

printf("hello, world\n");

Create another file with name hello.c and write the following code.

#include <stdio.h>

int main(void) {
    #include "print.c"

    return 0;
}

Now, compile and run the hello.c program (without including print.c in compilation).

gcc hello.c && ./a.out 
hello, world

It prints the output - hello, world as #include directive copy-paste the content of print.c file.

If you need proof then use -E compilation option. It'll print the preprocessed output. Let's also pass -P option to pretty-print the output.

gcc -E –P hello.c

The output is big. So, I truncated to only last few lines.

// ...
// ...
extern void flockfile (FILE *__stream) __attribute__ ((__nothrow__ , __leaf__)) __attribute__ ((__nonnull__ (1)));
extern int ftrylockfile (FILE *__stream) __attribute__ ((__nothrow__ , __leaf__)) __attribute__ ((__nonnull__ (1)));
extern void funlockfile (FILE *__stream) __attribute__ ((__nothrow__ , __leaf__)) __attribute__ ((__nonnull__ (1)));
extern int __uflow (FILE *);
extern int __overflow (FILE *, int);

int main(void) {
printf("hello, world\n");
 return 0;
}

There it is!