How can to write a c program without a main()

M

Mike Wahler

Spidey said:
How can to write a c program without a main()
so i can compile and run it


If you are able to write and run such a program,
it, by definition, is not a C program.

-Mike
 
K

Keith Thompson

Spidey said:
How can to write a c program without a main()
so i can compile and run it

Some systems, particularly embedded systems, may not require a main()
function. (I think some Windows implementations may use something
like "WinMain".) Conforming hosted implementations always require
main().

Why do you want to do this? It seems like another "How do I drive
nails without using a hammer?" question.
 
W

Walter Roberson

Some systems, particularly embedded systems, may not require a main()
function. (I think some Windows implementations may use something
like "WinMain".) Conforming hosted implementations always require
main().

Some compilers and/or linkers have options to designate alternate
entry points -- which would be system specific (and thus non-portable).
Even systems that provide such mechanisms might only allow limited
functionality -- for example, potentially if main() is skipped, then
I/O might not be initialized, or the environment variables might
not be set up properly. Tis thus very much a "use at your own risk"
feature that is outside the boundaries of the C language.
 
M

Mike Wahler

Joel said:
Though it is idiotic but try this .....:):):):):):):):):)


#define A main(){
A
printf("hello\n");
}

OP asked about a program *without* a 'main()'
function. The above has one. However the
program has undefined behavior due to the
call to 'printf()' with no prototype in scope.

Also note that it doesn't conform to C99, which
requires a return type of 'int'.

-Mike
 
W

William J. Leary Jr.

Keith Thompson said:
Some systems, particularly embedded systems, may not require a main()
function.

Yes. I used an embedded devel system where explicitly declaring the entry
point (it could be "main" if you wanted, but usually wasn't) would eliminate
the initialization code. You didn't get zeroed globals, I/O had to be
initialized by the code, if you wanted to use it, the stack was undeclared,
couldn't return from that code or use exit(), and so on. You needed to either
set up the stack very early (like, first line) with an asm( ) statement, or the
entry could be assembly code. I recall a warning at the top of the page which
outlined was would NOT be done that said something like "If you don't
understand all this, don't do it".
Why do you want to do this? It seems like another "How do I drive
nails without using a hammer?" question.

Nice turn of a phrase there.

- Bill
 
N

neo

dear spidey,

there are always rules for the game otherwise things won't go work . C
language has only way as an entry point for a program is main()
function in DOS environment and WinMain () function in Windows
environment.

To my knowledge all C compilers are need to follow this standard, well
you search for some alternative and let me know if you find an answer

Take Care
neo
 
C

Christopher Benson-Manica

Also note that it doesn't conform to C99, which
requires a return type of 'int'.

Since we're in pedantry mode, it also returns an undefined termination
status to the host environment in C89.
 
R

Randy Howard

neo wrote
(in article
dear spidey,

there are always rules for the game otherwise things won't go work . C
language has only way as an entry point for a program is main()
function in DOS environment and WinMain () function in Windows
environment.

Do really think C gets is use of main() from DOS? Or are DOS
and Windows the only two platforms you are aware of being used
for C programming?
 
M

Mark B

Spidey said:
How can to write a c program without a main()
so i can compile and run it

Link against a(ny) library (such as cgic) which defines the main function
for you.
 
K

Kenny McCormack

Link against a(ny) library (such as cgic) which defines the main function
for you.

Or, to put it another way, write a shared library (off topic here, of
course), that is loaded by an existing piece of software that you did not
write.

Lawyers can debate whether this constitutes "writing a program", but for
the rest of us, it's good enough.
 
M

Mike Wahler

neo said:
dear spidey,

there are always rules for the game otherwise things won't go work . C
language has only way as an entry point for a program is main()
function in DOS environment and WinMain () function in Windows
environment.

Really? I write quite a few C programs with entry point
'main()' in a Windows environment.

Whether the host environment is DOS, Windows, or something
else, has no bearing at all on the C entry point function's
name.
To my knowledge all C compilers are need to follow this standard,

Those for hosted implementations claiming conformance must
require one entry point function name: 'main'.

-Mike
 
F

Frodo Baggins

Spidey said:
How can to write a c program without a main()
so i can compile and run it


Maybe the OP wants to compile a module.
you can have modules without main().
gcc -c module.c
produces just the .o (object) file required for linking.

Regards,
Frodo Baggins
 
K

Keith Thompson

Mike Wahler said:
Really? I write quite a few C programs with entry point
'main()' in a Windows environment.

Whether the host environment is DOS, Windows, or something
else, has no bearing at all on the C entry point function's
name.

That's true for conforming hosted implementations. I think that some
of the popular implementations under Windows don't qualify, at least
in some modes.
 
K

Keith Thompson

Frodo Baggins said:
Maybe the OP wants to compile a module.
you can have modules without main().
gcc -c module.c
produces just the .o (object) file required for linking.

Yes, but the OP specifically asked how to compile *and run* a
*program*.

There's no point in guessing what he meant. If he cares, he can come
back and explain. If he doesn't, neither do I.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,049
Latest member
Allen00Reed

Latest Threads

Top