473,785 Members | 2,354 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2848
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...@h p.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...@h p.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**********@h p.comwrites:
christian.bau wrote:
>On Jun 3, 10:46 am, Chris Dollin <chris.dol...@h p.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_Keit h) 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...@h p.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*********@gm ail.comwrites:
Chris Dollin wrote:
>christian.ba u wrote:
>>On Jun 3, 10:46 am, Chris Dollin <chris.dol...@h p.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_Keit h) 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*********@gm ail.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...@h p.comwrote:
>>The same values apply to the argument of `exit`, but why use
`exit` when you're in `main`?
>christian.ba u 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*********@ne ws-pa1.hpl.hp.com>
Chris Dollin <ch**********@h p.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

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

Similar topics

192
9009
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 programmer that you pretend to be, why don't you just write some? (And oh, void main is still not allow by the C++ standard.) Seeming as how you tried to quote the standard in an attempt to pretend you're right, I'll quote the standard to once...
45
3627
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 themselves are an exception to this), and 'bootstrap' your program by instantiating a single application object in main(), would that place any limitations on what you could accomplish with your program? Are there any benefits to doing things that...
2
10999
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 one rather than the other? Thanks!
5
2811
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 needed during cleanup. (Finally the two forms are obviously not equivalent in a recursive call to `main'). My questions are
20
1468
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 with name main. Thanks.
8
2744
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 I'd like to know what C89/C90 says about it - quotation from C89 or C90 would be nice. Moreover, in C99 main() function is somewhat special because lack of return statement is equivalent with returning zero (5.1.2.2.3p1). Is it also the case...
9
3722
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 42 where is this value stored in memory ?
27
2508
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
2044
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 skeleton. In my ray tracing project, I have to carry out following task (in sequence) 1. Read the mesh from an ascii file and store it in the mesh data structure. 2. Create a ray list and store it in a ray list.
0
9646
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10350
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10157
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10097
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8983
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7505
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6742
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5518
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2887
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.