473,698 Members | 1,985 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

What will happen if main called in side main function?

void main()
{
main();
}

int main()
{
main();
}

Mar 31 '06 #1
17 6796


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

--
Er*********@sun .com

Mar 31 '06 #2
But i have read that C can have only one main function, so is this
valid?

Mar 31 '06 #3
On 2006-03-31, Ravi <ra***********@ 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.
Mar 31 '06 #4

"Ravi" <ra***********@ gmail.com> wrote in message
news:11******** **************@ i39g2000cwa.goo glegroups.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
Mar 31 '06 #5
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
Mar 31 '06 #6
On 31 Mar 2006 09:03:16 -0800, "Ravi" <ra***********@ 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.l earn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Mar 31 '06 #7
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.

Mar 31 '06 #8
sw***********@g mail.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.
Mar 31 '06 #9
Skarmander <in*****@dontma ilme.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_Keit h) ks***@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.
Mar 31 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
2931
by: F. Da Costa | last post by:
Hi, I was wondering whether someone could enlighten me as to the reason why the slice does not work in IE when the arr is passed in properly. Checked the values in the srcArr and they are correct so no problems there. Gecko works as expected. Prior to entering the function I can slice the array being entered so I wouldn't expect an "Unexpected call to method or property access" (in IE 6). I guess its something silly but as of yet i'm...
11
2535
by: modemer | last post by:
If I define the following codes: void f(const MyClass & in) {cout << "f(const)\n";} void f(MyClass in) {cout<<"f()\n";} MyClass myclass; f(myclass); Compiler complain that it can't find the best match. Anyone could give a detail explanation in theory? Which one is good?
140
7833
by: Oliver Brausch | last post by:
Hello, have you ever heard about this MS-visual c compiler bug? look at the small prog: static int x=0; int bit32() { return ++x; }
6
2648
by: Niklaus | last post by:
Hi, Can someone point out what is wrong with this code ? How can i make it better optimize it. When run it gives me seg fault in linux. But windows it works fine(runs for a long time). Do we have something like stack size growing enormously and then saying you can't access ,so a segfault ? It would be helpful if someone can run the code and give me the output. It takes a long time on my PC.
17
3496
by: Anoob | last post by:
Can we consider () unary operator when calling a function, in exps eq. f(), ( 1 + 2). But when we call function f( 10 ) it is a binary operator. Even if we pass f( 10, 20) as we are using , operator there one operand will be there for ()? And Unary operators like !, +, -, ~, ... are said to be having associativity right to left which means that we can write, (but not allowed) 1. 2! 2. !2
4
40015
by: Rachel Suddeth | last post by:
What is the difference between a managed/unmanaged resource, and how do you tell which is which? I'm trying to understand how to write some Dispose() methods, and we are supposed to put code that deals with managed in one place, and code that deals with unmanaged in another place, but I can't seem to find anything that clearly explains what that means. I think if I used a Windows API function to optain a handle, that handle would be an...
9
2221
by: AFN | last post by:
I was just dropped into someone else's code (isn't that always so fun?). I can't figure out why a custom validation control's server event function is executing. There is nothing (that I see) in page_load, or elsewhere, that says page.validate, no control says "causesvalidation=true", and the AutoEventWireup is set to false. So I would think that the control's server event function would NOT execute, but it does execute right after...
17
2106
by: Grizlyk | last post by:
Hello. What can be optimised in C++ code and how i can garantee stable behaviour below 1. Are expression "auto volatile" can deny removing as "unused temporary" like this: auto volatile const class_name tmp;
11
3351
by: venkatagmail | last post by:
I have problem understanding pass by value and pass by reference and want to how how they are or appear in the memory: I had to get my basics right again. I create an array and try all possible ways of passing an array. In the following code, fun1(int a1) - same as fun1(int* a1) - where both are of the type passed by reference. Inside this function, another pointer a1 is created whose address &a1 is different from that of the passed...
0
8674
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9023
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8893
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8861
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6518
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5860
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4366
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4615
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2327
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.