liveBook · Manning

Header file

As explained in Modern C

A header file is a source file containing declarations and other material intended for inclusion before compilation.

A small C program can obtain declarations through included headers: #include <stdio.h> #include <stdlib.h> int main(void) { printf("hello\n"); return EXIT_SUCCESS; } The names printf, size_t, and EXIT_SUCCESS are declared elsewhere. A header file is a source file containing declarations and other material intended for inclusion before compilation. section 2.2 section 8.1.1

A header describes an interface; it does not create the declared feature. Repeating consistent declarations is usually harmless, but contradictory declarations in one scope are generally forbidden. Header contents are normally implementation details: consult platform documentation for semantics rather than trying to read barely readable system headers. section 2.2

 1   #ifndef BRIAN_H
 2   #define BRIAN_H 1
 3   #include <time.h>
 4
 5   /** @file
 6    ** @brief Following Brian the Jay
 7    **/
 8
 9   typedef struct brian brian;
10   enum chap { sct, en, };
11   typedef enum chap chap;
12
13   struct brian {
14     struct timespec ts; /**< point in time */
15     unsigned counter;   /**< wealth        */
16      chap masterof;     /**< occupation    */
17   };
18
19   /**
20    ** @brief get the data for the next point in time
21    **/
22   brian brian_next(brian);
23
24   ...
25   #endif


!@%STYLE%@!
{"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":0,\"ch\":1},{\"line\":0,\"ch\":2}],[{\"line\":1,\"ch\":21},{\"line\":1,\"ch\":22}],[{\"line\":9,\"ch\":0},{\"line\":9,\"ch\":1}],[{\"line\":10,\"ch\":0},{\"line\":10,\"ch\":1}],[{\"line\":11,\"ch\":0},{\"line\":11,\"ch\":1}],[{\"line\":12,\"ch\":0},{\"line\":12,\"ch\":1}],[{\"line\":13,\"ch\":0},{\"line\":13,\"ch\":1}],[{\"line\":14,\"ch\":0},{\"line\":14,\"ch\":1}],[{\"line\":15,\"ch\":0},{\"line\":15,\"ch\":1}],[{\"line\":16,\"ch\":0},{\"line\":16,\"ch\":1}],[{\"line\":17,\"ch\":0},{\"line\":17,\"ch\":1}],[{\"line\":18,\"ch\":0},{\"line\":18,\"ch\":1}],[{\"line\":20,\"ch\":1},{\"line\":20,\"ch\":2}],[{\"line\":0,\"ch\":5},{\"line\":0,\"ch\":12}],[{\"line\":1,\"ch\":1},{\"line\":1,\"ch\":2}],[{\"line\":11,\"ch\":1},{\"line\":11,\"ch\":2}],[{\"line\":19,\"ch\":0},{\"line\":19,\"ch\":1}],[{\"line\":20,\"ch\":0},{\"line\":20,\"ch\":1}],[{\"line\":21,\"ch\":0},{\"line\":21,\"ch\":1}],[{\"line\":22,\"ch\":0},{\"line\":22,\"ch\":1}],[{\"line\":23,\"ch\":0},{\"line\":23,\"ch\":1}],[{\"line\":24,\"ch\":0},{\"line\":24,\"ch\":1}],[{\"line\":1,\"ch\":5},{\"line\":1,\"ch\":12}],[{\"line\":2,\"ch\":1},{\"line\":2,\"ch\":2}],[{\"line\":12,\"ch\":1},{\"line\":12,\"ch\":2}],[{\"line\":22,\"ch\":1},{\"line\":22,\"ch\":2}],[{\"line\":2,\"ch\":5},{\"line\":2,\"ch\":13}],[{\"line\":2,\"ch\":15},{\"line\":2,\"ch\":21}],[{\"line\":3,\"ch\":1},{\"line\":3,\"ch\":2}],[{\"line\":13,\"ch\":1},{\"line\":13,\"ch\":2}],[{\"line\":23,\"ch\":1},{\"line\":23,\"ch\":2}],[{\"line\":4,\"ch\":1},{\"line\":4,\"ch\":2}],[{\"line\":14,\"ch\":1},{\"line\":14,\"ch\":2}],[{\"line\":24,\"ch\":1},{\"line\":24,\"ch\":2}],[{\"line\":5,\"ch\":1},{\"line\":5,\"ch\":2}],[{\"line\":15,\"ch\":1},{\"line\":15,\"ch\":2}],[{\"line\":6,\"ch\":1},{\"line\":6,\"ch\":2}],[{\"line\":16,\"ch\":1},{\"line\":16,\"ch\":2}],[{\"line\":7,\"ch\":1},{\"line\":7,\"ch\":2}],[{\"line\":17,\"ch\":1},{\"line\":17,\"ch\":2}],[{\"line\":8,\"ch\":1},{\"line\":8,\"ch\":2}],[{\"line\":18,\"ch\":1},{\"line\":18,\"ch\":2}],[{\"line\":8,\"ch\":5},{\"line\":8,\"ch\":19}],[{\"line\":9,\"ch\":0},{\"line\":9,\"ch\":2}],[{\"line\":9,\"ch\":5},{\"line\":9,\"ch\":9}],[{\"line\":10,\"ch\":13},{\"line\":10,\"ch\":17}],[{\"line\":10,\"ch\":0},{\"line\":10,\"ch\":2}],[{\"line\":10,\"ch\":5},{\"line\":10,\"ch\":17}],[{\"line\":11,\"ch\":0},{\"line\":11,\"ch\":2}],[{\"line\":12,\"ch\":0},{\"line\":12,\"ch\":2}],[{\"line\":8,\"ch\":13},{\"line\":8,\"ch\":19}],[{\"line\":12,\"ch\":5},{\"line\":12,\"ch\":11}],[{\"line\":13,\"ch\":7},{\"line\":13,\"ch\":13}],[{\"line\":13,\"ch\":0},{\"line\":13,\"ch\":2}],[{\"line\":8,\"ch\":13},{\"line\":8,\"ch\":19}],[{\"line\":12,\"ch\":5},{\"line\":12,\"ch\":11}],[{\"line\":13,\"ch\":7},{\"line\":13,\"ch\":13}],[{\"line\":13,\"ch\":14},{\"line\":13,\"ch\":22}],[{\"line\":14,\"ch\":0},{\"line\":14,\"ch\":2}],[{\"line\":14,\"ch\":7},{\"line\":14,\"ch\":15}],[{\"line\":15,\"ch\":0},{\"line\":15,\"ch\":2}],[{\"line\":16,\"ch\":0},{\"line\":16,\"ch\":2}],[{\"line\":17,\"ch\":0},{\"line\":17,\"ch\":2}],[{\"line\":18,\"ch\":0},{\"line\":18,\"ch\":2}],[{\"line\":19,\"ch\":0},{\"line\":19,\"ch\":2}],[{\"line\":20,\"ch\":0},{\"line\":20,\"ch\":2}],[{\"line\":21,\"ch\":0},{\"line\":21,\"ch\":2}],[{\"line\":22,\"ch\":0},{\"line\":22,\"ch\":2}],[{\"line\":23,\"ch\":0},{\"line\":23,\"ch\":2}],[{\"line\":24,\"ch\":0},{\"line\":24,\"ch\":2}],[{\"line\":24,\"ch\":5},{\"line\":24,\"ch\":11}]]"}
!@%STYLE%@!

This header groups the declarations and type definitions needed to use a data type, includes a dependency, documents the interface, and prevents multiple inclusion with BRIAN_H. section 10.1

Header files commonly provide:

  • Standard-library interfaces such as input/output through stdio.h, numerical and string facilities through stdlib and string.h, and time handling through time.h. section 8.8
  • Project-specific interfaces, often grouping functions that treat one data type and including any headers needed to compile those declarations. section 8.1.1 section 10.1
  • Include guards, which protect a header against multiple inclusion. section 10.1

Finding interface documentation

When printf is the feature of interest, search platform documentation rather than the header itself. The suggested command sequence is: apropos printf, man printf, or man 3 printf. section 2.2

How Modern C covers it

How deep it goes
Explains declarations, interface grouping, dependencies, and include guards, but defers detailed library semantics to platform documentation.
Examples
Uses stdio.h, stdlib.h, printf, standard-library headers, and a guarded project header named brian.h.
What it assumes
Assumes basic C declarations and compilation while teaching header-based library access and interface organization.
What you can do after
Build interfaces from scratch by grouping related declarations, dependencies, documentation, and include guards in a project header.
How it connects
Connects declarations with interfaces, functions, standard libraries, include directives, and include guards.

Also explained in

Compare how 2 books explain header file →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Modern C. The section links point to the parts of the book it draws on.