By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,992 Members | 891 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,992 IT Pros & Developers. It's quick & easy.

program without main () ?

P: n/a
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.

Jan 9 '06 #1
Share this Question
Share on Google+
13 Replies


P: n/a
Ico
robinsonreyna <ro***********@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
Jan 9 '06 #2

P: n/a

"robinsonreyna" <ro***********@gmail.com> wrote in message
news:11**********************@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.
Jan 9 '06 #3

P: n/a
M.B

pemo wrote:
"robinsonreyna" <ro***********@gmail.com> wrote in message
news:11**********************@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...

Jan 9 '06 #4

P: n/a
Ico <us****@zevv.nl> wrote:
robinsonreyna <ro***********@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
Jan 9 '06 #5

P: n/a
M.B

Richard Bos wrote:
Ico <us****@zevv.nl> wrote:
robinsonreyna <ro***********@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


Jan 9 '06 #6

P: n/a
In article <43***********************@dreader21.news.xs4all.n l>, Ico
<us****@zevv.nl> writes
robinsonreyna <ro***********@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 /\/\/\/\/
/\/\/ ch***@phaedsys.org www.phaedsys.org \/\/\
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/

Jan 9 '06 #7

P: n/a

In article <11**********************@g47g2000cwa.googlegroups .com>, "M.B" <mb*******@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 mi************@microfocus.com

The guy who's fast in the mountain pass is the coolest.
-- _Initial D: Second Stage_
Jan 9 '06 #8

P: n/a

"robinsonreyna" <ro***********@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.
Jan 9 '06 #9

P: n/a
Chris Hills <ch***@phaedsys.org> writes:
In article <43***********************@dreader21.news.xs4all.n l>, Ico
<us****@zevv.nl> writes
robinsonreyna <ro***********@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) 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.
Jan 9 '06 #10

P: n/a
"robinsonreyna" <ro***********@gmail.com> 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) 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.
Jan 9 '06 #11

P: n/a
On 9 Jan 2006 00:44:46 -0800, in comp.lang.c , "robinsonreyna"
<ro***********@gmail.com> 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 =----
Jan 9 '06 #12

P: n/a
On Mon, 9 Jan 2006 12:03:34 +0000, Chris Hills <ch***@phaedsys.org>
wrote in comp.lang.c:
In article <43***********************@dreader21.news.xs4all.n l>, Ico
<us****@zevv.nl> writes
robinsonreyna <ro***********@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


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
Jan 10 '06 #13

P: n/a

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.


Jan 10 '06 #14

This discussion thread is closed

Replies have been disabled for this discussion.