Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > What will happen if main called in side main function?

Reply
Thread Tools

What will happen if main called in side main function?

 
 
Ravi
Guest
Posts: n/a
 
      03-31-2006
void main()
{
main();
}

int main()
{
main();
}

 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      03-31-2006


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

--
http://www.velocityreviews.com/forums/(E-Mail Removed)

 
Reply With Quote
 
 
 
 
Ravi
Guest
Posts: n/a
 
      03-31-2006
But i have read that C can have only one main function, so is this
valid?

 
Reply With Quote
 
Jordan Abel
Guest
Posts: n/a
 
      03-31-2006
On 2006-03-31, Ravi <(E-Mail Removed)> wrote:
> But i have read that C can have only one main function, so is this
> valid?


But a function can call itself.
 
Reply With Quote
 
Fred Kleinschmidt
Guest
Posts: n/a
 
      03-31-2006

"Ravi" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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


 
Reply With Quote
 
Flash Gordon
Guest
Posts: n/a
 
      03-31-2006
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
 
Reply With Quote
 
Jack Klein
Guest
Posts: n/a
 
      03-31-2006
On 31 Mar 2006 09:03:16 -0800, "Ravi" <(E-Mail Removed)> 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
 
Reply With Quote
 
swengineer001@gmail.com
Guest
Posts: n/a
 
      03-31-2006

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

 
Reply With Quote
 
Skarmander
Guest
Posts: n/a
 
      03-31-2006
(E-Mail Removed) 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.
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      03-31-2006
Skarmander <(E-Mail Removed)> 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) (E-Mail Removed) <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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
What would happen if your destructors aren't get called? U.Mutlu C++ 14 02-07-2012 09:32 AM
main() called inside main() rahulsinner@gmail.com C Programming 14 05-07-2006 10:23 PM
how to run procedure in server side, which will be called from client side aa ASP .Net 1 03-08-2005 02:54 PM
three times copy ctor called, one ctor called, why? Apricot C++ 4 04-16-2004 07:55 AM



Advertisments