Thread: int main (void) vs int main ( )

  1. #1
    Registered User
    Join Date
    Jul 2019
    Posts
    5

    int main (void) vs int main ( )

    hi
    can anyone explain the difference between
    int main (void) vs int main ( )

    in a simple way?

  2. #2
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    728
    Quote Originally Posted by monther View Post
    hi
    can anyone explain the difference between
    int main (void) vs int main ( )

    in a simple way?
    Effectively, for C, there is no difference. The assembler code from gcc is the same from both on my system.

    For main() and all other functions, I strongly recommend using full prototypes, and definitions.

    Code:
    int main(void){...}
    
    // preferred over
    
    int main(){...}

  3. #3
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,750
    Quote Originally Posted by rstanley View Post
    Effectively, for C, there is no difference. The assembler code from gcc is the same from both on my system.
    The C11 standard would disagree and what gcc does is immaterial.

    For functions other than main(void) gcc on my system does produce different ASM for fn() and fn(void)

    Edit:

    1 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; or in some other implementation-defined manner.
    Therefore int main() is implemenation-defined. Other functions are treated differently, but I can assure you that int fn() vs. int fn(void) generates different ASM using my gcc.

    E.g.
    Code:
    #include <stdio.h>
    
    int fn()
    {
        return 55;
    }
    
    int main(void)
    {
        printf("%d\n", fn(78787));
    }
    Is 100% compliant with the C11 standard and gcc produces different ASM for int fn(void) even if fn is called with no parameters. (If fn is int fn(void) then of course fn(78787) is an error, but calling it as fn() produces different ASM)
    Last edited by Hodor; 04-04-2020 at 07:32 AM.

  4. #4
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    728
    And as you quoted from C11, "5.1.2.2.1 Program startup"
    It clearly shows main() defined with "void" as the parameter list, when no parameters are specified.

    I stand by my statement, in a hosted environment:
    For main() and all other functions, I strongly recommend using full prototypes, and definitions.

  5. #5
    Registered User
    Join Date
    Dec 2017
    Posts
    945
    From C11 draft standard:
    Quote Originally Posted by n1548
    6.7.3:14 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.
    If you want the truth to stand clear before you, never be for or against. - Sent-ts'an

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,777
    Yeah, I'm of the opinion that compilers that treat an empty identifier list in a function definition placed before another function definition as meaning "no information about the number or types of the parameters is supplied" when the definition is taken as serving as a forward declaration are likely non-conforming, but in theory this is academic because identifier lists are obsolescent, which leads me to my rant:

    They didn't remove identifier lists in C17 even though there probably hasn't been new code written with non-empty identifier lists in over two decades, and my guess is that's because there's plenty of new code that uses empty identifer lists in function definitions or mistakenly in forward declarations to mean "no parameters". At some point they should admit that this is a mess that could be cleaned up by simply removing identifier lists and re-introducing empty identifier lists as a special case having semantics identical to void parameter type lists.

    It'll upset those who have continued to use empty identifier lists in forward declarations to mean "no information about the number or types of the parameters is supplied", but that hasn't been good practice for awhile, and you get no sympathy for having the rug pulled from under you when you insist on using a feature that has been obsolescent for over two decades.
    Last edited by laserlight; 04-04-2020 at 05:37 PM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bit confused about void main() vs int main()
    By pyroknife in forum C Programming
    Replies: 9
    Last Post: 01-08-2013, 02:53 AM
  2. [DEBATE]int main VS void main?
    By sudox in forum C++ Programming
    Replies: 20
    Last Post: 11-26-2010, 03:18 PM
  3. Why do people still use main() or main(void)?
    By Christopher2222 in forum C Programming
    Replies: 18
    Last Post: 06-06-2007, 06:34 PM
  4. A question about void(main) and int(main)
    By edd1986 in forum C Programming
    Replies: 2
    Last Post: 03-05-2005, 03:18 PM
  5. void main(), int main(), argc, argv[]????
    By Jonny M in forum C Programming
    Replies: 3
    Last Post: 03-06-2002, 09:12 AM

Tags for this Thread