C99 and int main()

This is a discussion on C99 and int main() within the C Programming forums, part of the General Programming Boards category; This is a rather pedantic nitpick, but my flame retardant suit is fully donned. From the FAQ : "(C) The ...

  1. #1
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869

    C99 and int main()

    This is a rather pedantic nitpick, but my flame retardant suit is fully donned.

    From the FAQ:

    "(C) The difference between int main() and int main(void)

    A common misconception for C programmers, is to assume that a function prototyped as follows takes no arguments:

    int foo();

    In fact, this function is deemed to take an unknown number of arguments. Using the keyword void within the brackets is the correct way to tell the compiler that the function takes NO arguments."

    While it is true that the prototype int foo(); means that the function takes an unknown number of arguments, it is not true when it comes to the definition:
    Code:
    int foo()
    {
         return 0;
    }
    The above function is defined to take exactly zero arguments. This is just as valid in C99. Indeed, in the C99 standard, it even has int main() in one or more code examples.

    From the above FAQ also:

    "Under C89, main() is acceptable, although it is advisable to use the C99 standard, under which only these are acceptable:

    int main ( void )
    int main ( int argc, char *argv[] )"

    From how I interpret the standard, it is perfectly acceptable in C99 to use:
    Code:
    int main()
    {
        
    }
    See ISO 9899:1999 section 6.5.4.3.7 and 6.7.5.3.16 for examples of using int main() { }.

    From ISO 9899:1999 section 6.7.5.3.10 (bold and underline is mine):

    "An identifier list declares only the identifiers of the parameters of the function. An empty list in a function declarator that is part of a definition of that function specifies that the function has no parameters. The empty list in a function declarator that is not part of a definition of that function specifies that no information about the number or types of the parameters is supplied."

    It also has a footnote for that paragraph directing us to section 6.11.3:

    "The use of function declarators with empty parentheses (not prototype-format parameter type declarators) is an obsolescent feature."

    So, while it is considered obsolescent, it has not been made illegal in C99.

    Of course, if you were to prototype main, you would need int main(void);

  2. #2
    C/C++Newbie Antigloss's Avatar
    Join Date
    May 2005
    Posts
    212
    in fact, int foo() is not prototype, it's just an old style of function declaration

  3. #3
    C/C++Newbie Antigloss's Avatar
    Join Date
    May 2005
    Posts
    212

    ff

    Quote Originally Posted by C Primer Plus
    Older C supported a more limited form of function declaration in which you just specified the return type but omitted describing the arguments:

    void butler();

    Older C code uses function declarations like the preceding one instead of function prototypes. The C90 and C99 standards recognize this older form but indicate it will be phased out in time, so don't use it. If you inherit some legacy C code, you may want to convert the old-style declarations to prototypes.

    edit
    Last edited by Antigloss; 09-19-2005 at 12:20 AM. Reason: dff

  4. #4
    Registered User
    Join Date
    Jul 2005
    Location
    Transcarpathia
    Posts
    49
    by the way, FAQ mentions that in C99 return 0 is implied if no return is
    present and main() declared as int main().
    in what section is it, by the way ?
    are there rules in C99 about implicit return values ?

  5. #5
    C/C++Newbie Antigloss's Avatar
    Join Date
    May 2005
    Posts
    212
    Quote Originally Posted by valenok
    are there rules in C99 about implicit return values ?
    yes.

  6. #6
    Registered User
    Join Date
    Jul 2005
    Location
    Transcarpathia
    Posts
    49
    please, tell me the section where they are described

  7. #7
    C/C++Newbie Antigloss's Avatar
    Join Date
    May 2005
    Posts
    212
    5.1.2.2.3 Program termination
    If the return type of the main function is a type compatible with int, a return from the
    initial call to the main function is equivalent to calling the exit function with the value
    returned by the main function as its argument; reaching the } that terminates the
    main function returns a value of 0
    . If the return type is not compatible with int, the
    termination status returned to the host environment is unspecified.
    Last edited by Antigloss; 09-19-2005 at 07:10 AM.

  8. #8
    Registered User
    Join Date
    Jul 2005
    Location
    Transcarpathia
    Posts
    49
    does that apply for main() function or for any function ?

  9. #9
    C/C++Newbie Antigloss's Avatar
    Join Date
    May 2005
    Posts
    212
    just for main

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char and int
    By BlaX in forum C Programming
    Replies: 21
    Last Post: 07-11-2009, 01:24 PM
  2. Conversion of pointers to functions
    By hzmonte in forum C Programming
    Replies: 0
    Last Post: 01-20-2009, 01:56 AM
  3. Why do people still use main() or main(void)?
    By Christopher2222 in forum C Programming
    Replies: 18
    Last Post: 06-06-2007, 07:34 PM
  4. Completing this program
    By nesir in forum C Programming
    Replies: 2
    Last Post: 09-17-2006, 08:05 PM
  5. <regex.h> help needed! C slower than Java
    By bobk544 in forum C Programming
    Replies: 9
    Last Post: 02-06-2005, 06:03 AM

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