void main()
{
main();
}
int main()
{
main();
} 17 6774
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
But i have read that C can have only one main function, so is this
valid?
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.
"Ravi" <ra***********@gmail.com> wrote in message
news:11**********************@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
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
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.learn.c-c++ http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html 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. sw***********@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.
Skarmander <in*****@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) 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.
Keith Thompson wrote: Skarmander <in*****@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.
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.
In article <11**********************@i40g2000cwc.googlegroups .com>, sw***********@gmail.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 ri*****@cogsci.ed.ac.uk (Richard Tobin) writes: In article <11**********************@i40g2000cwc.googlegroups .com>, sw***********@gmail.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_Keith) 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.
Keith Thompson wrote: ri*****@cogsci.ed.ac.uk (Richard Tobin) writes: In article <11**********************@i40g2000cwc.googlegroups .com>, sw***********@gmail.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.
On 2006-03-31, Skarmander <in*****@dontmailme.com> wrote: Keith Thompson wrote: ri*****@cogsci.ed.ac.uk (Richard Tobin) writes: In article <11**********************@i40g2000cwc.googlegroups .com>, sw***********@gmail.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.
Richard Tobin wrote: In article <11**********************@i40g2000cwc.googlegroups .com>, sw***********@gmail.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. sw***********@gmail.com wrote: Richard Tobin wrote: In article <11**********************@i40g2000cwc.googlegroups .com>, sw***********@gmail.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.
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> This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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;
}
|
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...
|
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 ,...
|
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...
|
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)...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
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...
|
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,...
|
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...
|
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...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
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...
| |