2 very basic qns

M

mescaline

1. Why does C++ require the int in:
int main(){...}
(is there a special reason for int, why not void,etc...?)

2. What purpose does the return 0; serve at the end of the main
program:
(when the program is run successfully and the " return 0; " statement
is encountered, what exactly happens -- where is the 0 returned?)

thanks
 
R

Ron Natalie

mescaline said:
1. Why does C++ require the int in:
int main(){...}
(is there a special reason for int, why not void,etc...?)

Are you talking about main specifically or functions in general. The "implicit"
int return has been removed from the language because it wasn't exactly consistant.

As far as why MAIN itself must return int. Tradition is the major reason.
2. What purpose does the return 0; serve at the end of the main
program:
(when the program is run successfully and the " return 0; " statement
is encountered, what exactly happens -- where is the 0 returned?)
It's an implementation specific success code. On some systems it gets
passed back to the invoked program (like UNIX). On some systems
the environment calls a system call indicating normal termination where as
the error returns from exit/main call the error system call. On others a
zero means silent exit while a non-zero return cause an error popup.
Others the value is just discarded.
 
G

Gary Labowitz

Ron Natalie said:
Are you talking about main specifically or functions in general. The "implicit"
int return has been removed from the language because it wasn't exactly consistant.

As far as why MAIN itself must return int. Tradition is the major
reason.

Well, not really. The standards group, that works on defining the language,
has published a standard that main must return an int.
Unless you want to call standards a tradition...
 
R

Ron Natalie

Gary Labowitz said:
Well, not really. The standards group, that works on defining the language,
has published a standard that main must return an int.
Unless you want to call standards a tradition...
--
I know that. The poster asked for WHY. The language is defined by the
standard, but the standards people had reasons for the requirements they
set down. The reason main is REQUIRED BY THE STANDARD
to return in is TRADITION. Main has always returned int, ever since
the early days of C. Partially this was because C didn't even have a void
type in the beginning, then later because UNIX relied heavily on using the
numeric return from programs.
 
M

mescaline

Gary Labowitz said:
reason.

Well, not really. The standards group, that works on defining the language,
has published a standard that main must return an int.
Unless you want to call standards a tradition...


Conversely, does it always return a non-zero value, when there's *any*
error with the compiling?

thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top