main function

Discussion in 'C Programming' started by aarklon@gmail.com, Nov 29, 2007.

  1. Guest

    Hi all,

    I have heard many discussions among my colleagues that main is a user
    defined function or not.

    arguments in favour:-

    1) if it is built in function it must be defined in some header file
    but it is n't.

    2) every time we are defining how the main function should work

    arguments against:-

    1)user defined functions cannot be called from command line

    some say that it is a special user defined function ,which is the
    starting point of execution of a C program.

    now questions are

    1) What exactly is the correct answer..????
    2) in free standing implementations is main the starting point
    of execution..????
     
    , Nov 29, 2007
    #1
    1. Advertising

  2. Ian Collins Guest

    wrote:
    > Hi all,
    >
    > I have heard many discussions among my colleagues that main is a user
    > defined function or not.
    >
    > arguments in favour:-
    >
    > 1) if it is built in function it must be defined in some header file
    > but it is n't.
    >

    Declared in a header, defined in a library.

    > 2) every time we are defining how the main function should work
    >
    > arguments against:-
    >
    > 1)user defined functions cannot be called from command line
    >

    Nor can any other, including main.

    > some say that it is a special user defined function ,which is the
    > starting point of execution of a C program.
    >
    > now questions are
    >
    > 1) What exactly is the correct answer..????


    The standard specifies main as the name of the function called at
    program startup. There isn't a prototype in any headers because the
    function can have one of two.


    > 2) in free standing implementations is main the starting point
    > of execution..????


    In a freestanding environment, the he name of the function called at
    program startup is implementation defined.

    --
    Ian Collins.
     
    Ian Collins, Nov 29, 2007
    #2
    1. Advertising

  3. Mark Bluemel Guest

    wrote:
    > Hi all,
    >
    > I have heard many discussions among my colleagues that main is a user
    > defined function or not.


    What does the language specification say?

    I suggest you get a copy of the standard (or the draft standard which is
    available free) - see the FAQ for some pointers.

    A lot of the questions you seem to be asking would be answered simply
    by reading the standard rather than swapping opinions. In this case,
    section 5.1.2 seems relevant (I'm using the C99 draft PDF).

    > arguments in favour:-
    >
    > 1) if it is built in function it must be defined in some header file
    > but it is n't.


    There isn't, as far as I'm aware, any definition of "built in function"
    in the C language specification. There are functions which an
    implementation is required to provide and "main" isn't one of them.

    > 2) every time we are defining how the main function should work


    True enough. Though the standard defines what its arguments must be.

    > arguments against:-


    > 1)user defined functions cannot be called from command line
    >
    > some say that it is a special user defined function ,which is the
    > starting point of execution of a C program.
    >
    > now questions are
    >
    > 1) What exactly is the correct answer..????


    What does the standard say?

    > 2) in free standing implementations is main the starting point
    > of execution..????


    That depends on the implementation. The standard only mandates main for
    hosted implementations.
     
    Mark Bluemel, Nov 29, 2007
    #3
  4. santosh Guest

    wrote:

    > Hi all,
    >
    > I have heard many discussions among my colleagues that main is a user
    > defined function or not.
    >
    > arguments in favour:-
    >
    > 1) if it is built in function it must be defined in some header file
    > but it is n't.
    >
    > 2) every time we are defining how the main function should work
    >
    > arguments against:-
    >
    > 1)user defined functions cannot be called from command line


    Neither is main() called from the command line. What you actually do is
    invoke the _program_ from the command line. The implementation is
    responsible for collecting any arguments and placing them at main()'s
    disposal and calling main() itself.

    Some implementations may have no means to specify command line
    parameters and under some conditions it may not even be possible to
    invoke the program at all, (example if it is the "only" program running
    in the device).

    > some say that it is a special user defined function ,which is the
    > starting point of execution of a C program.


    From the perspective of the program itself, yes. But usually it is not
    the actual starting point.

    > now questions are
    >
    > 1) What exactly is the correct answer..????


    It is user _defined_ but it _must_ be present to produce a executable
    program. Also the Standard explicitly documents the portable forms for
    main()'s signature. Implementations may allow other forms.

    > 2) in free standing implementations is main the starting point
    > of execution..????


    Not necessarily, though often a function called main() is still used for
    the sake of convention.

    Under hosted implementations, typically a short piece of "start-up code"
    is responsible for calling main(). A freestanding program typically has
    to itself implement this start-up code, as well as the code that
    executes after a return from main(), if that is applicable.
     
    santosh, Nov 29, 2007
    #4
  5. wrote:
    > Hi all,
    >
    > I have heard many discussions among my colleagues that main is a user
    > defined function or not.


    Why? Did you get tired of counting the dancing angels on the needle?
     
    Martin Ambuhl, Nov 29, 2007
    #5
  6. wrote:
    > Hi all,
    >
    > I have heard many discussions among my colleagues that main is a user
    > defined function or not.


    The answer to that obviously depends on exactly what you mean by "a user
    defined function". C doesn't define the term, so its up to you and your
    colleagues to define what you mean by it; once you've done that, you'll
    know whether or not main is one.
     
    J. J. Farrell, Nov 29, 2007
    #6
  7. jaysome Guest

    On Thu, 29 Nov 2007 02:26:11 -0800 (PST), wrote:

    >Hi all,
    >
    >I have heard many discussions among my colleagues that main is a user
    >defined function or not.


    More exactly, the definition of main is user-defined. Its prototype is
    not declared by the implementation, and it follows that you--the
    user--must not declare its prototype.

    Your definition of main must conform to the C Standard, which
    specifies the following acceptable choices:

    int main(void) { /* ... */ }
    int main(int argc, char *argv[]) { /* ... */ }

    One of the most important things to notice about these definitions is
    that main returns a type of int. Defining main to return any type
    other than int is considered to be undefined behavior; some compilers
    gladly accept a definition of main that defines a return type other
    than int, such as void, but you should avoid using such a definition,
    for the following reasons: 1) a return type other than int (e.g.,
    void) is considered undefined behavior because it does not conform to
    the C Standard, and 2) those same compilers will gladly accept a
    return type of int, which does conform to the C Standard.

    After you have defined your main function to return a type of int,
    another important thing to keep in mind is that the actual value of
    the return type can take on only a few specific values. These values
    are 0, EXIT_SUCCESS and EXIT_FAILURE. Period. The latter two values
    are macros defined in the Standard C header file <stdlib.h>. Returning
    a value of 0 has the same effect as returning a value of EXIT_SUCCESS,
    as far as the C Standard is concerned.

    Whether or not you choose to return 0 or EXIT_SUCCESS from main is a
    matter of preference and perhaps style. My preference is to return 0
    if I never return an error status (i.e., EXIT_FAILURE) and return
    EXIT_SUCCESS if I do return an error status. One benefit of this is
    that for simple "Hello World!" programs, I can return 0 and do not
    have to include <stdlib.h>. For example:

    #include <stdio.h>
    int main(void)
    {
    printf("Hello World!\n");
    return 0;
    }

    If I return EXIT_FAILURE somewhere, someplace, then my (pseudo)code
    might look something like this:

    #include <stdlib.h>
    int main(void)
    {
    if ( /* some expression */ )
    {
    /* error */
    return EXIT_FAILURE;
    }
    else
    {
    /* success */
    return EXIT_SUCCESS;
    }
    }

    One final note about the function main. Sometimes its return type of
    int simply does not make sense. Most if not all embedded software
    written in C has a main function that looks something like this:

    int main(void)
    {
    /* initialize stuff */
    for ( ; ; )
    {
    /* do something */
    }
    return 0;
    }

    As long as the "do something" part does not contain a break statement
    (which is hardly if never the case), the return statement will never
    be executed. In that sense, and in the sense that there is no
    environment to which to return a value on many if not most embedded
    applications, it doesn't really make sense to define main to return a
    type of int, does it? Some compilers and source code analyzers will
    flag a warning about the return statement in the above example, saying
    something to the extent that it is unreachable code. What really makes
    the most sense in this instance is that the return type of main is
    void, e.g.:

    void main(void)
    {
    /* initialize stuff */
    for ( ; ; )
    {
    /* do something */
    }
    }

    If and when you ever run into such a situation, just make sure that
    your compiler provides the option to define main with a return type of
    void. Of course your code will be non-portable, but you should only be
    using such compiler-specific options in an application domain such as
    the embedded world, which is highly non-portable (though not totally
    non-portable), especially for any non-trivial application.

    Best regards
    --
    jay

    >
    >arguments in favour:-
    >
    >1) if it is built in function it must be defined in some header file
    >but it is n't.
    >
    >2) every time we are defining how the main function should work
    >
    >arguments against:-
    >
    >1)user defined functions cannot be called from command line
    >
    >some say that it is a special user defined function ,which is the
    >starting point of execution of a C program.
    >
    >now questions are
    >
    >1) What exactly is the correct answer..????
    >2) in free standing implementations is main the starting point
    > of execution..????
     
    jaysome, Nov 30, 2007
    #7
  8. Guest

    On Nov 30, 12:09 am, jaysome <> wrote:
    > More exactly, the definition of main is user-defined. Its prototype is
    > not declared by the implementation, and it follows that you--the
    > user--must not declare its prototype.



    Why would there be a problem providing a prototype for main(), so long
    as it matches one of the two acceptable forms, and matches the
    eventual definition?
     
    , Nov 30, 2007
    #8
  9. santosh Guest

    wrote:

    > On Nov 30, 12:09 am, jaysome <> wrote:
    >> More exactly, the definition of main is user-defined. Its prototype
    >> is not declared by the implementation, and it follows that you--the
    >> user--must not declare its prototype.

    >
    > Why would there be a problem providing a prototype for main(), so long
    > as it matches one of the two acceptable forms, and matches the
    > eventual definition?


    In fact if you use the "newer" style definition for writing main() you
    automatically provide the prototype too. A separate prototype for
    main() only rarely needed since main() is seldom called from other
    functions.
     
    santosh, Nov 30, 2007
    #9
  10. jaysome <> writes:
    > On Thu, 29 Nov 2007 02:26:11 -0800 (PST), wrote:
    >
    >>Hi all,
    >>
    >>I have heard many discussions among my colleagues that main is a user
    >>defined function or not.

    >
    > More exactly, the definition of main is user-defined. Its prototype is
    > not declared by the implementation,


    Right.

    > and it follows that you--the
    > user--must not declare its prototype.


    No, that doesn't follow at all. It's perfectly legal to declare a
    separate prototype for main:

    int main(void);

    int main(void)
    {
    /* ... */
    }

    though that's useful only if you call main recursively. But even
    without a separate prototype, the definition of main itself provides a
    prototype.

    > Your definition of main must conform to the C Standard, which
    > specifies the following acceptable choices:
    >
    > int main(void) { /* ... */ }
    > int main(int argc, char *argv[]) { /* ... */ }
    >
    > One of the most important things to notice about these definitions is
    > that main returns a type of int. Defining main to return any type
    > other than int is considered to be undefined behavior; some compilers
    > gladly accept a definition of main that defines a return type other
    > than int, such as void, but you should avoid using such a definition,
    > for the following reasons: 1) a return type other than int (e.g.,
    > void) is considered undefined behavior because it does not conform to
    > the C Standard, and 2) those same compilers will gladly accept a
    > return type of int, which does conform to the C Standard.


    That's not *quite* correct. A hosted implementation is allowed to
    support other forms of main. For example, if a compiler supports and
    documents "void main(void)", then a program that uses that form is
    valid *for that implementation*, and does not invoke undefined
    behavior. But your other reasons to avoid forms other than the two
    guaranteed ones are valid.

    > After you have defined your main function to return a type of int,
    > another important thing to keep in mind is that the actual value of
    > the return type can take on only a few specific values. These values
    > are 0, EXIT_SUCCESS and EXIT_FAILURE. Period. The latter two values
    > are macros defined in the Standard C header file <stdlib.h>. Returning
    > a value of 0 has the same effect as returning a value of EXIT_SUCCESS,
    > as far as the C Standard is concerned.


    Returning a value other than 0, EXIT_SUCCESS, or EXIT_FAILURE is
    merely non-portable, not illegal.

    [snip]

    > One final note about the function main. Sometimes its return type of
    > int simply does not make sense. Most if not all embedded software
    > written in C has a main function that looks something like this:
    >
    > int main(void)
    > {
    > /* initialize stuff */
    > for ( ; ; )
    > {
    > /* do something */
    > }
    > return 0;
    > }
    >
    > As long as the "do something" part does not contain a break statement
    > (which is hardly if never the case), the return statement will never
    > be executed. In that sense, and in the sense that there is no
    > environment to which to return a value on many if not most embedded
    > applications, it doesn't really make sense to define main to return a
    > type of int, does it? Some compilers and source code analyzers will
    > flag a warning about the return statement in the above example, saying
    > something to the extent that it is unreachable code. What really makes
    > the most sense in this instance is that the return type of main is
    > void, e.g.:
    >
    > void main(void)
    > {
    > /* initialize stuff */
    > for ( ; ; )
    > {
    > /* do something */
    > }
    > }
    >
    > If and when you ever run into such a situation, just make sure that
    > your compiler provides the option to define main with a return type of
    > void. Of course your code will be non-portable, but you should only be
    > using such compiler-specific options in an application domain such as
    > the embedded world, which is highly non-portable (though not totally
    > non-portable), especially for any non-trivial application.


    Embedded systems typically have freestanding implementations. In a
    freestanding implementation, the program's entry point is entirely
    implementation-defined; it needn't even be called "main".

    [...]

    --
    Keith Thompson (The_Other_Keith) <>
    Looking for software development work in the San Diego area.
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Nov 30, 2007
    #10
  11. James Kuyper Guest

    wrote:
    > Hi all,
    >
    > I have heard many discussions among my colleagues that main is a user
    > defined function or not.
    >
    > arguments in favour:-
    >
    > 1) if it is built in function it must be defined in some header file
    > but it is n't.


    That argument doesn't hold up. Depending upon the implementation, there
    might be quite a few functions that are not declared in standard
    headers, but which are also not user defined. These might be hidden
    helper functions that are called by routines from the C standard
    library. These might be routines which are executed to emulate in
    software C features for which native hardware support is lacking on a
    particular platform, such as long-double _Complex arithmetic on a
    platform which has native support only for 8-bit integer arithmetic.

    > 2) every time we are defining how the main function should work


    That is precisely the definition of "user defined", and the only
    relevant argument you've cited.

    > arguments against:-
    >
    > 1)user defined functions cannot be called from command line


    That's also true for main(). What you call from the command line is the
    entire program, which eventually results in a call to main(); and later
    on, it eventually may result in a call to any of the other user-defined
    functions in the program.
     
    James Kuyper, Nov 30, 2007
    #11
  12. pete Guest

    jaysome wrote:

    > More exactly, the definition of main is user-defined. Its prototype is
    > not declared by the implementation, and it follows that you--the
    > user--must not declare its prototype.


    Because its prototype is not declared by the implementation,
    it follows that you--the user--may declare its prototype.

    --
    pete
     
    pete, Nov 30, 2007
    #12
  13. jaysome wrote:
    > On Thu, 29 Nov 2007 02:26:11 -0800 (PST), wrote:
    >
    >> Hi all,
    >>
    >> I have heard many discussions among my colleagues that main is a user
    >> defined function or not.

    >
    > More exactly, the definition of main is user-defined. Its prototype is
    > not declared by the implementation, and it follows that you--the
    > user--must not declare its prototype.
    >
    > Your definition of main must conform to the C Standard, which
    > specifies the following acceptable choices:
    >
    > int main(void) { /* ... */ }
    > int main(int argc, char *argv[]) { /* ... */ }
    >
    > One of the most important things to notice about these definitions is
    > that main returns a type of int. Defining main to return any type
    > other than int is considered to be undefined behavior; some compilers
    > gladly accept a definition of main that defines a return type other
    > than int, such as void, but you should avoid using such a definition,
    > for the following reasons: 1) a return type other than int (e.g.,
    > void) is considered undefined behavior because it does not conform to
    > the C Standard, and 2) those same compilers will gladly accept a
    > return type of int, which does conform to the C Standard.
    >
    > After you have defined your main function to return a type of int,
    > another important thing to keep in mind is that the actual value of
    > the return type can take on only a few specific values. These values
    > are 0, EXIT_SUCCESS and EXIT_FAILURE. Period. The latter two values
    > are macros defined in the Standard C header file <stdlib.h>. Returning
    > a value of 0 has the same effect as returning a value of EXIT_SUCCESS,
    > as far as the C Standard is concerned.
    >
    > Whether or not you choose to return 0 or EXIT_SUCCESS from main is a
    > matter of preference and perhaps style. My preference is to return 0
    > if I never return an error status (i.e., EXIT_FAILURE) and return
    > EXIT_SUCCESS if I do return an error status. One benefit of this is
    > that for simple "Hello World!" programs, I can return 0 and do not
    > have to include <stdlib.h>. For example:
    >
    > #include <stdio.h>
    > int main(void)
    > {
    > printf("Hello World!\n");
    > return 0;
    > }
    >
    > If I return EXIT_FAILURE somewhere, someplace, then my (pseudo)code
    > might look something like this:
    >
    > #include <stdlib.h>
    > int main(void)
    > {
    > if ( /* some expression */ )
    > {
    > /* error */
    > return EXIT_FAILURE;
    > }
    > else
    > {
    > /* success */
    > return EXIT_SUCCESS;
    > }
    > }
    >
    > One final note about the function main. Sometimes its return type of
    > int simply does not make sense. Most if not all embedded software
    > written in C has a main function that looks something like this:
    >
    > int main(void)
    > {
    > /* initialize stuff */
    > for ( ; ; )
    > {
    > /* do something */
    > }
    > return 0;
    > }
    >
    > As long as the "do something" part does not contain a break statement
    > (which is hardly if never the case), the return statement will never
    > be executed. In that sense, and in the sense that there is no
    > environment to which to return a value on many if not most embedded
    > applications, it doesn't really make sense to define main to return a
    > type of int, does it? Some compilers and source code analyzers will
    > flag a warning about the return statement in the above example, saying
    > something to the extent that it is unreachable code. What really makes
    > the most sense in this instance is that the return type of main is
    > void, e.g.:
    >
    > void main(void)
    > {
    > /* initialize stuff */
    > for ( ; ; )
    > {
    > /* do something */
    > }
    > }
    >
    > If and when you ever run into such a situation, just make sure that
    > your compiler provides the option to define main with a return type of
    > void. Of course your code will be non-portable, but you should only be
    > using such compiler-specific options in an application domain such as
    > the embedded world, which is highly non-portable (though not totally
    > non-portable), especially for any non-trivial application.
    >
    > Best regards


    "Main" is "main" only in C. Other languages all have their own ways of letting
    the linker and loader know were the system needs to jump (function call) to to
    start execution. C could have used a switch to give the name of the function to
    call to begin execution, with "main" being the default value, there is nothing
    special about the name.

    Now as to the two forms of "main" the loader likely passes the arguments even if
    you ignore them, no harm in this as they pop off the stack upon return. Other
    O/S may not even have a concept of argument values but the loader would still
    have to pass a zero count and a null pointer on such systems as C requires it.
    Or the complier might have to fake those depending on the implementation, but it
    would be implementation dependent.

    I'll leave it to the experts to say what might happen, besides the obvious
    breaking of a script, if you don't return a value.
     
    Golden California Girls, Dec 1, 2007
    #13
  14. Thad Smith Guest

    Ian Collins wrote:
    > wrote:


    >> I have heard many discussions among my colleagues that main is a user
    >> defined function or not.


    It is programmer defined because the programmer writes the code for
    main, which defines it.

    >> 2) in free standing implementations is main the starting point
    >> of execution..????

    >
    > In a freestanding environment, the he name of the function called at
    > program startup is implementation defined.


    This is true. For freestanding implementations the entry routine is
    normally main, with no parameters and no return value.


    --
    Thad
     
    Thad Smith, Dec 1, 2007
    #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. John M
    Replies:
    1
    Views:
    1,310
    Kumar Reddi
    May 29, 2005
  2. Hal Styli
    Replies:
    14
    Views:
    1,716
    Old Wolf
    Jan 20, 2004
  3. Frederick Ding

    int main() or int main(void)?

    Frederick Ding, Dec 3, 2005, in forum: C Programming
    Replies:
    10
    Views:
    675
  4. Ravi
    Replies:
    17
    Views:
    951
    Kenneth Brody
    Apr 1, 2006
  5. ravi
    Replies:
    28
    Views:
    1,029
    Richard Heathfield
    Sep 26, 2007
Loading...

Share This Page