C++ program starts execution from main and ends at main?

4306 views c++
2

#include <bits/stdc++.h>
using namespace std;

class A
{

public:
A()
   {
    cout<<"A is called";

    }
}a;

int main()

{
    cout<<"main is called";

}

but here the A funtion is called first and main is called later what is the mechanism behind it ?

answered question

read about static storage duration, a is global variable, so it is created before entering main function.

1 Answer

8

What is going on is that the global variable a of type A is created first. In the construtor you output the text A is called and than the program starts with the main funtion.

So what you see is to be expected and you take from it that prior to the execution of main() global variables have to be created.

posted this

Have an answer?

JD

Please login first before posting an answer.