473,725 Members | 2,127 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
17 6801
Keith Thompson wrote:
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.

Yes, true. Although some calling environments will get quite upset if you
don't return a decent value from main(), the hissy fit they throw is not
undefined behavior.

I'm confusing this with declaring main as returning void, which is a lot worse.
I think part of the motivation for the change is that made some of the
examples in K&R retroactively valid.

That's... interesting. I suppose it really doesn't matter much, as it's a
minor thing. But still, a peculiar addition.

S.
Mar 31 '06 #11
In article <11************ **********@i40g 2000cwc.googleg roups.com>,
sw***********@g mail.com <sw***********@ gmail.com> wrote:
> int main()
> {
> main();
> }
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.


Since the recursive call to main() never returns, the end of the
function cannot be reached, so a return statement would be
superfluous. Indeed, a sufficiently good compiler would warn if
you did have one!

-- Richard
Mar 31 '06 #12
ri*****@cogsci. ed.ac.uk (Richard Tobin) writes:
In article <11************ **********@i40g 2000cwc.googleg roups.com>,
sw***********@g mail.com <sw***********@ gmail.com> wrote:
> int main()
> {
> main();
> }

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.


Since the recursive call to main() never returns, the end of the
function cannot be reached, so a return statement would be
superfluous. Indeed, a sufficiently good compiler would warn if
you did have one!


A sufficiently good compiler would translate the program to a single
instruction that does the equivalent of a stack overflow.

--
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 #13
Keith Thompson wrote:
ri*****@cogsci. ed.ac.uk (Richard Tobin) writes:
In article <11************ **********@i40g 2000cwc.googleg roups.com>,
sw***********@g mail.com <sw***********@ gmail.com> wrote:
> int main()
> {
> main();
> }
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.

Since the recursive call to main() never returns, the end of the
function cannot be reached, so a return statement would be
superfluous. Indeed, a sufficiently good compiler would warn if
you did have one!


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.

S.
Mar 31 '06 #14
On 2006-03-31, Skarmander <in*****@dontma ilme.com> wrote:
Keith Thompson wrote:
ri*****@cogsci. ed.ac.uk (Richard Tobin) writes:
In article <11************ **********@i40g 2000cwc.googleg roups.com>,
sw***********@g mail.com <sw***********@ gmail.com> wrote:

>> int main()
>> {
>> main();
>> }
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.
Since the recursive call to main() never returns, the end of the
function cannot be reached, so a return statement would be
superfluous. Indeed, a sufficiently good compiler would warn if
you did have one!


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.


Or just have it exit after a set time. Would be really impressive on
benchmarks.
Mar 31 '06 #15

Richard Tobin wrote:
In article <11************ **********@i40g 2000cwc.googleg roups.com>,
sw***********@g mail.com <sw***********@ gmail.com> wrote:
> int main()
> {
> main();
> }

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.


Since the recursive call to main() never returns, the end of the
function cannot be reached, so a return statement would be
superfluous. Indeed, a sufficiently good compiler would warn if
you did have one!

-- Richard


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.

Apr 1 '06 #16
sw***********@g mail.com wrote:
Richard Tobin wrote:
In article <11************ **********@i40g 2000cwc.googleg roups.com>,
sw***********@g mail.com <sw***********@ gmail.com> wrote:
> int main()
> {
> main();
> }
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.

Since the recursive call to main() never returns, the end of the
function cannot be reached, so a return statement would be
superfluous. Indeed, a sufficiently good compiler would warn if
you did have one!

-- Richard


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.

Actually, this isn't just very hard, it's impossible in general.
http://en.wikipedia.org/wiki/Halting_problem

S.
Apr 1 '06 #17
Ravi wrote:

But i have read that C can have only one main function, so is this
valid?
Please see the other posts about how to properly reply using Google's
interface.

Here is the code from your original post:
void main()
{
main();
}

int main()
{
main();
}


If you mean to have both of these functions in one file, then this is
not valid. However, this would be true regardless of the name of the
function.

--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer .h> |
+-------------------------+--------------------+-----------------------------+
Don't e-mail me at: <mailto:Th***** ********@gmail. com>

Apr 1 '06 #18

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

Similar topics

4
2932
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
2548
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
7847
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
2649
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
3501
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
40025
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
2223
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
2113
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
3359
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
8888
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
8752
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9401
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9176
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,...
1
6702
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
4519
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
4784
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2635
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2157
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.