In C/C++ why does the main function have return type int, instead of double or string?

Answer by Steve Lacy:

Because the result value of main() is passed to the exit() system call on UNIX systems.

The exit() system call takes a single integer argument which is stored in the PID info record in the kernel, and can then be retrieved by the calling process when they call one of the wait() family of system calls.

This method of passing a return code through the PID info has been around since the earliest days of UNIX, which was written in C. 
In UNIX, processes typically return a success/failure code in this space, so that the calling process.  Making the return code from main() the same value is just a simple shortcut.

In C/C++ why does the main function have return type int, instead of double or string?

Advertisements

Leave a comment

Filed under Life

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s