Thread: main

  1. #1
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138

    main

    There is one thing I never really get. Why do people use int main(void) instead of int main()? I know it explicitly tells there are no parameters, but are there any other benefits?

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    It's better style to be explicit, and the standard says nothing about int main(). The three definitions that the standard specifies are:
    int main ( void )
    int main ( int argc, char *argv[] )
    int main ( int argc, char **argv )

    -Prelude
    My best code is written with the delete key.

  3. #3
    l'Anziano DavidP's Avatar
    Join Date
    Aug 2001
    Location
    Plano, Texas, United States
    Posts
    2,743
    Also, there is a difference between C and C++ isnt there?

    If you do int main () in C, it means the same as int main ( ... ) in C++, right? That is what I heard somewhere...
    My Website

    "Circular logic is good because it is."

  4. #4
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    As far as I know there is no difference between C and C++ at that point.

  5. #5
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    Actually the standard uses an implicit void. Everything you want to know about main() -

    [basic.start.main] 3.6.1 Main function
    1 A program shall contain a global function called main, which is the designated start of the program.
    2 This function is not predefined by the implementation, it cannot be overloaded, and 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 environment 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 (NTMBSs) 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 nonnegative. The value of argv[argc] shall be 0.
    [Note: It is recommended that any further (optional) parameters be added after argv. ]
    3 The function main() shall not be called from within a program. The linkage (3.5) of main() is
    implementation-defined. The address of main() shall not be taken and main() shall not be declared
    inline or static. The name main is not otherwise reserved. [Example: member functions, classes,
    and enumerations can be called main, as can entities in other namespaces. ]
    4 Calling the function
    void exit(int);
    declared in <cstdlib> (18.3) terminates the program without leaving the current block and hence with-out
    destroying any objects with automatic storage duration (12.4). The argument value is returned to the
    program’s environment as the value of the program.
    5 A return statement in main() has the effect of leaving the main function (destroying any objects with
    automatic storage duration) and calling 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;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why To Use int main()
    By year2038bug in forum C Programming
    Replies: 2
    Last Post: 08-19-2005, 01:28 PM
  2. passing params to main()
    By mike11 in forum C++ Programming
    Replies: 14
    Last Post: 06-21-2005, 12:36 PM
  3. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  4. Int Main or Void Main?
    By FromHolland in forum C++ Programming
    Replies: 9
    Last Post: 06-12-2003, 04:29 PM
  5. int main (), main (), main (void), int main (void) HELP!!!
    By SmokingMonkey in forum C++ Programming
    Replies: 7
    Last Post: 05-31-2003, 09:46 PM