Thread: A short question!

  1. #1
    Infant of C
    Join Date
    May 2010
    Location
    Karachi, Pakistan
    Posts
    118

    A short question!

    I just wanna ask that what is the difference between

    void main(void)

    and

    main()

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    void main (void) is a function prototype

    main() is a function call.

  3. #3
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Have a look at the FAQ to start.

    Edit:

    See also the following entries from the comp.lang.c FAQ:

    Last edited by kermit; 08-28-2010 at 02:38 PM.

  4. #4
    Infant of C
    Join Date
    May 2010
    Location
    Karachi, Pakistan
    Posts
    118
    Quote Originally Posted by CommonTater View Post
    void main (void) is a function prototype

    main() is a function call.
    I know that very well. I just wanted to ask that is this the same case with main() because in our code we don't need to declare main()'s prototype.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Yep... main is just a subrotine like all others... It's prototype is in the language statup code.

  6. #6
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Quote Originally Posted by C_programmer.C View Post
    I know that very well. I just wanted to ask that is this the same case with main() because in our code we don't need to declare main()'s prototype.
    Without declaring the return type, you are implying that main returns int. This is acceptable under the C89 standard, but not under C99. It is probably best to explicitly declare the int return type.

  7. #7
    Infant of C
    Join Date
    May 2010
    Location
    Karachi, Pakistan
    Posts
    118
    you mean that main should be declared as follow

    void main()

  8. #8
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Quote Originally Posted by C_programmer.C View Post
    you mean that main should be declared as follow

    void main()

    No, that is not at all what I mean. Declaring main as

    Code:
    void main
    is almost never correct. Certainly for the typical user with a full operating system, and the standard C implementation, it is incorrect. main returns an int. See the FAQ (11.12b) for when main might not return int.

  9. #9
    Infant of C
    Join Date
    May 2010
    Location
    Karachi, Pakistan
    Posts
    118
    what i understand from the FAQs is that code must be like this
    Code:
    int main(void)
    {
    .
    .
    .
    .
    .
    return(0);
    }

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    That is one of the correct formats. Void main() is never correct in C.

  11. #11
    Infant of C
    Join Date
    May 2010
    Location
    Karachi, Pakistan
    Posts
    118
    Aha! that's why my compiler always warn me "main should return a value".
    Coming back to me question, what i understand from all this discussion that
    main()
    means that i am clling the main function that is already declared in start up code.
    while in
    void main(void)
    i am re-declaring main's prototype in wrong way!

  12. #12
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Quote Originally Posted by Adak View Post
    That is one of the correct formats. Void main() is never correct in C.
    As I mentioned, void main is incorrect for a hosted environment. The same does not apply for a freestanding environment, but is "implementation defined."

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  2. Accessing Structures Inside Structures
    By Mellowz in forum C Programming
    Replies: 1
    Last Post: 01-13-2008, 03:55 AM
  3. Simple Short Class Question
    By Travis Dane in forum C++ Programming
    Replies: 2
    Last Post: 12-24-2002, 07:12 PM
  4. traversal of tree (short question)
    By mackol in forum C Programming
    Replies: 5
    Last Post: 11-25-2002, 08:41 AM
  5. Replies: 1
    Last Post: 01-23-2002, 03:34 AM