Search code examples
c++entry-point

Question on entry point (beginner level )


I have started learning C++ according to a recommended list in Stack Overflow. There is book called "C++ primer" got me interested .Anyway in that book writer called "main" function an entry point. According to "Wikipedia" (what I understood) entry points are used to run a program .Does it give permission to OS to run my code? Is that why main is needed so OS can recognize and have the authority to run the code?


Solution

  • Does it give permission to OS to run my code?

    Nope.

    A program is a sequence of commands for the computer, commands such as std::cout << "Hello, world!\n";. The formal term for a such command (in C++) is statement.

    Statements are generally executed from top to bottom, but which statement should be executed first? Can't be the first statement in the source code file, because there can be more than one file.

    In C++ it was decided that the first statement to be executed is the first statement of main, followed by the rest of statements in it. Even if your program contains more than one source code file, there can't be more than one main.

    Execution of statements in a specific order is called control flow, and since control flow enters your program at the beginning of main, it's called the entry point.

    It will make more sense once you learn about functions.