87

In an interview I was confronted with a question such as this:

Your friend has given you a single source code file which prints the Fibonacci numbers on the console. Note that the main() block is empty and doesn't have any statements inside it.

Explain how this is possible (hint: global instance!)

I really want to know about this, how such a thing can be even possible!

7
  • 26
    Look at the hint! Commented Jul 8, 2013 at 15:18
  • 14
    Because it's something that 1) I hadn't heard of, 2) is useful trivia because people ask it in interviews, 3) an interesting application of the language to know so that 4) I can recognize it and stab anyone in the face with a rusty shiv if I see them actually using it in production code. Commented Jul 8, 2013 at 15:43
  • 4
    A competent, professional C++ programmer will know the answer to this question. If the purpose of this interview question is to determine if the person being interviewed is a competent, professional C++ programmer, then the question shouldn't give them the answer. Commented Jul 8, 2013 at 15:47
  • 1
    In an interview setting, one alternative would be to have the logic inside any function in the code and log the output using 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. ;-) Commented Jul 10, 2013 at 2:24
  • 1
    Was it an interview for IOCC? :-) Ok, I admitt I do it often for initializing my factories or executing some test-code. Btw, 'single source code file' is also a hint, that the entry-pint (main by default) is not replaced by linker.
    – Valentin H
    Commented Jan 28, 2015 at 20:55

6 Answers 6

128

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++).

15
  • 3
    @Nawaz It's probably worth citing the exact guarantees. Objects within a translation unit are guaranteed to be initialized in order. The standard stream objects are guaranteed to be initialized before or during the first initialization of an std::ios_base::Init object. And <iostream> is guaranteed to behave "as if" it contained an instance of an std::ios_base_Init object at namespace scope. Commented Jul 8, 2013 at 15:30
  • 3
    @Steve314: It doesn't return anything which is why I've used comma operator, to ensure that the type of the whole expression (print_fibs(), 0) is int. Here is Online Demo. Commented Jul 8, 2013 at 15:41
  • 1
    @Nawaz An alternative to the void function and the comma operator would be to return a bool, and the variable bool fibsPrinted. That's probably slightly cleaner if the function only serves here. (But the difference is probably not enough to worry about.) Commented Jul 8, 2013 at 16:07
  • 1
    +1, Talk about awesome. Had to join stackoverflow just to upvote this question and this answer. Commented Jul 9, 2013 at 1:58
  • 1
    @Nawaz I'm not sure what your point is. The definition of std::cout is somewhere in the library. But as I have already pointed out, the standard requires it to be initialized before the first constructor of an std::ios_base::Init object has finished, and it requires that including <iostream> behave as if an std::ios_base::Init object were defined at namespace scope. If the translation unit includes <iostream> before the definition of the object being initialized, std::cout is guaranteed to be constructed. Commented Jul 9, 2013 at 8:38
18

Hope this helps

class cls
{
  public:
    cls()
    {
      // Your code for fibonacci series
    }
} objCls;

int main()
{
}

So as soon as a global variable of the class is declared, the constructor is called and there you add the logic to print out the Fibonacci series.

9

Yes it is possible. You need to declare a global instance of an object that calculates the Fibonacci numbers in the object constructor.

1
  • 6
    You need to declare a global instance of an object whose initializer calculates the Fibonacci numbers. Commented Jul 8, 2013 at 15:24
4

I know some examples like that you tell. One way to get it is using the template metaprogramming. Using it you can move some compute process to the compilation.

Here you can get an example with the Fibonacci numbers

If you use it in a static class constructor and you can write the numbers without need to write any code in the main function.

Hope it helps you.

3

Things can happen during initialization of global/static variables. The code will be trigger at the application start.

3

All[*] constructors for file-scope objects get called before reaching main, as do all initializer expressions for non-object file-scope variables.

Edit: Also, all[*] destructors for all file-scope objects get called in reverse order of construction after main exits. You could, theoretically, put your fibonacci program in an object's destructor.

[*] Note that 'all' ignores the behavior of dynamically loading and unloading libraries that your program wasn't directly linked with. Those technically are outside the base C++ language, though.

4
  • All? Even those in dll's that are explicitly loaded after main? Commented Jul 8, 2013 at 15:24
  • Well, C++ doesn't technically define dynamically loaded libraries, so within pure C++, my statement is correct. So, shade it "All, save for initializers and file-scope objects contained in DLLs/DSOs loaded after reaching main." In this case, main is empty, so those DLLs/DSOs would have to be loaded by destructors, which is darn perverse. But, this being computer science, I suppose we should be careful with words like "all".
    – Joe Z
    Commented Jul 8, 2013 at 15:35
  • I added a caveat on 'all' to my answer above, and also added a note about dtors.
    – Joe Z
    Commented Jul 8, 2013 at 15:39
  • Yes, but hopefully that will come. Pre C++11 contained some weasel wording meant to allow DLLs, but which in practice only meant that technically, the guarantee wasn't always there, even though it was in all actual implementations, and much code depended on it. C++11 fixed that, at least. Commented Jul 8, 2013 at 16:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.