Entry Point (main
)¶
When you run a program, something must happen first, that's our entry point. It's the very first function the operating system or runtime environment calls to kick off your code.
In most high-level compiled languages like C#, C++, or Rust, the entry point is a function called main
. Some scripting languages like Python don't need a special function — code at the top level runs automatically.
Think of it as the front door of your application: when the operating system "enters" your program, it goes through main.
Why does it matter?
- It's the root of your program's execution tree.
- It's where you allocate memory, initialize subsystems, or call other functions.
- Every compiled binary or assembly must define this point clearly for the OS or runtime to find it.
Note
Even in Assembly, there's still a "main", it's just not always called that.
Anatomy of an Entry Point¶
The entry point main
is a function, just like many others it has;
- A name (identifier)
- A return type
- (Possibility of using parameters)
However, main is special in the way that it doesn't NEED to return it's return type, which is unlike any other function in C++.
main()
is always looked for first by the runtime in C, C++, and C# (via metadata).- The return value of
main()
tells the OS if the program ran successfully (0 means OK). - In Assembly, we manually define a label (like
_start
) as the entry point, and call system calls directly.
Parameters¶
All versions of main can accept arguments passed by the operating system. These are usually the commandline arguments that we pass when starting a certain app.
Think of it as using the commandprompt to open chrome in incognito mode and browse to google. Google and incognito are our arguments that we pass to chrome.exe's main function.
Example