R
Ravi
void main()
{
main();
}
int main()
{
main();
}
{
main();
}
int main()
{
main();
}
void main()
{
main();
}
int main()
{
main();
}
But i have read that C can have only one main function, so is this
valid?
Please always include the context of your message.Ravi said:But i have read that C can have only one main function, so is this
valid?
Eric said:Ravi wrote On 03/31/06 12:03,:
This example will execute forever, provided the machine
running it has infinite resources. Less powerful machines
may be unable to run this program to completion ...
void main()
{
main();
}
int main()
{
main();
}
This example will execute forever, provided the machine
running it has infinite resources. Less powerful machines
may be unable to run this program to completion ...
I don't beleive any machine can run this to completion since the
program operates recursively forever.
GCC with -std=c99 will not issue a warning, because in C99 it's legal toAlso this should cause a compiler warning about no return statement in a
non void function, at least on any compiler I have ever used.
Skarmander said:GCC with -std=c99 will not issue a warning, because in C99 it's legal
to omit the return statement from main() (and *only* main()), in which
case a return value of 0 is implied.
I don't think I've yet read why this was considered a good idea; maybe
to retroactively fix all the broken code that invoked undefined
behavior.
Yes, true. Although some calling environments will get quite upset if youKeith said:Omitting the return statement in main() never invoked undefined
behavior (unless a recursive call to main() attempts to use the
result). At worst, it merely returns an unspecified status to the
calling environment, and the behavior of the calling environment is
outside the scope of the C standard.
That's... interesting. I suppose it really doesn't matter much, as it's aI think part of the motivation for the change is that made some of the
examples in K&R retroactively valid.
Also this should cause a compiler
warning about no return statement in a non void function, at least on
any compiler I have ever used.
Also this should cause a compiler
warning about no return statement in a non void function, at least on
any compiler I have ever used.
Actually, I would be impressed if the compiler turned it into a self-jumpKeith said:A sufficiently good compiler would translate the program to a single
instruction that does the equivalent of a stack overflow.
Actually, I would be impressed if the compiler turned it into a self-jump
instead. Optimizing tail recursion is not very hard, but it generally
doesn't pay off in imperative languages, so you don't see many C compilers
doing it.
Of course, an even more efficient compiler would use the equivalent of
HLT, to save processor cycles.
Also this should cause a compiler
warning about no return statement in a non void function, at least on
any compiler I have ever used.
Actually, this isn't just very hard, it's impossible in general.I can't get a warning out of my compilers for the following:
int main()
{
main();
return 0;
}
However I do get warning for the original one with no return statement.
I would think that in general irt would be very hard for the compiler
to recognize that recursion is going to continue forever.
Ravi said:But i have read that C can have only one main function, so is this
valid?
void main()
{
main();
}
int main()
{
main();
}
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.