Considering the efforts it goes to add include guards to each header file and the added possibilities of errors they introduce (e.g. using the same macro name in more than one file), why are they not build into the language as default behaviour?
What language design tradeoffs have lead to this decision by the makers of C/C++ standards?
Are there any practical benefits for being able to include files multiple times?

- 2,060
- 2
- 23
- 29
-
See this post: https://stackoverflow.com/questions/24943620/when-is-it-useful-to-include-the-same-header-multiple-times-in-one-file – P.W Jan 21 '19 at 06:12
-
2Highly relevant, even a dupe I'd say https://stackoverflow.com/questions/53416657/why-isnt-pragma-once-automatically-assumed – StoryTeller - Unslander Monica Jan 21 '19 at 06:12
-
3This decision is nearly *fifty years old* and it reflects the state of compiler technology of the time and the language design philosophy of tge time. It is too late to change now. – n. m. could be an AI Jan 21 '19 at 06:26
-
1@n.m.: I'll say it is related to language design philosophy. C and even C++ have a textual preprocessor. Lisp macros are much more powerful (and C++ did not take enough inspiration from them). – Basile Starynkevitch Jan 21 '19 at 06:29
-
1@BasileStarynkevitch Lisp macros don't make much sense in the context of Algol-like languages. One needs uniform minimalistic syntax for them to work. Furthermore, the C preprocessor wasn't designed from the ground up but evolved with time from a simple file inclusion mechanism. – n. m. could be an AI Jan 21 '19 at 06:40
-
@n.m: I disagree, but I need to write an entire paper to explain why – Basile Starynkevitch Jan 21 '19 at 06:50
-
@BasileStarynkevitch please ping me when the paper is out. – n. m. could be an AI Jan 21 '19 at 06:56
-
@n.m. You might look into my [technical report on `bismon`](http://starynkevitch.net/Basile/bismon-chariot-doc.pdf) (this is a draft in january 2019) for some partial and indirect insights. – Basile Starynkevitch Jan 21 '19 at 20:02
1 Answers
In some cases, you want to include the same header several times, for different purposes.
Are there any practical benefits for being able to include files multiple times?
Of course yes. Read also about X-macros.
For example, you are defining some enum
for colors.
You could have a myenum.h
header with
// file myenum.h included several times
MYENUM(red)
MYENUM(blue)
MYENUM(yellow)
(and you could imagine hundreds of other colors there, that you don't want to repeat)
then, in your main.c
file (or in some guarded header file), you declare that enum using:
enum color_en {
#define MYENUM(X) X,
#include "myenum.h"
#undef MYENUM
};
and in the same main.c
file, you later have a printing routine:
void print_color(enum color_en col) {
switch (col) {
#define MYENUM(X) case X: puts(#X); break;
#include "myenum.h"
#undef MYENUM
default: puts("???"); break;
}
}
Read the documentation of cpp
. The #X
above is stringizing X
Notice that even with modern C++, doing the same with templates is surprisingly difficult (or still impossible).
I am surprised people are not taught more about such useful tricks. Of course, you probably want to add more comments to keep your code readable.
PS. Notice that C preprocessing and its macros is profoundly (and sadly) textual - designed as string rewriting. Common Lisp and Terra and Rust macros show that other approaches are possible (they view their macro system as manipulation of ASTs).

- 223,805
- 18
- 296
- 547