Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #67346 > unrolled thread

Can main() be called by itself?

Started byfl <rxjwg98@gmail.com>
First post2015-08-13 16:51 -0700
Last post2015-08-14 12:32 +0000
Articles 13 — 10 participants

Back to article view | Back to comp.lang.c


Contents

  Can main() be called by itself? fl <rxjwg98@gmail.com> - 2015-08-13 16:51 -0700
    Re: Can main() be called by itself? Melzzzzz <mel@zzzzz.com> - 2015-08-14 01:58 +0200
    Re: Can main() be called by itself? James Kuyper <jameskuyper@verizon.net> - 2015-08-13 23:32 -0400
      Re: Can main() be called by itself? Keith Thompson <kst-u@mib.org> - 2015-08-14 11:14 -0700
    Re: Can main() be called by itself? Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2015-08-13 22:14 -0600
      Re: Can main() be called by itself? Keith Thompson <kst-u@mib.org> - 2015-08-14 11:15 -0700
    Re: Can main() be called by itself? gordonb.mj8yu@burditt.org (Gordon Burditt) - 2015-08-14 01:52 -0500
      Re: Can main() be called by itself? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-08-14 11:46 +0100
        Re: Can main() be called by itself? James Kuyper <jameskuyper@verizon.net> - 2015-08-14 07:54 -0400
          Re: Can main() be called by itself? Melzzzzz <mel@zzzzz.com> - 2015-08-14 14:19 +0200
            Re: Can main() be called by itself? David Brown <david.brown@hesbynett.no> - 2015-08-14 14:50 +0200
              Re: Can main() be called by itself? Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-08-16 10:39 -0700
        Re: Can main() be called by itself? gazelle@shell.xmission.com (Kenny McCormack) - 2015-08-14 12:32 +0000

#67346 — Can main() be called by itself?

Fromfl <rxjwg98@gmail.com>
Date2015-08-13 16:51 -0700
SubjectCan main() be called by itself?
Message-ID<39b9ef39-3874-4983-8165-035b51b78827@googlegroups.com>
Hi,

Recently, I had a quiz onsite. It looks like below, but it is not exactly (I
cannot remember its exact form now).



int main(int a)
{
int temp;

	temp = main(3);

	return temp;

}


In the exam, I thought main() can call itself, and gave a value to the answer.

Now, the above main() can build, but it has stack overflow when running at 
MSVC. So, could you experts think main() can call itself?
And if it is Yes, can you give me a correct form to replace the above code?


Thanks a lot.

[toc] | [next] | [standalone]


#67347

FromMelzzzzz <mel@zzzzz.com>
Date2015-08-14 01:58 +0200
Message-ID<20150814015809.636b77cf@maxa-pc>
In reply to#67346
On Thu, 13 Aug 2015 16:51:28 -0700 (PDT)
fl <rxjwg98@gmail.com> wrote:

> Hi,
> 
> Recently, I had a quiz onsite. It looks like below, but it is not
> exactly (I cannot remember its exact form now).
> 
> 
> 
> int main(int a)
> {
> int temp;
> 
> 	temp = main(3);
> 
> 	return temp;
> 
> }
> 
> 
> In the exam, I thought main() can call itself, and gave a value to
> the answer.
> 
> Now, the above main() can build, but it has stack overflow when
> running at MSVC. So, could you experts think main() can call itself?
> And if it is Yes, can you give me a correct form to replace the above
> code?
> 
> 
> Thanks a lot.

You have infinite recursion and wrong prototype.
Problem is that I don't know if main itself is allowed to set up
argc/argv parameters. If this is so main cannot be called like
ordinary function.

[toc] | [prev] | [next] | [standalone]


#67350

FromJames Kuyper <jameskuyper@verizon.net>
Date2015-08-13 23:32 -0400
Message-ID<mqjne6$o1$1@dont-email.me>
In reply to#67346
On 08/13/2015 07:51 PM, fl wrote:
> Hi,
> 
> Recently, I had a quiz onsite. It looks like below, but it is not exactly (I
> cannot remember its exact form now).
> 
> 
> 
> int main(int a)

main() has two standard forms, and implementations are only required to
accept it if defined with one of those two forms, or an equivalent. This
isn't one of those forms.

> {
> int temp;
> 
> 	temp = main(3);
> 
> 	return temp;

"temp" serves no useful purpose, you could just as well write
	return main(3);

Note that you've define an infinitely recursive function.

> }
> 
> 
> In the exam, I thought main() can call itself, and gave a value to the answer.
> 
> Now, the above main() can build, but it has stack overflow when running at 
> MSVC. So, could you experts think main() can call itself?

That's perfectly normal for an infinite recursion.

> And if it is Yes, can you give me a correct form to replace the above code?

Here's an example that uses recursion to achieve what could just as
easily have been done with a loop:

#include <stdio.h>

int main(int argc, char *argv[])
{
    if(argv[0])
    {
        printf("%s ", argv[0]);
        return main(argc-1, argv+1);
    }
    putchar('\n');
    return 0;
}


-- 
James Kuyper

[toc] | [prev] | [next] | [standalone]


#67412

FromKeith Thompson <kst-u@mib.org>
Date2015-08-14 11:14 -0700
Message-ID<lnlhddagam.fsf@kst-u.example.com>
In reply to#67350
James Kuyper <jameskuyper@verizon.net> writes:
> On 08/13/2015 07:51 PM, fl wrote:
>> Recently, I had a quiz onsite. It looks like below, but it is not exactly (I
>> cannot remember its exact form now).
>> 
>> int main(int a)
>
> main() has two standard forms, and implementations are only required to
> accept it if defined with one of those two forms, or an equivalent. This
> isn't one of those forms.

main() may also be defined using some other implementation-defined form
(meaning the implementation must document it).  If an implementation's
documentation says that it accepts the above form, then it behaves in a
manner specified by the implementation.  If not, the behavior is
undefined.

In at least some implementations, though the behavior is not defined
either by the standard or by the implementation, will run normally, with
`a` initialized to the value that would have been stored in `argc`.

Defining it as:

    int main(int a, char **v)

would be valid (the names of the parameters are not mandated), and would
avoid several paragraphs of nitpicking.

[...]

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

[toc] | [prev] | [next] | [standalone]


#67352

FromJoe Pfeiffer <pfeiffer@cs.nmsu.edu>
Date2015-08-13 22:14 -0600
Message-ID<1b7foy7bhj.fsf@pfeifferfamily.net>
In reply to#67346
fl <rxjwg98@gmail.com> writes:

> Hi,
>
> Recently, I had a quiz onsite. It looks like below, but it is not exactly (I
> cannot remember its exact form now).
>
>
>
> int main(int a)
> {
> int temp;
>
> 	temp = main(3);
>
> 	return temp;
>
> }
>
>
> In the exam, I thought main() can call itself, and gave a value to the answer.
>
> Now, the above main() can build, but it has stack overflow when running at 
> MSVC. So, could you experts think main() can call itself?
> And if it is Yes, can you give me a correct form to replace the above code?

Yes, it can.

Without knowing what your code is supposed to do, I can't tell you how
to make it do it.  But the big problem with this code is that its
recursion is infinite -- it will call itself, which will call itself,
which will.... until you run out of memory.  Something similar that
won't do anything any more useful than your code, but will terminate,
would be

// we'll just use the number of parameters on the command line as the
// depth of recursion
#include <stdio.h>
int main(int argc, char *argv[])
{
   printf("argc is %d\n", argc);
   
   if (argc == 0)
       return 0;

   return main(argc-1, NULL);
}

snowball:1010$ gcc -o bogus bogus.c
snowball:1011$ bogus a b c d
argc is 5
argc is 4
argc is 3
argc is 2
argc is 1
argc is 0

[toc] | [prev] | [next] | [standalone]


#67413

FromKeith Thompson <kst-u@mib.org>
Date2015-08-14 11:15 -0700
Message-ID<lnh9o1ag8g.fsf@kst-u.example.com>
In reply to#67352
Joe Pfeiffer <pfeiffer@cs.nmsu.edu> writes:
[...]
> Without knowing what your code is supposed to do, I can't tell you how
> to make it do it.  But the big problem with this code is that its
> recursion is infinite -- it will call itself, which will call itself,
> which will.... until you run out of memory.
[...]

With an implementation that does sufficiently clever tail-call
optimization, it might merely be an infinite loop that does not consume
unbounded memory.

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

[toc] | [prev] | [next] | [standalone]


#67356

Fromgordonb.mj8yu@burditt.org (Gordon Burditt)
Date2015-08-14 01:52 -0500
Message-ID<hfGdndgRHve0DVDInZ2dnUU7-b2dnZ2d@posted.internetamerica>
In reply to#67346
> Recently, I had a quiz onsite. 

On what site?  Are you taking an online C course?

> It looks like below, but it is not exactly (I
> cannot remember its exact form now).
 
> int main(int a)

The appropriate function signatures for main() are:
	int main(void)
OR
	int main(int argc, char **argv)
and the above isn't equivalent to one of them.

> {
> int temp;
> 
>         temp = main(3);
> 
>         return temp;
			/* the above line of code is unreachable */
> 
> }
> 
> 
> In the exam, I thought main() can call itself, and gave a value to the answer.

As others have pointed out, infinite recursion will not complete
unless it hits an exception that kills the program, such as stack
overflow (and possibly scribbling on parts of the program beyond
the stack), running out of memory, or dereferencing NULL because
you failed to check the return value of malloc() before using the
memory you failed to allocate.

> Now, the above main() can build, but it has stack overflow when running at 
> MSVC. So, could you experts think main() can call itself?

Yes, main() can call itself conditionally, but it has to eventually
reach a point where it does not make deeper calls.  Sometimes the limit
for hitting stack overflow is relatively small, like 1,000 levels.

> And if it is Yes, can you give me a correct form to replace the above code?

Please send me your instructor's email address, so I can send it
directly.

[toc] | [prev] | [next] | [standalone]


#67380

FromBen Bacarisse <ben.usenet@bsb.me.uk>
Date2015-08-14 11:46 +0100
Message-ID<87d1yqywoy.fsf@bsb.me.uk>
In reply to#67356
gordonb.mj8yu@burditt.org (Gordon Burditt) writes:

>> Recently, I had a quiz onsite.
<snip>
>> It looks like below, but it is not exactly (I
>> cannot remember its exact form now).
>  
>> int main(int a)
>
> The appropriate function signatures for main() are:
> 	int main(void)
> OR
> 	int main(int argc, char **argv)
> and the above isn't equivalent to one of them.

Right, but an implementation can define others, so there is no reason to
reject the question on this basis.  (I know you are not doing that.  I'm
just clearing up a detail.)

<snip>
-- 
Ben.

[toc] | [prev] | [next] | [standalone]


#67387

FromJames Kuyper <jameskuyper@verizon.net>
Date2015-08-14 07:54 -0400
Message-ID<mqkkqm$m65$1@dont-email.me>
In reply to#67380
On 08/14/2015 07:04 AM, Stefan Ram wrote:
> Ben Bacarisse <ben.usenet@bsb.me.uk> writes:
>> Right, but an implementation can define others, so there is no reason to
>> reject the question on this basis.  (I know you are not doing that.  I'm
>> just clearing up a detail.)
> 
>   In C, main may call itself. In C++, this has been forbidden.

In C++, the rule is more general: no part of your program can call
main(). In C, any part can.
-- 
James Kuyper

[toc] | [prev] | [next] | [standalone]


#67389

FromMelzzzzz <mel@zzzzz.com>
Date2015-08-14 14:19 +0200
Message-ID<20150814141959.49a369c4@maxa-pc>
In reply to#67387
On Fri, 14 Aug 2015 07:54:33 -0400
James Kuyper <jameskuyper@verizon.net> wrote:

> On 08/14/2015 07:04 AM, Stefan Ram wrote:
> > Ben Bacarisse <ben.usenet@bsb.me.uk> writes:
> >> Right, but an implementation can define others, so there is no
> >> reason to reject the question on this basis.  (I know you are not
> >> doing that.  I'm just clearing up a detail.)
> > 
> >   In C, main may call itself. In C++, this has been forbidden.
> 
> In C++, the rule is more general: no part of your program can call
> main(). In C, any part can.

Hm that has implication of how CRT initializes. In C main has to be
ordinary function, while in C++ main can perform run time initialization
of program.

[toc] | [prev] | [next] | [standalone]


#67395

FromDavid Brown <david.brown@hesbynett.no>
Date2015-08-14 14:50 +0200
Message-ID<mqko2v$2h7$1@dont-email.me>
In reply to#67389
On 14/08/15 14:19, Melzzzzz wrote:
> On Fri, 14 Aug 2015 07:54:33 -0400
> James Kuyper <jameskuyper@verizon.net> wrote:
> 
>> On 08/14/2015 07:04 AM, Stefan Ram wrote:
>>> Ben Bacarisse <ben.usenet@bsb.me.uk> writes:
>>>> Right, but an implementation can define others, so there is no
>>>> reason to reject the question on this basis.  (I know you are not
>>>> doing that.  I'm just clearing up a detail.)
>>>
>>>   In C, main may call itself. In C++, this has been forbidden.
>>
>> In C++, the rule is more general: no part of your program can call
>> main(). In C, any part can.
> 
> Hm that has implication of how CRT initializes. In C main has to be
> ordinary function, while in C++ main can perform run time initialization
> of program.
> 

I believe that in most C++ implementations, run-time initialisation
(such as setting up initialised data, running global constructors, and
possibly running static constructors) is done before main() is called.
However, I have seen implementations where the global constructors are
called by a hidden loop added by the compiler at the start of main().
As far as I know both methods are valid since it is illegal to call
main() from elsewhere in the code.

Another issue for C++ main() is name mangling - main() is implicitly
extern "C" in it's naming and linkage.  If it were legal to call main()
from somewhere other than the CRT startup, then that would also have to
be handled somehow.

[toc] | [prev] | [next] | [standalone]


#67535

FromMalcolm McLean <malcolm.mclean5@btinternet.com>
Date2015-08-16 10:39 -0700
Message-ID<643f7ad1-bdd4-4f51-84a6-d8171240ef49@googlegroups.com>
In reply to#67395
On Friday, August 14, 2015 at 1:50:27 PM UTC+1, David Brown wrote:
>
> I believe that in most C++ implementations, run-time initialisation
> (such as setting up initialised data, running global constructors, and
> possibly running static constructors) is done before main() is called.
> However, I have seen implementations where the global constructors are
> called by a hidden loop added by the compiler at the start of main().
> As far as I know both methods are valid since it is illegal to call
> main() from elsewhere in the code.
> 
> Another issue for C++ main() is name mangling - main() is implicitly
> extern "C" in it's naming and linkage.  If it were legal to call main()
> from somewhere other than the CRT startup, then that would also have to
> be handled somehow.
>
C++ was originally a front end to a C compiler. So of course it had to use
the second method. 

[toc] | [prev] | [next] | [standalone]


#67390

Fromgazelle@shell.xmission.com (Kenny McCormack)
Date2015-08-14 12:32 +0000
Message-ID<mqkn5p$bqh$1@news.xmission.com>
In reply to#67380
In article <C-main-20150814130337@ram.dialup.fu-berlin.de>,
Stefan Ram <ram@zedat.fu-berlin.de> wrote:
>Ben Bacarisse <ben.usenet@bsb.me.uk> writes:
>>Right, but an implementation can define others, so there is no reason to
>>reject the question on this basis.  (I know you are not doing that.  I'm
>>just clearing up a detail.)
>
>  In C, main may call itself. In C++, this has been forbidden.
>

I think it is forbidden in Fortran as well.

(And BASIC, too)

-- 
Religion is what keeps the poor from murdering the rich.

	- Napoleon Bonaparte -

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.c


csiph-web