main exit/return

R

Rahul

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 ! ! !
 
C

Chris Dollin

Rahul said:
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`?
 
C

christian.bau

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.
 
V

vippstar

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/>
 
C

Chris Dollin

christian.bau said:
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".
 
K

Keith Thompson

Chris Dollin said:
christian.bau said:
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?)
 
S

santosh

Chris said:
christian.bau said:
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?
 
K

Keith Thompson

santosh said:
Chris said:
christian.bau said:
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.
 
C

Chris Torek

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.)
 
C

Chris Dollin

Chris said:
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 ...
 
C

Chris Dollin

Richard said:
Keith Thompson said:


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?
 

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
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top