CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Aug 2011
    Posts
    80

    int main() / void main()

    Sorry guys,

    i know there are some threads around on the internet sort of explaining why we can not use void main()

    i sort of understand it, but somehow the explanations i found on the internet do not make it crystal clear to me....

    Can someone please tell me in a noob way what the big difference is, both will work, but i read every where the void main() is a absolute NO...

  2. #2
    Join Date
    Aug 2008
    Posts
    902

    Re: int main() / void main()

    The explanation is quite simple. int main is the only correct way to declare main because that's what the language standard says.

  3. #3
    Join Date
    Nov 2003
    Posts
    1,902

  4. #4
    Join Date
    Aug 2011
    Posts
    80

    Re: int main() / void main()

    thanks, i copied some lines:

    Code:
    Actually, main can be declared in any of the four following ways:
    int main()
    int main(void)
    int main(int argc, char *argv[])
    int main(int argc, char** argv)
    Unless a program uses command-line arguments, one of the first two would suffice.
    i've created a program with many functions with: void function(){} The above lines say unless a program uses command-line argumentes int main and int main(void) will be enough.

    i use some user_input in my functions and all my functions are like: void function()

    so what about:

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

  5. #5
    Join Date
    Apr 2008
    Posts
    725

    Re: int main() / void main()

    ??

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

    are for handling command line arguments. You know there's a difference between command line args and user input?

  6. #6
    Join Date
    Aug 2011
    Posts
    80

    Re: int main() / void main()

    Quote Originally Posted by Amleto View Post
    ??

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

    are for handling command line arguments. You know there's a difference between command line args and user input?
    well never used it that way (yet)

  7. #7
    Join Date
    Sep 2011
    Posts
    5

    Re: int main() / void main()

    Because the standard dictates that every C/C++ program must return a value to indicate its success. It also says that a program must return either 0 or EXIT_SUCCESS if the execution was successful. Declaring main() as void doesn't mean the program won't return a value -- it just means that the value it returns will probably not be 0.

    And that's bad because now the operating system thinks that the execution failed. Other horrible things can happen, too.

    A bit of searching found that.

    in simple terms it says main()has to return 0 or EXIT_SUCCESS to show the program has run successfully and void will not let the return of a proper value that is needed
    Last edited by medicalsalt; September 17th, 2011 at 02:36 PM.

  8. #8
    Join Date
    Aug 2011
    Posts
    80

    Re: int main() / void main()

    Quote Originally Posted by medicalsalt View Post
    Because the standard dictates that every C/C++ program must return a value to indicate its success. It also says that a program must return either 0 or EXIT_SUCCESS if the execution was successful. Declaring main() as void doesn't mean the program won't return a value -- it just means that the value it returns will probably not be 0.

    And that's bad because now the operating system thinks that the execution failed. Other horrible things can happen, too.

    A bit of searching found that.

    in simple terms it says main()has to return 0 or EXIT_SUCCESS to show the program has run successfully and void will not let the return of a proper value that is needed
    This was very helpfull, thanks.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured