/*=== self.c 2.2.0 (2001-Sep-13-Thu) http://www.cs.berkeley.edu/~amc/utils/#self.c Adam M. Costello Problem: Write a C89 (ANSI C) program that outputs its own source code. Try to make it as small as possible (newline counts as one character). Notes on C89: The source file must end with a newline. The character encoding is not necessarily ASCII (so 34 cannot be used for double-quote, for example). If a function that takes a variable number of arguments (like printf) is called outside the scope of a declaration that includes an ellipsis (...), the behavior is undefined. A 76-character solution appears below. It will typically generate some warnings, none of which invalidate it as a correct C89 solution: * The return type of main() is omitted and defaults to "int". C89 allows this (though C99 does not). * main() is declared with no parameters. C89 allows this (as does C99). * The function puts() is declared implicitly. C89 allows this (though C99 does not). * The end of a non-void function (main) is reached. I don't think C89 prohibits this (K&R2 on page 70 explicitly says that it is not illegal, but I don't have a copy of the official standard). This leaves the return value unspecified (and hence the exit status of the program is unspecified), but the output is correct, and that is all that the problem statement requires. I have been unable to find anything in the C99 standard about whether this is valid in C99, which I suppose means that it's not. For other (larger) solutions to this problem, see: http://www.nyx.org/~gthompso/self_c.txt ===*/ #define a(x)x(#x")");} a( main(){puts("#define a(x)x(#x\")\");}\na(");puts)