473,503 Members | 1,888 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

What is the difference ?

What is the difference between:

main(......) {

exit(0);
}

and

main(......) {

return(0);
}

Thank you!
Dec 19 '06 #1
5 1972
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
"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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

125
14539
by: Sarah Tanembaum | last post by:
Beside its an opensource and supported by community, what's the fundamental differences between PostgreSQL and those high-price commercial database (and some are bloated such as Oracle) from...
1
8238
by: mike | last post by:
best regards: What is the difference between DOM Level 1 and DOM Level 2. Thank you. May god be with you.
5
20585
by: radnus2004 | last post by:
Hi all, I am not clear what is the difference between signed and unsigned in C. Many say, unsigned means only +ve values and 0. Can I use unsigned int i = -3? What happens internally ? What...
10
15611
by: tinesan | last post by:
Hello fellow C programmers, I'm just learning to program with C, and I'm wondering what the difference between signed and unsigned char is. To me there seems to be no difference, and the...
13
4998
by: Jason Huang | last post by:
Hi, Would someone explain the following coding more detail for me? What's the ( ) for? CurrentText = (TextBox)e.Item.Cells.Controls; Thanks. Jason
12
17357
by: Nathan Sokalski | last post by:
What is the difference between the Page_Init and Page_Load events? When I was debugging my code, they both seemed to get triggered on every postback. I am assuming that there is some difference,...
3
2999
by: Anoj Kumar | last post by:
Hi All , can anyone tell me what is the difference between the following declaration and how it affects application performance. 1. Dim cn As ADODB.Connection Set cn = New ADODB.Connection
6
11233
by: Mark Kamoski | last post by:
Hi Everyone-- Please help. What is the difference between a Field and a Property? Here is the context. In the VS.NET 2002 documentation, in the "String Members" section, we have the...
5
5835
by: kai | last post by:
Hi, In ASP.NET , what is the difference between OnClick and Click events for a button? Because we have button click event, it can trigger events, why we still need OnClick? Please help. ...
49
2380
by: Zach | last post by:
After having taken a looong break from VB (last used 6.0), I started getting back into programming again, this time with VS 2005. I began reading up on VB.NET from various sources (books,...
0
7348
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7006
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7467
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5021
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4685
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3175
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3166
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1519
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
744
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.