program without main () ?

Discussion in 'C Programming' started by robinsonreyna, Jan 9, 2006.

  1. 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.
    robinsonreyna, Jan 9, 2006
    #1
    1. Advertising

  2. robinsonreyna

    Ico Guest

    robinsonreyna <> 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
    Ico, Jan 9, 2006
    #2
    1. Advertising

  3. robinsonreyna

    pemo Guest

    "robinsonreyna" <> wrote in message
    news:...
    > 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.
    pemo, Jan 9, 2006
    #3
  4. robinsonreyna

    M.B Guest

    pemo wrote:
    > "robinsonreyna" <> wrote in message
    > news:...
    > > 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...
    M.B, Jan 9, 2006
    #4
  5. robinsonreyna

    Richard Bos Guest

    Ico <> wrote:

    > robinsonreyna <> 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
    Richard Bos, Jan 9, 2006
    #5
  6. robinsonreyna

    Chris Hills Guest

    In article <43c226f8$0$25626$4all.nl>, 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

    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 /\/\/\/\/
    /\/\/ www.phaedsys.org \/\/\
    \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/
    Chris Hills, Jan 9, 2006
    #6
  7. robinsonreyna

    M.B Guest

    Richard Bos wrote:
    > Ico <> wrote:
    >
    > > robinsonreyna <> 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
    M.B, Jan 9, 2006
    #7
  8. In article <>, "M.B" <> 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

    The guy who's fast in the mountain pass is the coolest.
    -- _Initial D: Second Stage_
    Michael Wojcik, Jan 9, 2006
    #8
  9. robinsonreyna

    Malcolm Guest

    "robinsonreyna" <> 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.
    Malcolm, Jan 9, 2006
    #9
  10. Chris Hills <> writes:
    > In article <43c226f8$0$25626$4all.nl>, 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)


    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) <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.
    Keith Thompson, Jan 9, 2006
    #10
  11. "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) <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.
    Keith Thompson, Jan 9, 2006
    #11
  12. 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 =----
    Mark McIntyre, Jan 9, 2006
    #12
  13. robinsonreyna

    Jack Klein Guest

    On Mon, 9 Jan 2006 12:03:34 +0000, Chris Hills <>
    wrote in comp.lang.c:

    > In article <43c226f8$0$25626$4all.nl>, 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/~ajo/docs/FAQ-acllc.html
    Jack Klein, Jan 10, 2006
    #13
  14. robinsonreyna

    Walter Banks Guest

    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.
    Walter Banks, Jan 10, 2006
    #14
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. wenjie wang
    Replies:
    5
    Views:
    471
    wenjie wang
    Nov 1, 2004
  2. Guruz

    Program without main

    Guruz, May 14, 2004, in forum: C Programming
    Replies:
    5
    Views:
    6,048
    Mitchell
    May 15, 2004
  3. c program without main( ) ?

    , Jul 15, 2005, in forum: C Programming
    Replies:
    16
    Views:
    1,651
    Chris Hills
    Jul 17, 2005
  4. Spidey
    Replies:
    37
    Views:
    1,253
    Keith Thompson
    Dec 6, 2005
  5. knguyen
    Replies:
    9
    Views:
    2,805
    Roedy Green
    Feb 15, 2008
Loading...

Share This Page