473,407 Members | 2,320 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

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.

Jan 9 '06 #1
13 2413
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

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

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

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

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Baloff | last post by:
Hello I am learning C++, a book exercise is asking to write a C program for the purpose of learning compilation output when compile with c or c++ compiler. the program must use puts() without...
92
by: Raghavendra R A V, CSS India | last post by:
hie.. Do any one knows how to write a C program without using the conditional statements if, for, while, do, switch, goto and even condotional statements ? It would be a great help for me if...
5
by: Guruz | last post by:
hi C gurus do anyone of u know how to write a program in C without main and still create a executable out of it. Remember, I said no main() function not in -->include files -->libraries -->no...
54
by: bnp | last post by:
Hi, I took a test on C. there was an objective question for program output type. following is the program: main() { char ch; int i =2;
5
by: Seong-Kook Shin | last post by:
Hi, I'm reading Steve's "C Programming FAQs" in book version, and have two question regarding to Q11.16 ... Also, a `return' from `main' cannot be expected to work if data local to main might be...
16
by: Martin Joergensen | last post by:
Hi, I wanted to try something which I think is a very good exercise... I read in data from the keyboard and store them in a structure. There's a pointer called "data_pointer" which I use to...
10
by: yaniv.dg | last post by:
hi all, i'm bumping into smething very starnge its very simple code but from some reason the program is acting strange this is my code: #include<stdio.h> #include<conio.h> void main(void) {...
0
by: ashishbathini | last post by:
Hi guys here is my problem ... this is the source code I Have , honestly I hav no idea how it works bcos its too complicated for me .... but my problem is ... i hav a freq comonent in it .......
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.