It is most likely implemented as (or a variant of it):
void print_fibs()
{
//implementation
}
int ignore = (print_fibs(), 0);
int main() {}
In this code, the global variable ignore
has to be initialized before entering into main()
function. Now in order to initialize the global, print_fibs()
needs to be executed where you can do anything — in this case, compute fibonacci numbers and print them! A similar thing I've shown in the following question (which I had asked long back):
Note that such code is not safe and should be best avoided in general. For example, the std::cout
object may not be initialized when print_fibs()
is executed, if so then what would std::cout
do in the function? However, if in other circumstances, it doesn't depend on such initialization order, then it is safe to call initialization functions (which is a common practice in C and C++).
assert
or#pragma message
etc. This will redirect the output to the console during compilation. The program may never even fully compile, but this sure is a fun way of getting to show your "out-of-the-box" thinking during the interview. This satisfies the quoted question as it does NOT mention anything about binary being generated; rather it just talks about a C file that can display "stuff" on the console. ;-)