Why could a header file be including itself?

2020-06-22 02:41发布

问题:

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?

回答1:

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.