Thread: Int Main or Void Main?

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    50

    Int Main or Void Main?

    Can someone tell me what the advantage is of using Int Main instead of Void Main? And, does Void Main also return a value in the end? - -

    This may sound kinda noob but I am

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    void main( ) is not valid C or C++. The only valid return type for main is int.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  3. #3
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    int main is standard (look for Salem's avatar), void main is not (and therefore, incorrect). The return value tells the system (and possibly other programs) the exit status of your program.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    50
    #include <iostream.h>
    #include <string.h>
    #include <stdlib.h>

    struct StringData
    {
    char string[100];
    };
    void main()
    {
    StringData data[10];
    for (int x=0;x<9;x++)
    {
    cout<<"Please enter a variable #"<<x<<":";
    cin>>data[x].string;
    }
    cout<<"Data "<<data[1].string<<endl;
    cout<<"Data "<<data[2].string<<endl;
    cout<<"Data "<<data[3].string<<endl;
    system("pause");
    }

    ________________

    These are both the same, only once thing is different!
    The upper code uses VOID MAIN and the bottom code
    use INT MAIN. They both work, no difference at all! Does
    it matter what I use??
    ________________

    #include <iostream.h>
    #include <string.h>
    #include <stdlib.h>

    struct StringData
    {
    char string[100];
    };
    int main()
    {
    StringData data[10];
    for (int x=0;x<9;x++)
    {
    cout<<"Please enter a variable #"<<x<<":";
    cin>>data[x].string;
    }
    cout<<"Data "<<data[1].string<<endl;
    cout<<"Data "<<data[2].string<<endl;
    cout<<"Data "<<data[3].string<<endl;
    system("pause");
    }

  5. #5
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    You should always use int main( ), because void main( ) is not valid C++.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    50
    OKay, thx for the advice!

  7. #7
    Registered User
    Join Date
    Apr 2003
    Posts
    50

    One more thing!!!

    I noticed that some programs contain a \n in cout functions.

    like this:

    cout<<"\nHello";

    Why is this? I couldn't find the answer in a tutorial, but I know it is used in other functions.

  8. #8
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    '\n' is the new-line character. When used with cout, it is roughly equivalent to endl.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  9. #9
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Originally posted by FromHolland

    These are both the same, only once thing is different!
    The upper code uses VOID MAIN and the bottom code
    use INT MAIN. They both work, no difference at all! Does
    it matter what I use??
    That depends on what you mean by "work". On your particular compiler, it may give the results you expect. But just because *YOUR* compiler allows incorrect code to compile doesn't mean that other compilers will.

    void main IS an error. It's not legal C++. It's unfortunate, but many compilers don't report it as an error -- this is a flaw in the compilers, though. It "works" on many compilers as an undesired byproduct of how those compilers implement name mangling. It's not guaranteed to work.

    Heck, I've seen compilers that would compile:

    Code:
    class Node{
    /*...*/
    };
    
    Node main(Node in){
    }
    but it does NOT mean it will perform as expected, and it does NOT mean the program is legal C++
    Last edited by Cat; 06-12-2003 at 03:11 PM.

  10. #10
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953
    Is this question going to stop someday?!
    Almost every month we have a new post about "int main() or void main()", "int main() VS. void main()"....

    I think mods should reply to this kind of post with "READ THE FAQ'S", and then close the post!
    What do you think...
    none...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  4. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  5. Quack! It doesn't work! >.<
    By *Michelle* in forum C++ Programming
    Replies: 8
    Last Post: 03-02-2003, 12:26 AM