473,378 Members | 1,393 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

main exit/return

Hi Everyone,

I have seen code in different styles like

main(argc,argv)
int argc;
char **argv;
{
if(argc != 2) exit(1);
else exit(0);
}

int
main(argc,argv)
int argc;
char **argv;
{
if(argc != 2) return(-1);
else return(0);
}

I'm not sure as to which version is advised by the standard and the
reason to avoid the other approach. What does the standard recommend
in case of main() function?

Thanks in advance ! ! !
Jun 27 '08 #1
11 2792
Rahul wrote:
Hi Everyone,

I have seen code in different styles like

main(argc,argv)
int argc;
char **argv;
That's a bit ancient.
{
if(argc != 2) exit(1);
else exit(0);
}

int
main(argc,argv)
int argc;
char **argv;
{
if(argc != 2) return(-1);
else return(0);
}

I'm not sure as to which version is advised by the standard and the
reason to avoid the other approach. What does the standard recommend
in case of main() function?
For the head:

int main(void) { ... }
int main( int argc, char **argv ) { ... }

(and trivial equivalent syntaxes)

For the return:

return 0;
return EXIT_SUCCESS;

return EXIT_FAILURE;

(there's an escape-hatch for no return at all; avoid.)

(`return` doesn't need parentheses; the standard doesn't
exhibit a preference, but I see no reason to use `()` when
they're completely irrelevant, and at least one reason -- to
emphasise that `return` is not a function call -- to leave
them out.)

The same values apply to the argument of `exit`, but why use
`exit` when you're in `main`?

--
"The original and modest plans had to be continually extended."/Sector General/

Hewlett-Packard Limited registered no:
registered office: Cain Road, Bracknell, Berks RG12 1HN 690597 England

Jun 27 '08 #2
On Jun 3, 10:46 am, Chris Dollin <chris.dol...@hp.comwrote:
The same values apply to the argument of `exit`, but why use
`exit` when you're in `main`?
There is a subtle difference if you used atexit () to register
functions that will be called when the program exits: If you call exit
() from main, local variables of the main function will still exist
when these functions execute; if you use a return statement then local
variables of main are gone.
Jun 27 '08 #3
On Jun 3, 11:55 am, Rahul <sam_...@yahoo.co.inwrote:
Hi Everyone,

I have seen code in different styles like

main(argc,argv)
int argc;
char **argv;
{
if(argc != 2) exit(1);
else exit(0);

}

int
main(argc,argv)
int argc;
char **argv;
{
if(argc != 2) return(-1);
else return(0);

}

I'm not sure as to which version is advised by the standard and the
reason to avoid the other approach. What does the standard recommend
in case of main() function?
Question 11.4 of the C-FAQ.
<http://c-faq.com/>
Jun 27 '08 #4
christian.bau wrote:
On Jun 3, 10:46 am, Chris Dollin <chris.dol...@hp.comwrote:
>The same values apply to the argument of `exit`, but why use
`exit` when you're in `main`?

There is a subtle difference if you used atexit () to register
functions that will be called when the program exits: If you call exit
() from main, local variables of the main function will still exist
when these functions execute; if you use a return statement then local
variables of main are gone.
That's a nice [1] gotcha; I hadn't thought of that. I'm trying to
think of a natural example where it would matter ...

[1] As in "oh noes, my brain just melted".

--
"The letter was not unproductive." /Mansfield Park/

Hewlett-Packard Limited registered office: Cain Road, Bracknell,
registered no: 690597 England Berks RG12 1HN

Jun 27 '08 #5
Chris Dollin <ch**********@hp.comwrites:
christian.bau wrote:
>On Jun 3, 10:46 am, Chris Dollin <chris.dol...@hp.comwrote:
>>The same values apply to the argument of `exit`, but why use
`exit` when you're in `main`?

There is a subtle difference if you used atexit () to register
functions that will be called when the program exits: If you call exit
() from main, local variables of the main function will still exist
when these functions execute; if you use a return statement then local
variables of main are gone.

That's a nice [1] gotcha; I hadn't thought of that. I'm trying to
think of a natural example where it would matter ...

[1] As in "oh noes, my brain just melted".
I thought I had come up with a plausible example, but I don't think
I actually have.

Suppose main() saves copies of argc and argv in "global" variables
saved_argc and saved_argv, so they can be accessed by other functions.
An atexit-registered cleanup routine uses these to access the
command-line arguments for whatever reason.

But then I looked at C99 5.1.2.2.1p2:

If the value of argc is greater than zero, the array members
argv[0] through argv[argc-1] inclusive shall contain pointers to
strings, which are given implementation-defined values by the host
environment prior to program startup.

[...]

The parameters argc and argv and the strings pointed to by the
argv array shall be modifiable by the program, and retain their
last-stored values between program startup and program
termination.

The strings pointed to by the argv array retain their values until
*program termination*, not just until termination of the main()
function.

If a program saved *pointers to* argc and argv, then there could be a
problem, but I can't think of any good reason to do that rather than
saving copies of them.

The standard is vague about the argv array itself (i.e., the array of
char* to whose first element the argv parameter points). It *should*
say that this array also survives until program termination, and I
think it's reasonably safe to assume that it does (other than on the
DS9K).

(Question: Does the DS9K implementation exploit poor wording, or does
it just exploit freedoms deliberately offered by the standard to
absurd extremes?)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jun 27 '08 #6
Chris Dollin wrote:
christian.bau wrote:
>On Jun 3, 10:46 am, Chris Dollin <chris.dol...@hp.comwrote:
>>The same values apply to the argument of `exit`, but why use
`exit` when you're in `main`?

There is a subtle difference if you used atexit () to register
functions that will be called when the program exits: If you call
exit () from main, local variables of the main function will still
exist when these functions execute; if you use a return statement
then local variables of main are gone.

That's a nice [1] gotcha; I hadn't thought of that. I'm trying to
think of a natural example where it would matter ...

[1] As in "oh noes, my brain just melted".
Maybe one should use return when main has been called recursively and
exit() for a normal termination of main?

Jun 27 '08 #7
santosh <sa*********@gmail.comwrites:
Chris Dollin wrote:
>christian.bau wrote:
>>On Jun 3, 10:46 am, Chris Dollin <chris.dol...@hp.comwrote:

The same values apply to the argument of `exit`, but why use
`exit` when you're in `main`?

There is a subtle difference if you used atexit () to register
functions that will be called when the program exits: If you call
exit () from main, local variables of the main function will still
exist when these functions execute; if you use a return statement
then local variables of main are gone.

That's a nice [1] gotcha; I hadn't thought of that. I'm trying to
think of a natural example where it would matter ...

[1] As in "oh noes, my brain just melted".

Maybe one should use return when main has been called recursively and
exit() for a normal termination of main?
Maybe one shouldn't call main recursively. You can always write a
recursive function and call it from main.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jun 27 '08 #8
Keith Thompson said:
santosh <sa*********@gmail.comwrites:
<snip>
>Maybe one should use return when main has been called recursively and
exit() for a normal termination of main?

Maybe one shouldn't call main recursively. You can always write a
recursive function and call it from main.
Recursive main is one of those techniques that every C programmer should
use occasionally. It's like int delete, or struct T *new, or x =
a//*divide a by b */b, and so on. I'm not saying you should do it too
much, mind you - just... um... now and again.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jun 27 '08 #9
>On Jun 3, 10:46 am, Chris Dollin <chris.dol...@hp.comwrote:
>>The same values apply to the argument of `exit`, but why use
`exit` when you're in `main`?
>christian.bau wrote:
>There is a subtle difference if you used atexit () to register
functions that will be called when the program exits: If you call exit
() from main, local variables of the main function will still exist
when these functions execute; if you use a return statement then local
variables of main are gone.
In article <g2*********@news-pa1.hpl.hp.com>
Chris Dollin <ch**********@hp.comwrote:
>That's a nice [1] gotcha; I hadn't thought of that. I'm trying to
think of a natural example where it would matter ...

[1] As in "oh noes, my brain just melted".
The classic example problem is:

#include <stdio.h>

int main(void) {
char buf[BUFSIZ];

setbuf(stdout, buf);
... do some work ...
return 0;
}

Here, the setbuf() call sets "stdout" (a "global variable" of sorts,
depending on exactly how one defines "global variable") so that it
refers to the local variable "buf" in main(). The local variable
ceases to exist upon return from main(), so when the C system's
startup code does the equivalent of:

exit(main(argc, argv));

and when the exit() call tries to fflush(stdout) -- either because
there is pending output, or because of an active atexit() at that
point that calls printf() and/or fflush() -- things go wrong.

(The solution involves using either a static-duration buffer, or
malloc(), when using setbuf() or setvbuf(). Either way, one gets
a buffer whose storage duration persists until all atexit() handlers
have run and stdout itself is no longer needed.)
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: gmail (figure it out) http://web.torek.net/torek/index.html
Jun 27 '08 #10
Chris Torek wrote:
>>On Jun 3, 10:46 am, Chris Dollin <chris.dol...@hp.comwrote:
The same values apply to the argument of `exit`, but why use
`exit` when you're in `main`?
>>christian.bau wrote:
>>There is a subtle difference if you used atexit () to register
functions that will be called when the program exits: If you call exit
() from main, local variables of the main function will still exist
when these functions execute; if you use a return statement then local
variables of main are gone.

In article <g2*********@news-pa1.hpl.hp.com>
Chris Dollin <ch**********@hp.comwrote:
>>That's a nice [1] gotcha; I hadn't thought of that. I'm trying to
think of a natural example where it would matter ...

[1] As in "oh noes, my brain just melted".

The classic example problem is:

#include <stdio.h>

int main(void) {
char buf[BUFSIZ];

setbuf(stdout, buf);
... do some work ...
return 0;
}
Thanks, Chris; I don't use setbuf often enough to have thought of that ...

--
"I am your new best friend." /Electra City/

Hewlett-Packard Limited registered office: Cain Road, Bracknell,
registered no: 690597 England Berks RG12 1HN

Jun 27 '08 #11
Richard Heathfield wrote:
Keith Thompson said:
>santosh <sa*********@gmail.comwrites:

<snip>
>>Maybe one should use return when main has been called recursively and
exit() for a normal termination of main?

Maybe one shouldn't call main recursively. You can always write a
recursive function and call it from main.

Recursive main is one of those techniques that every C programmer should
use occasionally. It's like int delete, or struct T *new, or x =
a//*divide a by b */b, and so on. I'm not saying you should do it too
much, mind you - just... um... now and again.
And you can stop whenever you want?

--
"The original and modest plans had to be continually extended."/Sector General/

Hewlett-Packard Limited registered no:
registered office: Cain Road, Bracknell, Berks RG12 1HN 690597 England

Jun 27 '08 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

192
by: Kwan Ting | last post by:
The_Sage, I see you've gotten yourself a twin asking for program in comp.lang.c++ . http://groups.google.co.uk/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&th=45cd1b289c71c33c&rnum=1 If you the oh so mighty...
45
by: Steven T. Hatton | last post by:
This is a purely *hypothetical* question. That means, it's /pretend/, CP. ;-) If you were forced at gunpoint to put all your code in classes, rather than in namespace scope (obviously classes...
2
by: fwee | last post by:
What is the difference between using exit(n) and return n in main(), if any? I know that exit(n) is a function that exits and return n exits via a return value, but is there any reason to specify...
5
by: Seong-Kook Shin | last post by:
Hi, I'm reading Steve's "C Programming FAQs" in book version, and have two question regarding to Q11.16 ... Also, a `return' from `main' cannot be expected to work if data local to main might be...
20
by: small TUX | last post by:
Hello programmers; I joinded today itself. Can anyone say about the word main, is it a registered word, justify your question also. I would like to point about one thing that we can make variables...
8
by: Michal Nazarewicz | last post by:
Hi, What does returning 0 from main() mean according to C89/C90 standard? I've found that in C99 it means successful termination (7.20.4.3p5) however as I'm editing book on C at Polish Wikibooks...
9
by: DanielJohnson | last post by:
I am wondering where does the value returned by main goes. Suppoes main returns some number say 42, where is it stored. Supposed a shell script call a C program and the program returns the value...
27
by: junky_fellow | last post by:
Guys, Can I return 0, from main() ? Is this equivalent to exit(EXIT_SUCCESS) ? thanks for any help...
29
by: pereges | last post by:
By main file, I mean the one which contains the main routine. Can some one please provide suggestions as to how I can improve the organization of main file ? I have just though about a rough...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

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.