int main(void) VS int main()

This is a discussion on int main(void) VS int main() within the C Programming forums, part of the General Programming Boards category; I am a c beginner, I would like to know what is the difference between int main(void) vs int main(). ...

  1. #1
    Max
    Max is offline
    Registered User Max's Avatar
    Join Date
    Jul 2002
    Posts
    110

    Post int main(void) VS int main()

    I am a c beginner, I would like to know what is the difference between int main(void) vs int main().
    I have the feeling it is the same but just to make sure!!!!

  2. #2
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,493
    i think it is the same. i would use int main() myself, since it saves 4 letters typing.

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Whenever I write any function that takes on parameters I always specify 'void'. I just think it makes it obvious to the reader that the function takes no parameters. Just habit I guess.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  4. #4
    Registered User
    Join Date
    Jun 2002
    Posts
    9
    Hi,

    Though I am not very sure of this, I think the use of "void" allows certain old C compilers to handle functions that do not take any arguments. For the new ones this is redundant.

  5. #5
    Max
    Max is offline
    Registered User Max's Avatar
    Join Date
    Jul 2002
    Posts
    110
    Thank you all.....

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Here's an extract from the draft C99 standard:
    5.1.2.2.1 Program startup
    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.
    However, within the document some example code also uses
    >int main()

    Personally, I use
    >int main(void)
    if there are no arguments required.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  7. #7
    Registered User
    Join Date
    Jul 2002
    Posts
    13

    C

    babu,
    in C, if u dont specify any argueemts , it means tht the function will behave normally for any type and number of arguemnts....
    so if u dont want a function to accept atguemtns thn u declare its parameters as "void".

    in c++ , no args means function does not take any arguemsnts.

    so

    void print()
    {
    puts("c is easy.");
    }

    int main()
    {
    print();
    print(1); /* works if print is declared as void print(void) this fails to compile. */
    return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can some one please tell me the cause of the error ?
    By broli86 in forum C Programming
    Replies: 8
    Last Post: 06-26-2008, 08:36 PM
  2. Drawing Program
    By Max_Payne in forum C++ Programming
    Replies: 21
    Last Post: 12-21-2007, 04:34 PM
  3. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  4. getting a headache
    By sreetvert83 in forum C++ Programming
    Replies: 41
    Last Post: 09-30-2005, 05:20 AM
  5. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM

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