what is the differrence b/w the usage or return and the exit in the C programming..

J

jayapal

hi all,
what is the differrence b/w the usage or return and the exit in the C
programming..

thanks,
jay
 
P

Philip Potter

jayapal said:
hi all,
what is the differrence b/w the usage or return and the exit in the C
programming..

Within main(), not a lot.

Within any other function, return ends the execution of the function
immediately and returns to the caller, possibly also returning a value.
exit() terminates the program.
 
Q

quarkLore

hi all,
what is the differrence b/w the usage or return and the exit in the C
programming..

thanks,
jay

exit is a function call return is a language keyword apart from many
other differences.
 
S

santosh

jayapal said:
hi all,
what is the differrence b/w the usage or return and the exit in the C
programming..

In C return is a keyword while exit() is a Standard library function.
exit() accepts as argument a value of type int while return can
supplied an expression yielding a value of any legal type. exit()
causes termination of the program while return merely returns control
to the function's caller. A special case is a return in main() which is
roughly equivalent to an exit() call. exit() calls the functions
registered with atexit() while for return this is only true when in
main().

Use exit() when you want to terminate normally from anywhere in the
program. Use return for returning control to the function's caller.

<http://www.c-faq.com/>
<http://www.eskimo.com/~scs/cclass/>
<http://clc-wiki.net/>
<http://cprog.tomsweb.net/>
 
F

Flash Gordon

Philip Potter wrote, On 02/11/07 09:09:
Within main(), not a lot.

Why do people always forget about recursive calls to main? ;-)
Within any other function, return ends the execution of the function
immediately and returns to the caller, possibly also returning a value.
exit() terminates the program.

exit() does not always terminate the program...

#include <stdlib.h>

void foo(void)
{
for (;;) continue;
}

int main(void)
{
atexit(foo);
exit(0);
}
 
K

Kenny McCormack

In C return is a keyword while exit() is a Standard library function.
exit() accepts as argument a value of type int while return can
supplied an expression yielding a value of any legal type. exit()
causes termination of the program while return merely returns control
to the function's caller. A special case is a return in main() which is
roughly equivalent to an exit() call. exit() calls the functions
registered with atexit() while for return this is only true when in
main().

Use exit() when you want to terminate normally from anywhere in the
program. Use return for returning control to the function's caller.

<http://www.c-faq.com/>
<http://www.eskimo.com/~scs/cclass/>
<http://clc-wiki.net/>
<http://cprog.tomsweb.net/>

Next question:
what is the differrence b/w the usage of [the] printf and the gets in the C
programming..?
 
P

Peter Pichler

Kenny said:
Next question:
what is the differrence b/w the usage of [the] printf and the gets in the C
programming..?

What is b/w? Black and white?

What is the difference b/w a deposit and a withdrawal?

Printf prints a formatted string to a standard output, gets reads a
string from a standard input. Do not use gets, as it provides absolutely
no control against buffer overflow. Use fgets instead.
 
K

Keith Thompson

Peter Pichler said:
Kenny McCormack wrote: [...]
What is b/w? Black and white?

What is the difference b/w a deposit and a withdrawal?

Printf prints a formatted string to a standard output, gets reads a
string from a standard input. Do not use gets, as it provides
absolutely no control against buffer overflow. Use fgets instead.

Please don't feed the troll.
 
P

Philip Potter

Flash said:
Philip Potter wrote, On 02/11/07 09:09:

Why do people always forget about recursive calls to main? ;-)

Because they're usually a bad idea?

(Er, nice catch.)
exit() does not always terminate the program...

#include <stdlib.h>

void foo(void)
{
for (;;) continue;
}

int main(void)
{
atexit(foo);
exit(0);
}

Perhaps, but I'm quite glad I missed this special case out of my
original answer :)
 
F

Flash Gordon

Philip Potter wrote, On 03/11/07 09:54:
Because they're usually a bad idea?

I agree that they are a bad idea.
(Er, nice catch.)

Perhaps, but I'm quite glad I missed this special case out of my
original answer :)

Whenever anything is stated as an absolute truth someone, sometime, will
find an exception that proves that it is not absolute.
 
T

Tor Rustad

jayapal said:
hi all,
what is the differrence b/w the usage or return and the exit in the C
programming..

After the initial call to the main function, those two are equivalent
(assuming same value), except for one little detail, the exit(N) call
doesn't end lifetime of objects with automatic storage duration declared
in main.

To illustrate, TC2 gives this example as UB:

#include <stdio.h>
#include <stdlib.h>

void goodbye(void)
{
printf("Hello, World!\n");
}

int main(void)
{
char buf[20];
setvbuf(stdout, buf, _IOFBF, sizeof(buf));
atexit(goodbye);
return 0;
}

while replacing "return 0" with exit(0) above, gives a strictly
conforming program.
 
P

Philip Potter

Flash said:
Philip Potter wrote, On 03/11/07 09:54:

I agree that they are a bad idea.




Whenever anything is stated as an absolute truth someone, sometime, will
find an exception that proves that it is not absolute.

But it is more educationally sound to start with simple truths and only
mention the exceptions to those truths later on, than to find one cannot
define exit() without also defining atexit().
 
F

Flash Gordon

Philip Potter wrote, On 04/11/07 08:44:
But it is more educationally sound to start with simple truths and only
mention the exceptions to those truths later on, than to find one cannot
define exit() without also defining atexit().

Which is what we did.
 
P

Philip Potter

Flash said:
Philip Potter wrote, On 04/11/07 08:44:

Which is what we did.

Er.. I was working from the assumption that you were saying that I
should have mentioned atexit() from the beginning, but looking back on
the thread, it seems this is not the case.
 
F

Flash Gordon

Philip Potter wrote, On 04/11/07 16:26:
Er.. I was working from the assumption that you were saying that I
should have mentioned atexit() from the beginning, but looking back on
the thread, it seems this is not the case.

No, I was just being a pedantic awkward sod as was indicated in my sigs.
 
C

Charlie Gordon

Flash Gordon said:
Philip Potter wrote, On 03/11/07 09:54:

I agree that they are a bad idea.

Not as bad as calling main from a function registered with atexit ;-)
Whenever anything is stated as an absolute truth someone, sometime, will
find an exception that proves that it is not absolute.

That's typical c.l.c experience, except for qualifiers: here this happens
quickly and usually involves more than one responder.
 
F

Flash Gordon

Charlie Gordon wrote, On 07/11/07 14:22:
Not as bad as calling main from a function registered with atexit ;-)

Now you are getting *really* nasty :)
That's typical c.l.c experience, except for qualifiers: here this happens
quickly and usually involves more than one responder.

No, you've made the wrong response, you were meant to provide an
exception my statement! ;-)

Anyway, I think this thread has reached (or passed beyond) the point of
being worth pursuing.
 

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,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top