Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > program without main () ?

Reply
Thread Tools

program without main () ?

 
 
robinsonreyna
Guest
Posts: n/a
 
      01-09-2006
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.

 
Reply With Quote
 
 
 
 
Ico
Guest
Posts: n/a
 
      01-09-2006
robinsonreyna <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
 
pemo
Guest
Posts: n/a
 
      01-09-2006

"robinsonreyna" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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.


 
Reply With Quote
 
M.B
Guest
Posts: n/a
 
      01-09-2006

pemo wrote:
> "robinsonreyna" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) 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...

 
Reply With Quote
 
Richard Bos
Guest
Posts: n/a
 
      01-09-2006
Ico <(E-Mail Removed)> wrote:

> robinsonreyna <(E-Mail Removed)> 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
 
Reply With Quote
 
Chris Hills
Guest
Posts: n/a
 
      01-09-2006
In article <43c226f8$0$25626$(E-Mail Removed)4all.n l>, Ico
<(E-Mail Removed)> writes
>robinsonreyna <(E-Mail Removed)> 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 /\/\/\/\/
/\/\/ http://www.velocityreviews.com/forums/(E-Mail Removed) www.phaedsys.org \/\/\
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/



 
Reply With Quote
 
M.B
Guest
Posts: n/a
 
      01-09-2006

Richard Bos wrote:
> Ico <(E-Mail Removed)> wrote:
>
> > robinsonreyna <(E-Mail Removed)> 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


 
Reply With Quote
 
Michael Wojcik
Guest
Posts: n/a
 
      01-09-2006

In article <(E-Mail Removed) .com>, "M.B" <(E-Mail Removed)> 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 (E-Mail Removed)

The guy who's fast in the mountain pass is the coolest.
-- _Initial D: Second Stage_
 
Reply With Quote
 
Malcolm
Guest
Posts: n/a
 
      01-09-2006

"robinsonreyna" <(E-Mail Removed)> 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.


 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      01-09-2006
Chris Hills <(E-Mail Removed)> writes:
> In article <43c226f8$0$25626$(E-Mail Removed)4all.n l>, Ico
> <(E-Mail Removed)> writes
>>robinsonreyna <(E-Mail Removed)> 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) (E-Mail Removed) <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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How can to write a c program without a main() Spidey C Programming 37 12-06-2005 12:24 AM
c program without main( ) ? leeaby@gmail.com C Programming 16 07-17-2005 10:56 AM
Question: Program crashs without reaching the breakpoint set at the beginning of main()? wenjie wang C++ 5 11-01-2004 01:14 AM
Program without main Guruz C Programming 5 05-15-2004 02:34 AM



Advertisments