Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   program without main () ? (http://www.velocityreviews.com/forums/t440804-program-without-main.html)

robinsonreyna 01-09-2006 08:44 AM

program without main () ?
 
Hi everyone

Is it possible to write a program which do not have a
main() function. The program should compile and run. Please give
sample code to do this.


Ico 01-09-2006 09:03 AM

Re: program without main () ?
 
robinsonreyna <robinsonreyna@gmail.com> wrote:
>
> Is it possible to write a program which do not have a
> main() function.


No

> The program should compile and run.


Not possible

> Please give sample code to do this.


Does not exist

--
:wq
^X^Cy^K^X^C^C^C^C

pemo 01-09-2006 09:27 AM

Re: program without main () ?
 

"robinsonreyna" <robinsonreyna@gmail.com> wrote in message
news:1136796286.212467.296480@o13g2000cwo.googlegr oups.com...
> Hi everyone
>
> Is it possible to write a program which do not have a
> main() function. The program should compile and run. Please give
> sample code to do this.


Without altering the source of the startup code, I don't think this can be
done - the linker expects to find a main() somewhere.



M.B 01-09-2006 09:40 AM

Re: program without main () ?
 

pemo wrote:
> "robinsonreyna" <robinsonreyna@gmail.com> wrote in message
> news:1136796286.212467.296480@o13g2000cwo.googlegr oups.com...
> > Hi everyone
> >
> > Is it possible to write a program which do not have a
> > main() function. The program should compile and run. Please give
> > sample code to do this.

>
> Without altering the source of the startup code, I don't think this can be
> done - the linker expects to find a main() somewhere.


If main function need to be absent from your vivion (program file.c)
then this works
#include<stdio.h>
#define STUPID main

int STUPID()
{
puts("Are you nuts ?");
return(0);
}

if preprocessing is not your area...


Richard Bos 01-09-2006 11:20 AM

Re: program without main () ?
 
Ico <usenet@zevv.nl> wrote:

> robinsonreyna <robinsonreyna@gmail.com> wrote:
> >
> > Is it possible to write a program which do not have a
> > main() function.

>
> No


Almost no. It may be possible using a free-standing implementation.
Usually those are cross-compilers for, e.g., embedded chips, but from
one POV the usual kind of implementation of C on MS-Windows is also a
free-standing one. (And from another POV, it's a hosted implementation
of something similar to, but not the same as, C.) And indeed, under
MS-Windows, the entry function into your program is usually called
WinMain(), not main().

Using normal, ISO C on a normal, hosted implementation, though, it is
indeed not possible.

Richard

Chris Hills 01-09-2006 12:03 PM

Re: program without main () ?
 
In article <43c226f8$0$25626$e4fe514c@dreader21.news.xs4all.n l>, Ico
<usenet@zevv.nl> writes
>robinsonreyna <robinsonreyna@gmail.com> wrote:
>>
>> Is it possible to write a program which do not have a
>> main() function.

>
>No

This is not correct.

>
>> The program should compile and run.

>
>Not possible
>
>> Please give sample code to do this.

>
>Does not exist


It does.

However this is only in special circumstances. In a Hosted system. Ie
one with an operating system you must have

int main(void)
or
int main (argv argc)

in a self hosted system (usually an embedded system) you do not need to
have a main and as there is no OS main can be

void main(void)

but this is only in self hosted systems.

In these systems it is possible to have a function other than main as
the entry point BUT you have to adjust the startup assembler to jump to
this different label.

There is another caveat. Most embedded debuggers "run to main" when they
start and are looking for a function called main. some let you change
the default most don't.

So whilst in theory you can have something other than main it is not
normally done even where it can be.




--
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
\/\/\/\/\ Chris Hills Staffs England /\/\/\/\/
/\/\/ chris@phaedsys.org www.phaedsys.org \/\/\
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/




M.B 01-09-2006 12:07 PM

Re: program without main () ?
 

Richard Bos wrote:
> Ico <usenet@zevv.nl> wrote:
>
> > robinsonreyna <robinsonreyna@gmail.com> wrote:
> > >
> > > Is it possible to write a program which do not have a
> > > main() function.

> >
> > No

>
> Almost no. It may be possible using a free-standing implementation.
> Usually those are cross-compilers for, e.g., embedded chips, but from
> one POV the usual kind of implementation of C on MS-Windows is also a
> free-standing one. (And from another POV, it's a hosted implementation
> of something similar to, but not the same as, C.) And indeed, under
> MS-Windows, the entry function into your program is usually called
> WinMain(), not main().
>

I am not windows expert.
But there there might be a main() (in some library) calling WinMain();


> Using normal, ISO C on a normal, hosted implementation, though, it is
> indeed not possible.
>
> Richard



Michael Wojcik 01-09-2006 06:29 PM

Re: program without main () ?
 

In article <1136808427.887082.277140@g47g2000cwa.googlegroups .com>, "M.B" <mbpradeep@gmail.com> writes:
> Richard Bos wrote:
> >
> > Almost no. It may be possible using a free-standing implementation.


Or in an implementation-defined manner under a hosted implementation
(Richard knows that; I'm just clarifying).

> > Usually those are cross-compilers for, e.g., embedded chips, but from
> > one POV the usual kind of implementation of C on MS-Windows is also a
> > free-standing one. (And from another POV, it's a hosted implementation
> > of something similar to, but not the same as, C.) And indeed, under
> > MS-Windows, the entry function into your program is usually called
> > WinMain(), not main().
> >

> I am not windows expert.
> But there there might be a main() (in some library) calling WinMain();


That's irrelevant; it's an implementation detail for this particular
freestanding implementation.

--
Michael Wojcik michael.wojcik@microfocus.com

The guy who's fast in the mountain pass is the coolest.
-- _Initial D: Second Stage_

Malcolm 01-09-2006 08:58 PM

Re: program without main () ?
 

"robinsonreyna" <robinsonreyna@gmail.com> wrote
> Is it possible to write a program which do not have a
> main() function. The program should compile and run. Please give
> sample code to do this.
>

Portable ANSI C programs begin with main(). It is not possible to substitute
another identiifer.
However platform-specific programs quite often begin with another function
(a favourite for embedded programs is "boot"). You need to see your platform
documentation to get the details.



Keith Thompson 01-09-2006 09:17 PM

Re: program without main () ?
 
Chris Hills <chris@phaedsys.org> writes:
> In article <43c226f8$0$25626$e4fe514c@dreader21.news.xs4all.n l>, Ico
> <usenet@zevv.nl> writes
>>robinsonreyna <robinsonreyna@gmail.com> wrote:
>>>
>>> Is it possible to write a program which do not have a
>>> main() function.

>>
>>No

> This is not correct.
>
>>
>>> The program should compile and run.

>>
>>Not possible
>>
>>> Please give sample code to do this.

>>
>>Does not exist

>
> It does.
>
> However this is only in special circumstances. In a Hosted system. Ie
> one with an operating system you must have
>
> int main(void)
> or
> int main (argv argc)


Um, the order is argc argv.

> in a self hosted system (usually an embedded system) you do not need to
> have a main and as there is no OS main can be
>
> void main(void)
>
> but this is only in self hosted systems.


The standard uses the term "freestanding", not "self hosted". (In my
experience, the term "self hosted" refers to a compiler compiling
itself, not to the nature of the environment.)

In a freestanding environment, "the name and type of the function
called at program startup are implementation-defined".
"void main(void)" is allowed only if the implementation's
documentation specifically says so. As far as the standard is
concerned, there's no more reason to assume the entry point looks like
"void main(void)" than to assume that it looks like
"double start_here(unsigned long stack_size);".

--
Keith Thompson (The_Other_Keith) kst-u@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.


All times are GMT. The time now is 10:23 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.