Why can the header file include itself?

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

I saw this in one of the programs, and this inclusion is in the conditional compilation unit, for which at least I could not find any true condition. But I thought, could there be some technical requirement for such a scenario?

+4
source share
1 answer

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

 main(){} #define P define #PU 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 #PCK/16-2 (){char*H;FO=-263;for(H="$+---+|||";*++H;)*(FO=(*H+5&129)+1)= *H;F #PW 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 #PIK #PO + #UN 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/"[((HS)%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 #PU ifndef #include <stdio.h> 

In all seriousness, the header file should usually not try to include itself, because in general it is just a bad idea (you will have to use conditional compilation to control recursion and it quickly gets hairy). If the header file contains indirect (via the intermediate header file), then this is most likely an error, because the intermediate header file will not be able to use the definitions from the original header file.

However, in the hands of an experienced practitioner and a very specific need, you can perform calculations in the preprocessor. Such a calculation may be required to create compilation time constructions, perform manual cyclic deployment, or perform other “preprocessor tricks”. In all these cases, the use of self-service or multiple inclusions is carefully considered.

+6
source

Source: https://habr.com/ru/post/1499162/


All Articles