hello world program

Discussion in 'C++' started by Rahul Gandhi, Aug 29, 2003.

  1. Rahul Gandhi

    Rahul Gandhi Guest

    Hi guys,
    I'm trying to run small program helloworld.cpp.
    But i'm not getting any output on the stdout

    program is
    #include<iostream.h>
    main(){
    cout<<"hello world";
    }
    #include<iostream>
    using namespace std;
    main(){
    cout<<"hello world";
    }

    I have compiled the program with g++
    g++ helloworld.cpp -o helloworld
    ../helloworld

    It does prints hello world on stdout

    compiler I'm using is
    gcc version 3.2 20020903 (Red Hat Linux 8.0 3.2-7)
     
    Rahul Gandhi, Aug 29, 2003
    #1
    1. Advertisements

  2. I am using
    gcc version 3.2.2 20030222 (Red Hat Linux 3.2.2-5)
    and both versions compile and print to stdout, although the first gives a
    warning ,iostream.h seems to be "antiquated".
    Maybe You just overlooked it, because its in front of the next prompt.
    Insert \n after world.
     
    Volker Mosthaf, Aug 29, 2003
    #2
    1. Advertisements

  3. Rahul Gandhi

    Greg P. Guest

    | I don't know if this is the problem or not, but try flushing the output
    | stream 'cout' before the program ends:

    Yes, this is most likely the problem. You should also make main() int main()
    and return 0 at the end (before closing brace):

    #include <iostream>

    int main()
    {
    std::cout << "Hello Rahul Gandhi!" << std::flush;
    return(0);
    }
     
    Greg P., Aug 29, 2003
    #3
  4. Rahul Gandhi

    Attila Feher Guest

    This may still not work, there is no newline. You may flush it but
    displaying is only guaranteed for lines. So the game is:

    std::cout << "Hello Rahul Gandhi!" << std::endl;

    std::endl sends a newline to the buffer and then make sure it is written
    (does an std::flush).
    No need for that. And even if you write it there is absolutely no need for
    the parentheses around the 0.
     
    Attila Feher, Aug 29, 2003
    #4
  5. Rahul Gandhi

    chris Guest

    I thought the C++ standard said we don't have to bother returning 0 at
    the end, as it is done automatically?

    It is (arguably) better to write return 0;, to make it clear that return
    isn't a function but something special.
     
    chris, Aug 29, 2003
    #5
  6. Rahul Gandhi

    Greg P. Guest

    | > return(0);
    |
    | No need for that. And even if you write it there is absolutely no need
    for
    | the parentheses around the 0.

    If the OP is new to C++ (as I assume from his post), then it is good
    practice to start "return"ing. Yes it's true that it is not needed (as it
    automatically does this at the end of main's scope). But returning a status
    now will allow the OP to keep it in mind when it comes time to return during
    an error condition.

    Oh, and all my colleagues hassle me about my parenthesis. I was taught this
    early on by some old farts and it has since stuck to me. There is no harm,
    and it's purely a stylistic view. The same argument could be said about K&R
    coding style (which I have barely just got un-accustomed to). Please don't
    badger me on my style =)
     
    Greg P., Aug 29, 2003
    #6
  7. Rahul Gandhi

    Attila Feher Guest

    All my point was (no badgering! :) ) is that if you show this to a newbie
    he might think that the parenthesis are required and that return in main,
    too. My point is rather that IMHO it is preferable to give that little
    speach about "it is not mandatory to return but it is a good style to do so,
    because...". Well, I dunno any good excuses for the parentheses. ;-)
     
    Attila Feher, Aug 29, 2003
    #7
  8. Rahul Gandhi

    Attila Feher Guest

    If we really want to confuse them we can write return EXIT_SUCCESS. ;-)
     
    Attila Feher, Aug 29, 2003
    #8
  9. Rahul Gandhi

    Agent Mulder Guest

    <chris>
    I thought the C++ standard said we don't have to bother returning 0 at
    the end, as it is done automatically?
    </chris>

    What confuses me is this:

    int f(){}
    int main(int,char**){int a=f();}

    This compiles (with a warning).
    It looks like an error to me. And
    what is the value of 'a' after this?

    -X
     
    Agent Mulder, Aug 29, 2003
    #9
  10. main() is a special case and returns 0 if you don't explicitly do a
    return yourself (one popular compiler doesn't accept this however). As
    far as the f() function is concerned, I don't think that it should
    compile and you might be looking at a compiler bug (I didn't look it up
    in the standard so I might be wrong).
     
    Peter van Merkerk, Aug 29, 2003
    #10
  11. Rahul Gandhi

    Attila Feher Guest

    Peter van Merkerk wrote:
    [SNIP-SNAP]
    Unfortunately those bugs exist, especially if there is a complex branching
    inside with return statement. Some compilers just miss those branches where
    there is no return. Some others go to the other extreme:

    bool func( some args) {
    if (using args) {
    DoSomething();
    return true;
    } else {
    DoSomethingElse();
    return false;
    }
    }

    and you get an error that there is no return... Of course it is simple in
    this case: just remove the else and there ya go. ;-)
     
    Attila Feher, Aug 29, 2003
    #11
  12. Rahul Gandhi

    chris Guest

    Don't want to encourage too many parentheses, else people might mistake
    your code for lisp :)
     
    chris, Aug 29, 2003
    #12
  13. Rahul Gandhi

    White Wolf Guest

    They reminded me of lips. Time to get a girlfriend again. :)
     
    White Wolf, Aug 29, 2003
    #13
  14. Rahul Gandhi

    Bob Jacobs Guest

    From the standard, 6.3.3:
    "Flowing off the end of a function is equivalent to a return
    with no value; this results in undefined behavior in a value
    returning function."

    [main(), as we know, is an exception]
     
    Bob Jacobs, Aug 29, 2003
    #14
  15. Some shells will completely overwrite the last "line" of output if it's
    not properly terminated, I think. I also believe that ending output with
    a newline is required by the standard.

    -Kevin
     
    Kevin Goodsell, Aug 29, 2003
    #15
  16. Rahul Gandhi

    Greg P. Guest

    <snip>
    lol! You are all right. I have never looked at my returns in that light. I
    guess it's time for me to "re-adjust" my coding style if I plan to be
    helping the rookies out there =)
     
    Greg P., Aug 29, 2003
    #16
    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.