Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   What will happen if main called in side main function? (http://www.velocityreviews.com/forums/t442081-what-will-happen-if-main-called-in-side-main-function.html)

Ravi 03-31-2006 05:03 PM

What will happen if main called in side main function?
 
void main()
{
main();
}

int main()
{
main();
}


Eric Sosman 03-31-2006 05:14 PM

Re: What will happen if main called in side main function?
 


Ravi wrote On 03/31/06 12:03,:
> void main()
> {
> main();
> }


This example is incorrect because main() returns an
`int'; it is not a `void' function. The C language does
not define what will happen when you mis-define main()
this way; however, on many implementations the effect
will be similar to the second example.

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

--
Eric.Sosman@sun.com


Ravi 03-31-2006 05:36 PM

Re: What will happen if main called in side main function?
 
But i have read that C can have only one main function, so is this
valid?


Jordan Abel 03-31-2006 06:04 PM

Re: What will happen if main called in side main function?
 
On 2006-03-31, Ravi <ravi.wireless@gmail.com> wrote:
> But i have read that C can have only one main function, so is this
> valid?


But a function can call itself.

Fred Kleinschmidt 03-31-2006 06:44 PM

Re: What will happen if main called in side main function?
 

"Ravi" <ravi.wireless@gmail.com> wrote in message
news:1143826569.981525.100270@i39g2000cwa.googlegr oups.com...
> But i have read that C can have only one main function, so is this
> valid?
>

Please always include the context of your message.

int main() {
main();
return 0;
}

There is only one instance of main() defined. It just happens to recursively
call itself.
Forever.

This, on the other hand, will eventually stop:

int num = 10;
int main() {
if ( --num > 0 ) {
main();
}
return 0;
}
--
Fred L. Kleinschmidt
Boeing Associate Technical Fellow
Technical Architect, Software Reuse Project



Flash Gordon 03-31-2006 06:50 PM

Re: What will happen if main called in side main function?
 
Eric Sosman wrote:
>
> Ravi wrote On 03/31/06 12:03,:


<snip>

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


A slightly better implementation without infinite resources might use
tail recursion optimisation and convert it in to a simple loop that only
terminates when the machine itself is terminated.
--
Flash Gordon, living in interesting times.
Web site - http://home.flash-gordon.me.uk/
comp.lang.c posting guidelines and intro:
http://clc-wiki.net/wiki/Intro_to_clc

Jack Klein 03-31-2006 07:12 PM

Re: What will happen if main called in side main function?
 
On 31 Mar 2006 09:03:16 -0800, "Ravi" <ravi.wireless@gmail.com> wrote
in comp.lang.c:

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


It seems apparent from a later message you posted in the thread, you
are asking if the above is a valid program. No, it is not, you cannot
have two functions with external linkage and the same name in a
program. And you cannot have two functions with the same name in a
single translation unit, regardless of linkage.

And main() must be defined with a return type of int.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html

swengineer001@gmail.com 03-31-2006 08:43 PM

Re: What will happen if main called in side main function?
 

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


Skarmander 03-31-2006 09:23 PM

Re: What will happen if main called in side main function?
 
swengineer001@gmail.com wrote:
>>> 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.


I do believe the person you failed to attribute was trying to make a joke.
The standard form would be "the new X is so fast, it can execute an infinite
loop in under Y seconds".

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

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.

S.

Keith Thompson 03-31-2006 10:16 PM

Re: What will happen if main called in side main function?
 
Skarmander <invalid@dontmailme.com> writes:
[...]
> 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.


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.

I think part of the motivation for the change is that made some of the
examples in K&R retroactively valid.

The Rationale (C99RationaleV5.10.pdf) doesn't mention this change as
far as I can tell.

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.


All times are GMT. The time now is 11:00 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.