Learn more about Russian war crimes in Ukraine.

What is C include?

Take this C program:

#include <stdio.h>
int main() {
  fprintf(stdout, "Blimey! Error: %d\n", 42);
  return 0;
}

It’s tempting to see #include as importing some functionality to use in your program - in this case, #include <stdio.h> as importing fprintf and stdout. But this is not what #include does here. Instead, #include <stdio.h> just pulls in some text which declares the existence of fprintf. You can run clang -E to run the C preprocessor on this file and spit out the included text. Stripping out the unused included text, we get the following program, which will compile and behave the same:

typedef long long __int64_t;
typedef __int64_t __darwin_off_t;
struct __sFILEX;
struct __sbuf {
 unsigned char *_base;
 int _size;
};
typedef __darwin_off_t fpos_t;
typedef struct __sFILE {
 unsigned char *_p;
 int _r;
 int _w;
 short _flags;
 short _file;
 struct __sbuf _bf;
 int _lbfsize;
 void *_cookie;
 int (* _Nullable _close)(void *);
 int (* _Nullable _read) (void *, char *, int);
 fpos_t (* _Nullable _seek) (void *, fpos_t, int);
 int (* _Nullable _write)(void *, const char *, int);
 struct __sbuf _ub;
 struct __sFILEX *_extra;
 int _ur;
 unsigned char _ubuf[3];
 unsigned char _nbuf[1];
 struct __sbuf _lb;
 int _blksize;
 fpos_t _offset;
} FILE;
extern FILE *__stdoutp;
int fprintf(FILE * restrict, const char * restrict, ...) __attribute__((__format__ (__printf__, 2, 3)));
int main() {
  fprintf(__stdoutp, "Blimey! Error: %d\n", 42);
  return 0;
}

This is a key difference between #include and any import features in other languages. In Go, for example, you cannot inline fmt instead of doing import "fmt".

How, then, does an implementation of fprintf get into your compiled program? Via linking. More on that later.

What can computers do? What are the limits of mathematics? And just how busy can a busy beaver be? This year, I’m writing Busy Beavers, a unique interactive book on computability theory. You and I will take a practical and modern approach to answering these questions — or at least learning why some questions are unanswerable!

It’s only $19, and you can get 50% off if you find the discount code ... Not quite. Hackers use the console!

After months of secret toil, I and Andrew Carr released Everyday Data Science, a unique interactive online course! You’ll make the perfect glass of lemonade using Thompson sampling. You’ll lose weight with differential equations. And you might just qualify for the Olympics with a bit of statistics!

It’s $29, but you can get 50% off if you find the discount code ... Not quite. Hackers use the console!

More by Jim

Tagged . All content copyright James Fisher 2017. This post is not associated with my employer. Found an error? Edit this page.