Thread: The 'main()' question!

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    8

    The 'main()' question!

    I'm intensively trying to learn how to program C++. I am using Visual C++ 2005 Express.

    I have had numerous books that have told me to write this:


    Code:
    #include
    
    using namespace std;
    
    main()
    {
    
    cout << "Hello World!" << endl;
    
    }


    Okay, I understand it all, but I don't understand the main() part. Everytime I try to compile it, I get an error. The only way I can get it to compile is if I type int main() or void main(void).

    If anyone can explain why this is, it would be more than helpful. Thank-You!

  2. #2
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    You need to show the return type for main which is int main. Look a Salem's avatar for guidance.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I have had numerous books that have told me to write this
    They were wrong, or too old to be useful. The language standard defines main to return an integer. As such, void main() is illegal and you shouldn't use it. Likewise, the standard has also removed a feature inherited from C called implicit int. That's where if you omit the type in a declaration where implicit int applies, the compiler will assume you meant int. That's why things like this used to work:
    Code:
    main()
    {
    }
    However, even when it wasn't illegal, it was a poor practice. The correct definition of main is:
    Code:
    int main()
    {
    }
    or:
    Code:
    int main ( int argc, char *argv[] )
    {
    }
    You can also use anything equivalent, so explicitly saying void for the argument list is legal as well for main taking no arguments:
    Code:
    int main ( void )
    {
    }
    Also note that main is special in how it handles return values. Something like implicit int, if you omit a return statement, 0 for success is returned automagically. Some people prefer to be explicit, which is also fine and the following is equivalent to the above:
    Code:
    int main()
    {
      return 0;
    }
    >Everytime I try to compile it, I get an error.
    Good, your compiler is correct.
    My best code is written with the delete key.

  4. #4
    Registered User
    Join Date
    Dec 2006
    Posts
    8
    Couldn't be explained any better. I feel a bunch of warm fuzzies because I now understand. Thank-You Very Much!

  5. #5
    Registered User
    Join Date
    Dec 2006
    Posts
    8
    ...yet, I am a little bit irritated that my books lied to me. Nonetheless, I am still very giddy.

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >...yet, I am a little bit irritated that my books lied to me.
    They might not have lied. It could be that you have outdated books. Anything published prior to 2000 is going to be hit or miss for standards compliance.
    My best code is written with the delete key.

  7. #7
    Registered User
    Join Date
    Dec 2006
    Posts
    11
    so what does this mean?

    Code:
    int main ( int argc, char *argv[] )
    {
    }

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    The first int parameter is the number of arguments from the command line. The second parameter is an array of C style strings containing the arguments. If your program uses command line arguments, then you use that version.

  9. #9
    In the Land of Diddly-Doo g4j31a5's Avatar
    Join Date
    Jul 2006
    Posts
    476
    Quote Originally Posted by lasher
    so what does this mean?

    Code:
    int main ( int argc, char *argv[] )
    {
    }
    For example if you make an application called "app" and you called it with:

    app -A -B -C

    the argc will become 3 and argv[0] = "-A", argv[1] = "-B", argv[2] = "-C".

  10. #10
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    the argc will become 3 and argv[0] = "-A", argv[1] = "-B", argv[2] = "-C".
    Not exactly
    argv[0] = "app"
    so argc will be 4 and argv[1] = "-A", argv[2] = "-B", argv[3] = "-C".
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  11. #11
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by vart
    Not exactly
    argv[0] = "app"
    so argc will be 4 and argv[1] = "-A", argv[2] = "-B", argv[3] = "-C".
    Not exactly, again.

    The contents of the elements of argv[0] are implementation defined. In practice, in this example, the content will often be as you describe, but could be different. For example, argv[0] will be "app" with a lot of compilers/systems, but it could be "/some/path/app" (where /some/path identifies the directory the app executable is in), or it could simply be an empty string "".

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. another do while question
    By kbpsu in forum C++ Programming
    Replies: 3
    Last Post: 03-23-2009, 12:14 PM
  2. main and return question
    By BuzzBuzz in forum C Programming
    Replies: 9
    Last Post: 02-25-2009, 03:38 AM
  3. void main question..
    By transgalactic2 in forum C Programming
    Replies: 22
    Last Post: 11-19-2008, 11:59 AM
  4. C99 and int main()
    By cwr in forum C Programming
    Replies: 8
    Last Post: 09-19-2005, 06:54 AM
  5. int main (), main (), main (void), int main (void) HELP!!!
    By SmokingMonkey in forum C++ Programming
    Replies: 7
    Last Post: 05-31-2003, 09:46 PM