473,699 Members | 2,443 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

c program without main( ) ?

Hi, I will appreciate your assistance.

Can we write a c code which do not contain main()
I have heard that this is possible. Is it really possible?

Thanks for your help in advance

lee

Nov 15 '05 #1
16 5386
le****@gmail.co m wrote:
Hi, I will appreciate your assistance.

Can we write a c code which do not contain main()
I have heard that this is possible. Is it really possible?


Yes, of course. Win32 programmers do it all the time.

C implementations which specify an entry point other than main are generally
called "freestandi ng" implementations , and requirements on them are far
less severe than on hosted implementations , this laxity over the name of
the entry point being one example of the lower requirements.

(In Win32 programs, the entry point is typically called WinMain.)

A more common situation in which we write C code without main() is when
we're writing a code library, rather than an actual program. The library is
built separately, without main(), and then linked to whichever programs
wish to use it.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
mail: rjh at above domain
Nov 15 '05 #2
le****@gmail.co m wrote on 15/07/05 :
Hi, I will appreciate your assistance.

Can we write a c code which do not contain main()
I have heard that this is possible. Is it really possible?


It's not possible to write a conforming C program without a main(). But
the main() function could be embedded in some 'framework library' (Like
a GUI Windows application for example).

I do that when I write code for dev-C++ :

I have a 'framework.c' containing the main() function :

(missing code at http://mapage.noos.fr/emdel/clib.htm)

#include "ed/inc/sysalloc.h"
#include <string.h>
int main_ (int argc, char *argv[]);

static int is_opt (char const *const sopt, int const argc, char *const
*const argv)
{
int ok = 0;
int i;

for (i = 1; i < argc; i++)
{
if (strcmp (sopt, argv[i]) == 0)
{
ok = 1;
break;
}
}
return ok;
}

static void onexit (void)
{
sys_mem_trace ();
system ("pause");
}

int main (int argc, char *argv[])
{
static char Trace[1 << 11];
int trace = TRACE_OFF;

atexit (onexit);

if (argc > 1)
{
if (is_opt ("/t", argc, argv))
{
trace = TRACE_ON;
}
}

sys_mem_init (Trace, sizeof Trace, trace, NULL);

main_ (argc, argv);

return 0;
}
and in my 'main.c', I have :

#define main main_
#if 0
#include "ed/inc/sysalloc.h"
#include "ed/inc/sys.h"
#undef assert
#define assert(e) ASSERT (e)
#else
#include <assert.h>
#endif
/*
----------------------------------------------------------------------
*/

int main ()
{

/* device under test */

return 0;
}
BTW, why do you ask ? What exactly is your goal ?

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"Clearly your code does not meet the original spec."
"You are sentenced to 30 lashes with a wet noodle."
-- Jerry Coffin in a.l.c.c++

Nov 15 '05 #3
On 2005-07-15 13:15:15 -0500, le****@gmail.co m said:
Hi, I will appreciate your assistance.

Can we write a c code which do not contain main()
I have heard that this is possible. Is it really possible?

Yes, a library would do that.

--
Sensei <se******@tin.i t>

cd /pub
more beer

Nov 15 '05 #4
"Richard Heathfield" <in*****@addres s.co.uk.invalid > wrote in message
news:db******** *@nwrdmz02.dmz. ncs.ea.ibs-infra.bt.com...
le****@gmail.co m wrote:
Hi, I will appreciate your assistance.
Can we write a c code which do not contain main()
I have heard that this is possible. Is it really possible?

Yes, of course. Win32 programmers do it all the time.
C implementations which specify an entry point other than main are
generally
called "freestandi ng" implementations , and requirements on them are far
less severe than on hosted implementations , this laxity over the name of
the entry point being one example of the lower requirements.
(In Win32 programs, the entry point is typically called WinMain.)
A more common situation in which we write C code without main() is when
we're writing a code library, rather than an actual program. The library
is
built separately, without main(), and then linked to whichever programs
wish to use it.


I've encountered situations in which main() was defined by a library which
provided an external function for the application to hook into. That
sucked!
Fortunately that code now resides in India!

Mark
Nov 15 '05 #5
le****@gmail.co m wrote:
Hi, I will appreciate your assistance.

Can we write a c code which do not contain main()
I have heard that this is possible. Is it really possible?


Yes, it is, but you can only obtain object code out of it, instead of an
executable file...

David.
--
David Lago <dl***@mundo-r.com>

PGP key available at: http://pgp.mit.edu
Personal Blizog: http://arcanelinux.org/blog/dlworld
The Enflame Project: http://enflame.org
Nov 15 '05 #6
"Emmanuel Delahaye" <em***@YOURBRAn oos.fr> wrote in message
news:mn******** *************** @YOURBRAnoos.fr ...
le****@gmail.co m wrote on 15/07/05 :
Hi, I will appreciate your assistance.

Can we write a c code which do not contain main()
I have heard that this is possible. Is it really possible?


It's not possible to write a conforming C program without a main(). But
the main() function could be embedded in some 'framework library' (Like a
GUI Windows application for example).

I do that when I write code for dev-C++ :

<snip garbage>

It would be cleaner if you were to provide an initialization function which
could be called on startup, why didn't you? What happens when the code
you want to test needs to link with another shitty library which also
decided
to define main() ?

Just curious,
Mark
Nov 15 '05 #7
Mark wrote on 15/07/05 :
I do that when I write code for dev-C++ : <snip garbage>


Garbage ? Thanks...
It would be cleaner if you were to provide an initialization function which
could be called on startup, why didn't you? What happens when the code
you want to test needs to link with another shitty library which also decided
to define main() ?

Just curious,


Linker error ?

This code is designed to test code (like the one posted by people I try
to help on the forums.

It happens that some of this code can be strange (to me), like having
several return from main() or a lot of exit()'s spread out. This is the
way I found to handle that. If you have a better solution, I'd be glad
to see it.

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"Mal nommer les choses c'est ajouter du malheur au
monde." -- Albert Camus.

Nov 15 '05 #8
"Emmanuel Delahaye" <em***@YOURBRAn oos.fr> wrote in message
news:mn******** *************** @YOURBRAnoos.fr ...
Mark wrote on 15/07/05 :
I do that when I write code for dev-C++ :

<snip garbage>


Garbage ? Thanks...

OK, a little harsh, sorry.
It would be cleaner if you were to provide an initialization function
which
could be called on startup, why didn't you? What happens when the code
you want to test needs to link with another shitty library which also
decided
to define main() ?

Just curious,


Linker error ?

This code is designed to test code (like the one posted by people I try to
help on the forums.

It happens that some of this code can be strange (to me), like having
several return from main() or a lot of exit()'s spread out. This is the
way I found to handle that. If you have a better solution, I'd be glad to
see it.


I've already given you the suggestion, here it is again:
It would be cleaner if you were to provide an initialization function
which
could be called on startup


The solution would be to rename your 'main()' function
calling it something intuitive, such as init_testlib();

Then when you want to help someone, you can still copy and paste their code
and need only to add 1 line to the main() function (a call to
init_testlib(ar gc, argv);)

As for multiple return statements and calls to exit... it makes no
difference to your
library as you have registered yourself with the atexit() function... you
don't perform
ANY calculations or do a cleanup of any sort when main returns control to
you!
Should you have such a requirement in the future, I'd suggest creating
another
function to perform the post-run tasks.

Regards,
Mark
Nov 15 '05 #9
Richard Heathfield <in*****@addres s.co.uk.invalid > wrote:
le****@gmail.co m wrote:
Hi, I will appreciate your assistance.

Can we write a c code which do not contain main()
I have heard that this is possible. Is it really possible?


Yes, of course. Win32 programmers do it all the time.

C implementations which specify an entry point other than main are generally
called "freestandi ng" implementations , and requirements on them are far
less severe than on hosted implementations , this laxity over the name of
the entry point being one example of the lower requirements.

(In Win32 programs, the entry point is typically called WinMain.)


So you mean to say that Windows+MSVC is a freestanding implementation?

--
Stan Tobias
mailx `echo si***@FamOuS.Be dBuG.pAlS.INVALID | sed s/[[:upper:]]//g`
Nov 15 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
1860
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 define thisthat main I mean absolutely no main() atall Well to start off here one method :
13
2438
by: robinsonreyna | last post by:
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.
4
7055
by: bagkalyan | last post by:
please help me.. Yes it is possible to write a program in c without using main. Here is the code: /* prog_without_main.c */ _start() { _exit(my_main()); } int my_main(void)
17
2036
by: mariz | last post by:
hi , i m a new member to this group . i start from the beginig - main(). Can any one give me an idea for how to write a program in C without using main() ? ie while looking source code it wont contain main keyword.
0
8691
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8620
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9180
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9038
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8920
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
5877
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4633
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3060
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2012
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.