Happy New Year!

Discussion in 'C++' started by Ioannis Vranos, Dec 31, 2004.

  1. #include <iostream>


    int main()
    {
    using namespace std;

    cout<<"Best wishes for a Great Happy New Year to all!"<<endl;
    }
     
    Ioannis Vranos, Dec 31, 2004
    #1
    1. Advertisements

  2. Ioannis Vranos

    Jon Wilson Guest


    This should give a compiler warning. You declare int main(), but have
    no return statement.
     
    Jon Wilson, Dec 31, 2004
    #2
    1. Advertisements

  3. Not on a conforming compiler.


    Jonathan
     
    Jonathan Mcdougall, Dec 31, 2004
    #3
  4. Ioannis Vranos

    Mike Wahler Guest

    No return statement is required. When it is not specified,
    the program performs an implicit 'return 0'. This is the case
    only for the 'main()' function.

    -Mike
     
    Mike Wahler, Dec 31, 2004
    #4
  5. gcc without options wouldn't give a warning, but what about -Wall?
     
    Florian Quetting, Dec 31, 2004
    #5
  6. Ioannis Vranos

    KPB Guest

    No. As others have said, the return statement is not needed.

    This was one of those weird facts that I didn't know about until
    recently. I just include the return statement without thinking about it.
    I think I read this fact in one of the Josuttis books.

    Also, I did try out the -Wall just to make sure.

    To me, this is similar to the variable initialization issue. Some are
    init'd implicitly (if you don't initialize them explicitly) and some
    aren't. I just explicitly initialize everything then I don't have to
    remember which are and which aren't.

    KPB
     
    KPB, Dec 31, 2004
    #6
  7. Actually that's not a problem (see other replies). However, a strictly
    conforming implementation has to complain about 'endl' being undefined.
    To find it, include <ostream> as well.

    Happy New Year!

    Victor
     
    Victor Bazarov, Dec 31, 2004
    #7
  8. Ioannis Vranos

    David Harmon Guest

    This should give a compiler warning. You declare int main(), but have
    no return statement.[/QUOTE]

    The compiler may give any warning it wants, but it shouldn't for
    that. main() has a special exemption and is allowed to have no
    return statement.

    The output line should be
    cout<<"Best wishes for a Great Happy New Year to all!\n";

    It is useless and wasteful to use endl to flush the stream,
    immediately before the program is going to end and flush it anyway.
     
    David Harmon, Dec 31, 2004
    #8
  9. Ioannis Vranos

    Mike Wahler Guest

    Correct (this only applies to 'main()'
    Actually, I and others feel that this constitutes 'good
    practice' anyway. But it is incorrect to state that
    omitting it is invalid.
    It's also cited in many others.
    That's the wrong way to determine how the language should
    work.
    Aa matter of 'habit', I will usually initialize every
    object I define, regardless of its storage duration.
    But the issue of which objects are automatically
    initialized and which are not is straightforward:

    1) Static storage duration objects are automatically default initialized.
    2) Objects with any other storage duration are not automatically
    initialized.
    3) ("Special case") If one or more of an array's elements are explicitly
    initialized, any elements for which there are no corresponding
    initializers
    are default inititalized.


    -Mike
     
    Mike Wahler, Dec 31, 2004
    #9
  10. Ioannis Vranos

    adbarnet Guest

    ...and - without some kind of <stdheader> include the compiler doesn't know
    anything about the namespace 'std' in any case - so the using statement
    should blow!

    Indeed - Happy New Year!
     
    adbarnet, Dec 31, 2004
    #10

  11. I just checked the 2003 standard about endl. In all examples where it is
    used, no <ostream> is included.
     
    Ioannis Vranos, Dec 31, 2004
    #11

  12. It is redundant, however I do not think it is wasteful. :)
     
    Ioannis Vranos, Dec 31, 2004
    #12
  13. Ioannis Vranos

    KPB Guest

    Yes. That's what we're talking about here, right?

    Where did I state that it was incorrect to omit this?

    Ok. I'm just telling you where I found it.

    Yes but it answered a question from a previous poster. You took this out
    of context as you seem to have done to the rest of my post.

    Ok. You remember your rules and I'll just keep initializing everything.

    KPB
     
    KPB, Dec 31, 2004
    #13
  14. Ioannis Vranos

    Mike Wahler Guest

    Which question?
    I don't think so. Whatever.
    I also usually initialize everything. But imo it is good to
    know the rules, especially when reading others' code.

    -Mike
     
    Mike Wahler, Dec 31, 2004
    #14

  15. As others said, this is valid code, and thus no warning should be provided.
     
    Ioannis Vranos, Dec 31, 2004
    #15
  16. Ioannis Vranos

    Mike Wahler Guest

    It's fine.

    BTW please do not top-post.

    -Mike
     
    Mike Wahler, Dec 31, 2004
    #16
  17. Ioannis Vranos

    KPB Guest

    Someone wondered if using the -Wall switch in gcc would generate a
    warning. I told him no after trying it out. I knew it would'nt but there
    was no harm in making sure.
    Still don't think so? Ok, whatever.
    I read other people's code all the time.

    I have Stroustrup's book if I need to deal with anything like this. This
    is just one thing that isn't important for me to remember because:

    1. I don't see it enough in code to have to
    2. Like I said.. I have reference materials if I do.

    KPB
     
    KPB, Dec 31, 2004
    #17
  18. #include <iostream>

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

    std::cout << "And Happy New Year to you too Ioannis."
    << std::endl;

    return 0;
    }
    And Happy New Year to you too Ioannis.
     
    E. Robert Tisdale, Dec 31, 2004
    #18
  19. As I had read in some newsgroup, this implicit return 0; statement has
    its roots in K&R 2.

    In K&R2 the programs were in the style:


    main()
    {
    /* ... */
    }


    which was not a valid C90 code anyway, but Kernighan used this style.

    The C90 valid code was


    // But not valid C++ and C99.
    main()
    {
    /* ... */

    return 0;
    }


    or


    int main()
    {
    /* ... */

    return 0;
    }


    Someday (probably in the C99 committee) one suggested, wouldn't it be
    nice K&R 2 programs to become valid C99 programs?

    Everyone agreed, making the return 0; implicit if no return statement is
    provided.


    However later, they decided that return types should be explicitly
    provided (while in C90 const x; was equivalent to const int x; for
    example), and thus we are today with int main() and no return statement
    in both C99 and C++98.
     
    Ioannis Vranos, Dec 31, 2004
    #19
  20. Ioannis Vranos

    KPB Guest


    Yes, I've seen these in some old C code and even some old C++ code
    (old.. bad... code.. that I've corrected).

    I've also seen the void main(). Have you?

    Thanks,
    KPB
     
    KPB, Dec 31, 2004
    #20
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.