4

Can anyone think of any scenario in which a header file includes itself?

I saw it in one of the program and this inclusion is under conditional compilation block for which at least i could not find any true condition.But, I was thinking could there be any technical requirement for such scenario?

Dariusz
  • 21,561
  • 9
  • 74
  • 114
Bhupesh Pant
  • 4,053
  • 5
  • 45
  • 70
  • 2
    Could you show us the actual code where you saw this? Do you mean "self-inclusion" in a circular manner across two or more files? Or actual self-inclusion in a single file? – Bart Aug 27 '13 at 14:19
  • 3
    Off-topic since this doesn't seem to be a practical problem the OP is facing. Maybe there is a reason for doing this, maybe there isn't, but without an example this is a poll question. – millimoose Aug 27 '13 at 14:21
  • Another way of looking at this is that you're begging the question - you're assuming that there's a valid technical reason for doing so when that might not be the case and what you saw might have been a brainfart or a historical artifact introduced by refactoring. Unless you know that there is a valid reason for doing so or can provide actual code you want someone to explain, I'm not sure how this is answerable with anything but guesses. – millimoose Aug 27 '13 at 14:23
  • 3
    There are "valid" (albeit hideous) reasons for doing this. The mostly relate to doing recursive computation in the preprocessor... – R.. GitHub STOP HELPING ICE Aug 27 '13 at 14:24
  • 3
    [PackedArray](https://github.com/gpakosz/PackedArray) uses such a strategy to unroll loops via the preprocessor. – user7116 Aug 27 '13 at 14:25
  • possible duplicate of [Including header files in C/C++ just once](http://stackoverflow.com/questions/10877494/including-header-files-in-c-c-just-once) – nneonneo Aug 27 '13 at 14:43

1 Answers1

6

Yes, if you are trying to win the International Obfuscated C Code Contest. Here's a nice example (the source file is called isaak.c):

main(){}
#define P define
#P U ifdef
#P main Si
#U y
#undef y
#include "isaak.c"
Pb
#else
char*K="4499999;8   9+jW*':'TZhD m:*h.4-j'9(z7Q>r*:G#FS]mATIdMZY^HaKFZZ\
JyJw:X49@eJj1,Z'\\c^jGU@IXTF@9P2i:gAZx0pD*W3\\<ZZs1:.~Z8U:P<\\:ZOI0GBPZ7",*H
,S[5202],*B="oA9BA6iN`]`Ph>5F4::M6A69@6I{g[Za__]NPV``aV\177E4C5CG;4C<BEJG;\
?LGlSZ[Y_!oYi@uXPzLFyPOYP][]`RTaQo86564CAHCG4ES",*F,N;int Bk,V;Y
#endif
#P C K/16-2
(){char*H;F O=-263;for(H="$+---+|||";*++H;)*(F O=(*H+5&129)+1)= *H;F
#P W sprintf(
O= -132;}I/**/r(){if((N= *IO/**/O%(21 O -5)+81 O 16)==107)N+=
#undef I
*K++&15;*F++=N;return*K;}
#undef O
#P I K
#P O +
#U N
exit(N){F=WH=S,"%5060d")+385;while(Br(),++B,Kr())F+=(N=
*B++/26-1)?(")21["[N]-46)*N*4-22:-3194;while(*--K!=9){while(!(*++H+5&64));
F=(40-"(\206/"[((H-S)%130+45)/57]<<3)+H;*F++=*H++;*F=
*H==106?32:*H;Y();W WF-131,"%-3d",++Bk)+260,"%3d",V+=
*C?*C:"hijpqv"[*--C]-106);Pb();}for(H=S;*H||(int)_exit(0);H+=130)write(1,1+W
F+3,"%c%-73.73s\n",0,H),74);}
#endif
#undef U
#P U ifndef
#include <stdio.h>

In all seriousness, a header file should not normally try to include itself directly because that's generally just a bad idea (you'd have to use conditional compilation to control recursion, and that gets hairy fast). If a header file includes itself indirectly (through an intermediate header file) then it is likely an error because the intermediate header file will not be able to use the definitions from the original header file.

That said, in the hands of a skilled practitioner, and a very specific need, it is possible to do computation in the preprocessor. Such computation may be necessary to generate compile-time constructs, perform manual loop unrolling, or do various other "preprocessor tricks". In all these cases, the use of self-includes or multiple includes is carefully designed.

nneonneo
  • 171,345
  • 36
  • 312
  • 383
  • 1
    If you really want to see "computation in the preprocessor", take a peek at [2004/vik2.c](http://www.ioccc.org/2004/vik2.c). This beauty computes primes using just the preprocessor. Of course, it may take several hours and gigs of RAM to complete... – nneonneo Aug 27 '13 at 14:36