main prototype pointless?

Z

Zach

I was just reading "Advanced C" and one of the example programs I
looked at had main prototyped:

int main void();

int main()
{
....
}

Is there *any* reason to do this or is it just a waste of time?

What do the C standards have to say about it?

Can anyone think of a case when you would want to prototype main()?

Zach
 
G

Guest

I was just reading "Advanced C" and one of the example programs I
looked at had main prototyped:

int main void();

I really hope it didn't say that...

int main()
{
...

}

Is there *any* reason to do this or is it just a waste of time?

it's a waste of time. What they should do is this

int main (void)
{
}

as that is both a definition and a protoype
What do the C standards have to say about it?

roughly what I said but in a far more elegant and humourous way
Can anyone think of a case when you would want to prototype main()?
maybe if you had some bizzare recursive or mutual recursive
call of main you might need a separate prototype

int main (void);

int main_caller (int n)
{
if (n > 0)
main();
}

int main(void)
{
static int i = 2;
i--;
main_caller (i);
return 0;
}

but that sort of stuff is only fit for the OCC


--
Nick Keighley

"It is a good rule in life never to apologize. The right sort of
people do not want apologies, and the wrong sort take a mean
advantage from them." - P. G. Wodehouse
 
J

jameskuyper

Zach said:
I was just reading "Advanced C" and one of the example programs I
looked at had main prototyped:

int main void();

As written, this is a non-prototype declaration that is permitted only
if both 'main' and 'void' are macros #defined to be something quite
different from what they appear to be. I presume you intended

int main(void);
int main()
{
...
}

Is there *any* reason to do this or is it just a waste of time?

What do the C standards have to say about it?

I have vague memories that prior declarations main() are prohibited,
but I couldn't find any such prohibition. It's therefore a perfectly
ordinary declaration, and therefore allowed. Whether or not it's a
good idea is not the kind fo thing the standard normally discusses.
Can anyone think of a case when you would want to prototype main()?

I think it's a good idea to prototype all functions, even main(), if
only for the same of consistency. However, I think you're actually
asking a slightly different question. I think you're asking about
providing a seperate prototype declaration for main() prior to the one
that should start the definiton of main.

If your program calls main() from other translation units, it should
pre-declare it in a seperate header, which is #defined in all
translation units where main() is either defined or used. If you call
it only in the same translation unit, but prior to the definition of
main(), you must also pre-declare main(). However, in such a case, I
would prefer to avoid the need for the seperate declaration by making
the defintion of main() itselt a prototype, prior to the first call to
main(). In all other cases, a prior declaration is pointless.

However, I doubt that this is the reason for the code you've seen. I
suspect that it's meant to shut up a warning from a compiler.

Many good compilers will give you a warning about defining a non-
static function without already having a previous declaration in
scope. This is a good thing, in general; if a function is defined or
used in two or more tranlsation units, a declaration of that function
should be put into a header file and shared between all of those
translation units using #Include. This will give you a warning message
in the event that there's any discrepancy between the declaration and
either the definition of the function or the uses of the function.

If a function will only be used in one translation unit, it should be
declared static. In some cases this will enable some better
optimiizations; in fact, it's been argued that 'inline' is redundant,
because static is sufficient to enable the same optimizations allowed
by 'inline', in almost every case where such optimizations are
desireable. However, static also enables the compiler to warn you if
you make no use of the function anywhere in the same translation unit
where it was defined; for static functions, any such function
definition is pointless, and probably an error.

main() is an exception to the above rules. However, I believe that
some of the compilers that provide this warning don't make an
exception for main().
 
N

Nelu

I was just reading "Advanced C" and one of the example programs I looked
at had main prototyped:

int main void();

This is not right. It should be: int main(void);
int main()
{
...
}

Is there *any* reason to do this or is it just a waste of time?

I'm not sure there's any reason to do that. Just write:
int main(void) {
...
}
which both declares and defines the function.
What do the C standards have to say about it?

I'm not sure but I don't think they disallow it.
Can anyone think of a case when you would want to prototype main()?

You may want to do that if you prototype all your functions, as a habit.
But, given the fact that main is the entry point in your application and
it's not likely that you would call it like you would call any other
function, I doubt there's a good reason to do that.
 
K

Kevin D. Quitt

What do the C standards have to say about it?

ISO/IEC 9899:1999 (E) ©ISO/IEC
5.1.2.2.1 Program startup
....It shall be defined...

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

Can anyone think of a case when you would want to prototype main()?

ibid. The implementation declares no prototype for this function.
 
J

jameskuyper

Kevin said:
What do the C standards have to say about it?

ISO/IEC 9899:1999 (E) �ISO/IEC
5.1.2.2.1 Program startup
...It shall be defined...

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

Can anyone think of a case when you would want to prototype main()?

ibid. The implementation declares no prototype for this function.

The question was a prototype declaration provided by the user code,
not by the implementation.
 

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,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top