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

    http://www23.brinkster.com/noicys
     
    Ioannis Vranos, Dec 31, 2004
    #1
    1. Advertising

  2. Ioannis Vranos

    Jon Wilson Guest

    Ioannis Vranos wrote:
    > #include <iostream>
    >
    >
    > int main()
    > {
    > using namespace std;
    >
    > cout<<"Best wishes for a Great Happy New Year to all!"<<endl;
    > }
    >
    >
    >



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

  3. Jon Wilson wrote:
    > Ioannis Vranos wrote:
    >
    >>#include <iostream>
    >>
    >>
    >>int main()
    >>{
    >> using namespace std;
    >>
    >> cout<<"Best wishes for a Great Happy New Year to all!"<<endl;
    >>}
    >>

    > This should give a compiler warning. You declare int main(), but have
    > no return statement.


    Not on a conforming compiler.


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

    Mike Wahler Guest

    "Jon Wilson" <> wrote in message
    news:YJeBd.667039$D%.153030@attbi_s51...
    > Ioannis Vranos wrote:
    > > #include <iostream>
    > >
    > >
    > > int main()
    > > {
    > > using namespace std;
    > >
    > > cout<<"Best wishes for a Great Happy New Year to all!"<<endl;
    > > }
    > >
    > >
    > >

    >
    >
    > This should give a compiler warning. You declare int main(), but have
    > no return statement.


    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?

    Jonathan Mcdougall wrote:

    > Jon Wilson wrote:
    >> Ioannis Vranos wrote:
    >>
    >>>#include <iostream>
    >>>
    >>>
    >>>int main()
    >>>{
    >>> using namespace std;
    >>>
    >>> cout<<"Best wishes for a Great Happy New Year to all!"<<endl;
    >>>}
    >>>

    >> This should give a compiler warning. You declare int main(), but have
    >> no return statement.

    >
    > Not on a conforming compiler.
    >
    >
    > Jonathan
     
    Florian Quetting, Dec 31, 2004
    #5
  6. Ioannis Vranos

    KPB Guest

    Florian Quetting wrote:
    > gcc without options wouldn't give a warning, but what about -Wall?


    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. "Jon Wilson" <> wrote...
    > Ioannis Vranos wrote:
    >> #include <iostream>
    >>
    >>
    >> int main()
    >> {
    >> using namespace std;
    >>
    >> cout<<"Best wishes for a Great Happy New Year to all!"<<endl;
    >> }
    >>
    >>
    >>

    >
    >
    > This should give a compiler warning. You declare int main(), but have no
    > return statement.


    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

    On Fri, 31 Dec 2004 16:02:00 GMT in comp.lang.c++, Jon Wilson
    <> wrote,
    >Ioannis Vranos wrote:
    >> #include <iostream>
    >>
    >>
    >> int main()
    >> {
    >> using namespace std;
    >>
    >> cout<<"Best wishes for a Great Happy New Year to all!"<<endl;
    >> }
    >>

    >
    >This should give a compiler warning. You declare int main(), but have
    >no return statement.


    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

    "KPB" <> wrote in message news:3xgBd.340$...
    > Florian Quetting wrote:
    > > gcc without options wouldn't give a warning, but what about -Wall?

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


    Correct (this only applies to 'main()'

    >
    > 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.


    Actually, I and others feel that this constitutes 'good
    practice' anyway. But it is incorrect to state that
    omitting it is invalid.

    > I think I read this fact in one of the Josuttis books.


    It's also cited in many others.

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


    That's the wrong way to determine how the language should
    work.

    >
    > 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.


    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!


    "Victor Bazarov" <> wrote in message
    news:2JgBd.667542$D%.148039@attbi_s51...
    > "Jon Wilson" <> wrote...
    >> Ioannis Vranos wrote:
    >>> #include <iostream>
    >>>
    >>>
    >>> int main()
    >>> {
    >>> using namespace std;
    >>>
    >>> cout<<"Best wishes for a Great Happy New Year to all!"<<endl;
    >>> }
    >>>
    >>>
    >>>

    >>
    >>
    >> This should give a compiler warning. You declare int main(), but have no
    >> return statement.

    >
    > 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
    >
     
    adbarnet, Dec 31, 2004
    #10
  11. Victor Bazarov wrote:

    > 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.



    I just checked the 2003 standard about endl. In all examples where it is
    used, no <ostream> is included.




    --
    Ioannis Vranos

    http://www23.brinkster.com/noicys
     
    Ioannis Vranos, Dec 31, 2004
    #11
  12. David Harmon wrote:

    > 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.



    It is redundant, however I do not think it is wasteful. :)




    --
    Ioannis Vranos

    http://www23.brinkster.com/noicys
     
    Ioannis Vranos, Dec 31, 2004
    #12
  13. Ioannis Vranos

    KPB Guest

    Mike Wahler wrote:
    > "KPB" <> wrote in message news:3xgBd.340$...
    >
    >>Florian Quetting wrote:
    >>
    >>>gcc without options wouldn't give a warning, but what about -Wall?

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

    >
    >
    > Correct (this only applies to 'main()'


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


    >
    >
    >>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.

    >
    >
    > Actually, I and others feel that this constitutes 'good
    > practice' anyway. But it is incorrect to state that
    > omitting it is invalid.


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


    >
    >
    >>I think I read this fact in one of the Josuttis books.

    >
    >
    > It's also cited in many others.


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


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

    >
    >
    > That's the wrong way to determine how the language should
    > work.


    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.


    >
    >
    >>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.

    >
    >
    > 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.


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

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

    Mike Wahler Guest

    "KPB" <> wrote in message news:SUiBd.364$...
    > Mike Wahler wrote:
    > > "KPB" <> wrote in message news:3xgBd.340$...
    > >
    > >>Florian Quetting wrote:
    > >>
    > >>>gcc without options wouldn't give a warning, but what about -Wall?
    > >>
    > >>No. As others have said, the return statement is not needed.

    > >
    > >
    > > Correct (this only applies to 'main()'

    >
    > Yes. That's what we're talking about here, right?
    >
    >
    > >
    > >
    > >>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.

    > >
    > >
    > > Actually, I and others feel that this constitutes 'good
    > > practice' anyway. But it is incorrect to state that
    > > omitting it is invalid.

    >
    > Where did I state that it was incorrect to omit this?
    >
    >
    > >
    > >
    > >>I think I read this fact in one of the Josuttis books.

    > >
    > >
    > > It's also cited in many others.

    >
    > Ok. I'm just telling you where I found it.
    >
    >
    > >
    > >
    > >>Also, I did try out the -Wall just to make sure.

    > >
    > >
    > > That's the wrong way to determine how the language should
    > > work.

    >
    > Yes but it answered a question from a previous poster.


    Which question?

    > You took this out
    > of context as you seem to have done to the rest of my post.


    I don't think so. Whatever.

    >
    >
    > >
    > >
    > >>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.

    > >
    > >
    > > 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.

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


    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. Jon Wilson wrote:

    >> #include <iostream>
    >>
    >>
    >> int main()
    >> {
    >> using namespace std;
    >>
    >> cout<<"Best wishes for a Great Happy New Year to all!"<<endl;
    >> }
    >>
    >>
    >>

    >
    >
    > This should give a compiler warning. You declare int main(), but have
    > no return statement.



    As others said, this is valid code, and thus no warning should be provided.




    --
    Ioannis Vranos

    http://www23.brinkster.com/noicys
     
    Ioannis Vranos, Dec 31, 2004
    #15
  16. Ioannis Vranos

    Mike Wahler Guest

    "adbarnet" <> wrote in message
    news:41d5b6c3$1_2@127.0.0.1...
    > ..and - without some kind of <stdheader> include the compiler doesn't know
    > anything about the namespace 'std' in any case


    But the code did #include a standard header -- <iostream>,
    so namespace 'std' is indeed accessible.

    > - so the using statement
    > should blow!


    It's fine.

    BTW please do not top-post.

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

    KPB Guest

    Mike Wahler wrote:
    > "KPB" <> wrote in message news:SUiBd.364$...
    >
    >>Mike Wahler wrote:
    >>
    >>>"KPB" <> wrote in message news:3xgBd.340$...
    >>>
    >>>
    >>>>Florian Quetting wrote:
    >>>>
    >>>>
    >>>>>gcc without options wouldn't give a warning, but what about -Wall?
    >>>>
    >>>>No. As others have said, the return statement is not needed.
    >>>
    >>>
    >>>Correct (this only applies to 'main()'

    >>
    >>Yes. That's what we're talking about here, right?
    >>
    >>
    >>
    >>>
    >>>>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.
    >>>
    >>>
    >>>Actually, I and others feel that this constitutes 'good
    >>>practice' anyway. But it is incorrect to state that
    >>>omitting it is invalid.

    >>
    >>Where did I state that it was incorrect to omit this?
    >>
    >>
    >>
    >>>
    >>>>I think I read this fact in one of the Josuttis books.
    >>>
    >>>
    >>>It's also cited in many others.

    >>
    >>Ok. I'm just telling you where I found it.
    >>
    >>
    >>
    >>>
    >>>>Also, I did try out the -Wall just to make sure.
    >>>
    >>>
    >>>That's the wrong way to determine how the language should
    >>>work.

    >>
    >>Yes but it answered a question from a previous poster.

    >
    >
    > Which question?
    >


    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.

    >
    >>You took this out
    >>of context as you seem to have done to the rest of my post.

    >
    >
    > I don't think so. Whatever.
    >


    Still don't think so? Ok, whatever.

    >
    >>
    >>>
    >>>>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.
    >>>
    >>>
    >>>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.

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

    >
    >
    > I also usually initialize everything. But imo it is good to
    > know the rules, especially when reading others' code.
    >
    > -Mike


    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. Ioannis Vranos wrote:

    > #include <iostream>
    >
    > int main()
    > {
    > using namespace std;
    >
    > cout<<"Best wishes for a Great Happy New Year to all!"<<endl;
    > }


    > cat main.cc

    #include <iostream>

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

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

    return 0;
    }

    > g++ -Wall -ansi -pedantic -o main main.cc
    > ./main

    And Happy New Year to you too Ioannis.
     
    E. Robert Tisdale, Dec 31, 2004
    #18
  19. KPB wrote:

    > 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.


    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

    http://www23.brinkster.com/noicys
     
    Ioannis Vranos, Dec 31, 2004
    #19
  20. Ioannis Vranos

    KPB Guest

    Ioannis Vranos wrote:
    > KPB wrote:
    >
    >> 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.

    >
    >
    > 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;
    > }
    >



    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. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ONil@

    OT: Happy New Year Everybody..

    ONil@, Dec 30, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    341
    ONil@
    Dec 30, 2003
  2. striker

    [OT][GEEK] Happy new year.

    striker, Dec 28, 2003, in forum: C Programming
    Replies:
    1
    Views:
    345
    Arthur J. O'Dwyer
    Dec 28, 2003
  3. Christos TZOTZIOY Georgiou

    ...And a happy new year btw (NT :)

    Christos TZOTZIOY Georgiou, Dec 31, 2003, in forum: Python
    Replies:
    0
    Views:
    293
    Christos TZOTZIOY Georgiou
    Dec 31, 2003
  4. Rob Hughes
    Replies:
    0
    Views:
    299
    Rob Hughes
    Jan 1, 2004
  5. Thomas Ploch

    Merry Christmas and a happy new year!

    Thomas Ploch, Dec 24, 2006, in forum: Python
    Replies:
    0
    Views:
    345
    Thomas Ploch
    Dec 24, 2006
Loading...

Share This Page