Velocity Reviews - Computer Hardware Reviews

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

Reply
Thread Tools

program without main () ?

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


This is like asking whether it's possible to drive a nail without
using a hammer. The answer to the latter question is "just use a
hammer, or explain to me why you can't".

Why do you want to write a program without a main() function?

Is this a homework question? If so, presumably you can figure out the
answer from what's been presented in class; if not, talk to your
instructor.

Or give us your instructor's e-mail address so we can submit our
answers directly (if we're doing all the work, I see no reason for you
to be involved).

Or you might have a perfectly legitimate reason for asking this. If
so, please explain it to us; your reason for asking is likely to
affect the answer.

--
Keith Thompson (The_Other_Keith) kst- <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
 
 
 
 
Mark McIntyre
Guest
Posts: n/a
 
      01-09-2006
On 9 Jan 2006 00:44:46 -0800, in comp.lang.c , "robinsonreyna"
<> wrote:

> Is it possible to write a program which do not have a
>main() function. The program should compile and run.


A C programme is required to have a main() in a hosted environment, ie
the sort most programmers are generally familiar with. However it may
be hidden in some implementation library.

>Please give sample code to do this.


Not possible, since its dependent entirely on your compiler &
platform. Ask again in a group specialising in your compiler or OS.

Mark McIntyre
--

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
 
Reply With Quote
 
 
 
 
Jack Klein
Guest
Posts: n/a
 
      01-10-2006
On Mon, 9 Jan 2006 12:03:34 +0000, Chris Hills <>
wrote in comp.lang.c:

> In article <43c226f8$0$25626$ l>, Ico
> <> writes
> >robinsonreyna <> 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


You are going overboard here. There are many embedded systems that
have operating systems, yet are "freestanding" environments as defined
the C standard. In fact, I've written operating systems for
freestanding embedded systems, and used others off-the-shelf.

Here is the C standard's definition of a freestanding environment:

"In a freestanding environment (in which C program execution may take
place without any benefit of an operating system), the name and type
of the function called at program startup are implementation-defined."

Note especially the phrase "may take place without the benefit of an
operating system". It does not forbid a freestanding environment from
having an operating system.

As far as the C standard is concerned, the real distinction between
hosted and freestanding environments is this:

"The two forms of conforming implementation are hosted and
freestanding. A conforming hosted implementation shall accept any
strictly conforming program. A conforming freestanding implementation
shall accept any strictly conforming program that does not use complex
types and in which the use of the features specified in the library
clause (clause 7) is confined to the contents of the standard headers
<float.h>, <iso646.h>, <limits.h>, <stdarg.h>, <stdbool.h>,
<stddef.h>, and <stdint.h>. A conforming implementation may have
extensions (including additional library functions), provided they do
not alter the behavior of any strictly conforming program."

In other words, a hosted implementation must provide every single
function defined in the standard library for the application to call,
but a freestanding environment does not have provide any standard
library functions at all.

--
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
 
Reply With Quote
 
Walter Banks
Guest
Posts: n/a
 
      01-10-2006

I know of one embedded processor case where code is only executed in response to an event. (Effectively and interrupt)

There is no reset event on the processor so there is not way the code for main can be reached.

The compiler we wrote for this processor allows for an empty void main (void) { } and gave an error if main contained any code.

main was not required and never executed.

Walter Banks

robinsonreyna wrote:

> 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
 
 
 
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