c - A macro that invokes itself prints itself? -


the following program looks c macro calls itself.

#define q(k)int puts();int main(){puts(#k"\nq("#k")");} q(#define q(k)int puts();int main(){puts(#k"\nq("#k")");}) 

it compiles , runs fine. prints out.

is code c? is, rely on outside of standard c work correctly?

@devnull pointed out this question has similar program:

#define q(k)main(){return!puts(#k"\nq("#k")");} q(#define q(k)main(){return!puts(#k"\nq("#k")");}) 

does program rely on outside of standard c work correctly?

the first program example of implementation of quine in c. @ high level, defines macro q() creates definition of main() prints out 2 lines. first line argument itself, second line argument wrapped in call q() itself. so, following program:

#define q(k)int puts();int main(){puts(#k"\nq("#k")");} q(foo) 

expands into:

int puts();int main(){puts("foo""\nq(""foo"")");} 

when compiled , run, results in output:

foo q(foo) 

substituting macro definition in place of foo results in quine. macro not invoke itself, invoked on same text defines it. in c, macros not expanded recursively (c.99 §6.10.3.4 ¶2).

as noted in question, program compiles without complaint under gcc using strict c.99 settings (-pedantic -std=c99). program uses standard c features, , conforms both c.99 , c.11.

  • macro replacement (c.99 §6.10.3), argument substituion (c.99 §6.10.3.1) , # "stringifying" operator (c.99 §6.10.3.2).
  • function declaration unspecified argument list (c.99 §6.7.5.3 ¶14).
  • string literal concatenation (c.99 §5.1.1.2 ¶1).
  • default main() return value (c.99 §5.1.2.2.3 ¶1).

of particular note, program not rely on ascii encoding of characters.

the program compile on c.89-90 compiler, behavior of not returning value main() not defined c.89-90. program can trivially modified become c.89-90 compliant adding return 0; after call puts().

as second program, quine. however, not c.89-90, nor c.99, nor c.11 compliant. because relies on puts() return positive number logical-not operator return value 0. however, c requires puts() return non-negative value on success (c.99 §7.19.7.10 ¶3). c.89-90 permits implicit function declarations (c.89, §3.3.2.2). program can modified conform c.89-90 removing return!, , adding return 0; after puts() call.

the structure of these programs largely inspired implementation of quine program in "fictional" language bloop, presented in book gödel, escher, bach: eternal golden braid, douglas r. hofstadter (credited having coined term quine).

define procedure "eniuq" [template]: print [template, left-bracket, quote-mark, template, quote-mark, right-bracket, period]. eniuq ['define procedure "eniuq" [template]: print [template, left-bracket, quote-mark, template, quote-mark, right-bracket, period]. eniuq']. 

as aside, here version of program prints out source code in reverse order:

#define q(k)r(char*s){if(*s)r(s+1);putchar(*s);}main(){r(#k"\nq("#k")\n");} q(#define q(k)r(char*s){if(*s)r(s+1);putchar(*s);}main(){r(#k"\nq("#k")\n");}) 

Comments

Popular posts from this blog

c++ - Creating new partition disk winapi -

Android Prevent Bluetooth Pairing Dialog -

php - joomla get content in onBeforeCompileHead function -