By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,443 Members | 1,409 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,443 IT Pros & Developers. It's quick & easy.

What is the difference ?

P: n/a
What is the difference between:

main(......) {

exit(0);
}

and

main(......) {

return(0);
}

Thank you!
Dec 19 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
If you are calling some function recursively then return 0 will just go
to one level down on the stack, while exit 0 will terminate this
thread.

int f(int i)
{
if (i == 3)
{
return 0;;
}
f(i+1);
cout <<" the value is "<<i<<endl;
}
int main(void)
{
f(0);
return 0;
}

OUTPUT >the value is 2
the value is 1
the value is 0

while

int f(int i)
{
if (i == 3)
{
exit (0);
}
f(i+1);
cout <<" the value is "<<i<<endl;
}
int main(void)
{
f(0);
return 0;
}

will just return with out printing anything. Even if we will create one
object inside this function "f", it will not call destructor of this
object.While in the first program it will call the destructor of of the
object.

Antonio Parolini wrote:
What is the difference between:

main(......) {

exit(0);
}

and

main(......) {

return(0);
}

Thank you!
Dec 19 '06 #2

P: n/a
"Anant" <an*********@gmail.comwrote in news:1166510526.110605.230050@
73g2000cwn.googlegroups.com:
If you are calling some function recursively then return 0 will just go
to one level down on the stack, while exit 0 will terminate this
thread.
Also, as I recall, if you do an exit(0) from main() instead of return(0),
global variables (and I suspect variables local to main()) will not get
destructed. exit(0) means for your program to die now, no chance to clean
up after itself.
Dec 19 '06 #3

P: n/a
Andre Kostur wrote:
"Anant" <an*********@gmail.comwrote in news:1166510526.110605.230050@
73g2000cwn.googlegroups.com:
>If you are calling some function recursively then return 0 will just go
to one level down on the stack, while exit 0 will terminate this
thread.

Also, as I recall, if you do an exit(0) from main() instead of return(0),
global variables (and I suspect variables local to main()) will not get
destructed. exit(0) means for your program to die now, no chance to clean
up after itself.
You're confusing it with abort(). When exit() is called, global variables
(or more precisely variables with static storage duration) are properly
destroyed. You're right about the local (or rather the automatic) variables
though. Those aren't destroyed.

Dec 19 '06 #4

P: n/a
Antonio Parolini wrote:
>What is the difference between:

main(......) {

exit(0);
}

and

main(......) {

return(0);
}
Anant wrote:
If you are calling some function recursively then return 0 will just go
to one level down on the stack, while exit 0 will terminate this
thread.
Note: Top posting corrected.

Per 3.6.1/3: "The function main shall not be used (3.2) within a
program." Per 3.2/2, this indicates that you cannot call main
recursively. So Anant's comment is irrelevant. Plus, the C++ standard
says nothing about threads.

Per 3.6.1/4 exit will terminate the program without destroying any
objects with automatic storage duration. Rolf noted this in response to
Andre's comment.

Thus, the difference between Antonio's two examples is mainly the
destruction of any automatic variables in main().

Dec 19 '06 #5

P: n/a
Rolf Magnus <ra******@t-online.dewrote in
news:em*************@news.t-online.com:
Andre Kostur wrote:
>"Anant" <an*********@gmail.comwrote in
news:1166510526.110605.230050@ 73g2000cwn.googlegroups.com:
>>If you are calling some function recursively then return 0 will just
go to one level down on the stack, while exit 0 will terminate this
thread.

Also, as I recall, if you do an exit(0) from main() instead of
return(0), global variables (and I suspect variables local to main())
will not get destructed. exit(0) means for your program to die now,
no chance to clean up after itself.

You're confusing it with abort(). When exit() is called, global
variables (or more precisely variables with static storage duration)
are properly destroyed. You're right about the local (or rather the
automatic) variables though. Those aren't destroyed.
I stand corrected. I re-read the section of the standard (3.6.1.4) that
talks about exit(). Automatic storage duration variables are not
destroyed. Static storage duration do. (Unless exit() is called during
the destruction of static storage duration variables, then it's Undefined
Behaviour).
Dec 19 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.