Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How is it possible to declare nothing inside main() in C++ and yet have a working application after compilation?

Tags:

c++

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!

like image 796
Hossein Avatar asked Jul 08 '13 15:07

Hossein


People also ask

What is the problem in declaring int main ()?

int main() doesn't include any list. So gcc is complaining about an old-style (K&R) function definition...that would allow main() to take any number of arguments. That's not one of the standard-sanctioned forms.

Is int main () a function declaration?

Nope. You can't call it anyway. You only need forward declarations for functions called before they are defined. You need external declarations (which look exactly like forward declarations on purpose) for functions defined in other files.

Does every program need a main?

Generally speaking, an application needs an entry point, and main is that entry point. The fact that initialization of globals might happen before main is pretty much irrelevant.

Why do we need a main function in C++?

The main function is a special function. Every C++ program must contain a function named main. It serves as the entry point for the program. The computer will start running the code from the beginning of the main function.


2 Answers

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):

  • Is main() really start of a C++ program?

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

like image 95
Nawaz Avatar answered Oct 12 '22 03:10

Nawaz


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.

like image 22
Saksham Avatar answered Oct 12 '22 03:10

Saksham