Like Tree1Likes
  • 1 Post By laserlight

Prototypes for main() function

This is a discussion on Prototypes for main() function within the C Programming forums, part of the General Programming Boards category; According to my knowledge, there are following two valid prototypes for main() function in C : Code: int main(void) int ...

  1. #1
    Registered User poornaMoksha's Avatar
    Join Date
    Sep 2011
    Location
    India
    Posts
    41

    Prototypes for main() function

    According to my knowledge, there are following two valid prototypes for main() function in C :
    Code:
    int main(void)
    int main(int argc, char *argv[])
    Now, If i try to create two functions with same name 'func' but different arguments then gcc gives the following error :

    Code:
    cmd.c:4: error: conflicting types for ‘func’
    cmd.c:3: note: previous declaration of ‘func’ was here
    My question is that how is this possible with main() function to have two different prototypes?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,461
    Quote Originally Posted by poornaMoksha
    My question is that how is this possible with main() function to have two different prototypes?
    Ah, but they are not prototypes. Rather, they are the possible ways to declare the main function, guaranteed by the C standard to be valid when programming for a hosted environment.

    If you wishes, you could declare a prototype for main, but you have to pick one, and it should match your definition (implementation) of main. However, since you are unlikely to want to call the main function from your own code, such a prototype would not be very useful.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User poornaMoksha's Avatar
    Join Date
    Sep 2011
    Location
    India
    Posts
    41
    Hmm.. I am sorry but I am still confused. I believe that the prototype of the main() function has to be present somewhere as we are only defining it. Or, is it so that our definition of the main() function serves the purpose of its declaration too?

  4. #4
    Registered User poornaMoksha's Avatar
    Join Date
    Sep 2011
    Location
    India
    Posts
    41
    @Jim
    Sorry I saw your reply a bit late. Thanks for the reply. If this is the case then I assume that main function is always called with two (or three) arguments. Now, suppose I have defined main() with 'void' argument. Then how the main() gets called. If call to main() in this case is possible then how come the call to func() in the following case is not possible :

    Code:
    #include<stdio.h>
    
    int func(void)
    {
       printf("\n inside func() \n");
       return 0;
    }
    
    int main(void)
    {
        func(3);
        return 0;
    }
    as gcc gives the following compilation error :

    Code:
    $ gcc -Wall cmd.c -o cmd
    cmd.c: In function ‘main’:
    cmd.c:12: error: too many arguments to function ‘func’

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,461
    Quote Originally Posted by poornaMoksha
    I believe that the prototype of the main() function has to be present somewhere as we are only defining it.
    You are mistaken: you do not need to have a prototype (as in a forward declaration) in order to define a function.

    Quote Originally Posted by poornaMoksha
    Or, is it so that our definition of the main() function serves the purpose of its declaration too?
    Yes, your definition of main is itself a declaration of the function since all definitions are declarations.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    2,698
    The function main does not actually have any prototypes. This function is "special" it requires no prototype. Here is the description of main from the C++ standard:
    3.6
    Start and termination
    3.6.1
    Main function
    [basic.start[basic.start.main1
    A program shall contain a global function called main, which is the designated start of the program. Iis implementation-defined whether a program in a freestanding environment is required to define a maifunction. [ Note: In a freestanding environment, start-up and termination is implementation-defined; startup contains the execution of constructors for objects of namespace scope with static storage durationtermination contains the execution of destructors for objects with static storage duration. — end note ]

    2
    An implementation shall not predefine the main function. This function shall not be overloaded. It shall
    have a return type of type int, but otherwise its type is implementation-defined. All implementations shall
    allow both of the following definitions of main:
    int main() { /* ...
    */ }
    and
    int main(int argc, char* argv[]) { /* ...
    */ }
    In the latter form argc shall be the number of arguments passed to the program from the environ-
    ment in which the program is run. If argc is nonzero these arguments shall be supplied in argv[0]
    through argv[argc-1] as pointers to the initial characters of null-terminated multibyte strings (ntmbs
    s) (17.5.2.1.4.2) and argv[0] shall be the pointer to the initial character of a ntmbs that represents the
    name used to invoke the program or "". The value of argc shall be non-negative. The value of argv[argc]
    shall be 0. [ Note: It is recommended that any further (optional) parameters be added after argv. — end
    note ]
    3
    The function main shall not be used within a program. The linkage (3.5) of main is implementation-defined.
    A program that defines main as deleted or that declares main to be inline, static, or constexpr is ill-
    formed. The name main is not otherwise reserved. [ Example: member functions, classes, and enumerations
    can be called main, as can entities in other namespaces. — end example ]
    4
    Terminating the program without leaving the current block (e.g., by calling the function std::exit(int) (18.does not destroy any objects with automatic storage duration (12.4). If std::exit is called to end a pro-
    gram during the destruction of an object with static or thread storage duration, the program has undefined
    behavior.
    5
    A return statement in main has the effect of leaving the main function (destroying any objects with automatic
    storage duration) and calling std::exit with the return value as the argument. If control reaches the end
    of main without encountering a return statement, the effect is that of executing
    return 0;
    Sorry about deleting the post but I was having some problems changing it, so I thought I would try again. Also the above is from the C++ standard, and not necessarily relevant to this forum, so here is the C standard version of the same:
    1
    2
    5.1.2.2.1 Program startup
    The function called at program startup is named main. The implementation declares no
    prototype for this function. It shall be defined with a return type of int and with no
    parameters:
    int main(void) { /* ... */ }
    or with two parameters (referred to here as argc and argv, though any names may be
    used, as they are local to the function in which they are declared):
    int main(int argc, char *argv[]) { /* ... */ }
    or equivalent;10) or in some other implementation-defined manner.
    If they are declared, the parameters to the main function shall obey the following
    constraints:
    — The value of argc shall be nonnegative.
    — argv[argc] shall be a null pointer.
    — If the value of argc is greater than zero, the array members argv[0] through
    argv[argc-1] inclusive shall contain pointers to strings, which are given
    implementation-defined values by the host environment prior to program startup. The
    intent is to supply to the program information determined prior to program startup
    from elsewhere in the hosted environment. If the host environment is not capable of
    supplying strings with letters in both uppercase and lowercase, the implementation
    shall ensure that the strings are received in lowercase.
    — If the value of argc is greater than zero, the string pointed to by argv[0]
    represents the program name; argv[0][0] shall be the null character if the
    program name is not available from the host environment. If the value of argc is
    greater than one, the strings pointed to by argv[1] through argv[argc-1]
    represent the program parameters.
    — The parameters argc and argv and the strings pointed to by the argv array shall
    be modifiable by the program, and retain their last-stored values between program
    startup and program termination.

    Jim

  7. #7
    Registered User poornaMoksha's Avatar
    Join Date
    Sep 2011
    Location
    India
    Posts
    41
    @Jim
    Sorry I saw your reply a bit late. Thanks for the reply. If this is the case then I assume that main function is always called with two (or three) arguments. Now, suppose I have defined main() with 'void' argument. Then how the main() gets called. If call to main() in this case is possible then how come the call to func() in the following case is not possible :

    Code:
    #include<stdio.h>
    
    int func(void)
    {
       printf("\n inside func() \n");
       return 0;
    }
    
    int main(void)
    {
        func(3);
        return 0;
    }
    as gcc gives the following compilation error :

    Code:
    $ gcc -Wall cmd.c -o cmd
    cmd.c: In function ‘main’:
    cmd.c:12: error: too many arguments to function ‘func’

  8. #8
    Registered User
    Join Date
    May 2010
    Posts
    2,698
    Your problem is that you defined function func() to take no arguments (void), but you try to pass an argument into the function. If you want to pass variables into a function you must tell the compiler the type and number of arguments to accept.

    Jim

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,461
    Quote Originally Posted by poornaMoksha
    If this is the case then I assume that main function is always called with two (or three) arguments. Now, suppose I have defined main() with 'void' argument. Then how the main() gets called.
    No, the compiler can figure out how to set it up such that your main function is invoked with the number of parameters that you provide. So, if you define main as having no parameters, then it will be such that your main function is invoked with no arguments.

    Quote Originally Posted by jimblumberg
    This function is "special" it requires no prototype.
    With a few exceptions (e.g., mutually recursive functions), most user defined functions don't require prototypes.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User poornaMoksha's Avatar
    Join Date
    Sep 2011
    Location
    India
    Posts
    41
    So, Does this mean that the call to main() is decided dynamicly? ie the compiler first parses the way we have defined the main() function in our code and then sets up the call to main() function accordingly. Is it so?

  11. #11
    Registered User
    Join Date
    May 2010
    Posts
    2,698
    With a few exceptions (e.g., mutually recursive functions), most user defined functions don't require prototypes.
    True but then they must be defined/implemented before they are used.

    Jim

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,461
    Quote Originally Posted by poornaMoksha
    the compiler first parses the way we have defined the main() function in our code and then sets up the call to main() function accordingly. Is it so?
    Yes, at least from our point of view as C programmers. What actually happens is implementation defined.
    iMalc likes this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    Registered User
    Join Date
    May 2010
    Posts
    2,698
    So, Does this mean that the call to main() is decided dynamicly? ie the compiler first parses the way we have defined the main() function in our code and then sets up the call to main() function accordingly. Is it so?
    How the compiler actually determines which main you are using differs between compilers. Some compilers may use some sort of macro to determine which startup routine to call depending on how you use the function. Some may use other ways. Unless you want to create a compiler it doesn't really matter how the compiler generates the proper code, just use the proper form of main for your program. If you want to accept command line arguments into your program use the two arguments, otherwise use void.

    Jim

  14. #14
    Registered User poornaMoksha's Avatar
    Join Date
    Sep 2011
    Location
    India
    Posts
    41
    Thanks a lot guys for your quick help!!!!
    I got the answer!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with Function Prototypes
    By Valandu in forum C Programming
    Replies: 16
    Last Post: 10-14-2011, 10:11 PM
  2. function prototypes
    By kawaikx15 in forum C Programming
    Replies: 1
    Last Post: 06-28-2011, 09:20 PM
  3. Function Prototypes
    By Kryota in forum C++ Programming
    Replies: 11
    Last Post: 12-28-2007, 03:52 AM
  4. 2 function prototypes
    By newbie101 in forum C++ Programming
    Replies: 3
    Last Post: 05-09-2006, 05:49 AM
  5. Replies: 3
    Last Post: 11-29-2004, 08:43 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21