hello world program

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Rahul Gandhi

    hello world program

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

    program is
    #include<iostre am.h>
    main(){
    cout<<"hello world";
    }
    [color=blue][color=green][color=darkred]
    >>>>>>>>>>>>>>> >>>>>>>>>>>[/color][/color][/color]
    #include<iostre am>
    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)
  • Volker Mosthaf

    #2
    Re: hello world program

    On Thu, 28 Aug 2003 23:04:03 -0700, Rahul Gandhi wrote:
    [color=blue]
    > Hi guys,
    > I'm trying to run small program helloworld.cpp.
    > But i'm not getting any output on the stdout
    >
    > program is
    > #include<iostre am.h>
    > main(){
    > cout<<"hello world";
    > }
    >[color=green][color=darkred]
    >>>>>>>>>>>>>>> >>>>>>>>>>>>[/color][/color]
    > #include<iostre am>
    > 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)[/color]

    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.

    --
    mfg
    Volker Mosthaf

    Comment

    • Greg P.

      #3
      Re: hello world program

      "Jim Fischer" <jfischer_link5 809@now.here.co m> wrote in message
      news:01C3b.3306 $xD2.1228@fe01. atl2.webusenet. com...
      | 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);
      }


      Comment

      • Attila Feher

        #4
        Re: hello world program

        Greg P. wrote:[color=blue]
        > "Jim Fischer" <jfischer_link5 809@now.here.co m> wrote in message
        > news:01C3b.3306 $xD2.1228@fe01. atl2.webusenet. com...[color=green]
        >> I don't know if this is the problem or not, but try flushing the
        >> output stream 'cout' before the program ends:[/color]
        >
        > 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;[/color]

        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).
        [color=blue]
        > return(0);[/color]

        No need for that. And even if you write it there is absolutely no need for
        the parentheses around the 0.
        [color=blue]
        > }[/color]

        --
        Attila aka WW


        Comment

        • chris

          #5
          Re: hello world program

          Greg P. wrote:[color=blue]
          > "Jim Fischer" <jfischer_link5 809@now.here.co m> wrote in message
          > news:01C3b.3306 $xD2.1228@fe01. atl2.webusenet. com...
          > | 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):[/color]

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

          [color=blue]
          >
          > #include <iostream>
          >
          > int main()
          > {
          > std::cout << "Hello Rahul Gandhi!" << std::flush;
          > return(0);
          > }[/color]

          It is (arguably) better to write return 0;, to make it clear that return
          isn't a function but something special.

          Comment

          • Greg P.

            #6
            Re: hello world program

            "Attila Feher" <attila.feher@l mf.ericsson.se> wrote in message
            news:bin8hv$200 $1@newstree.wis e.edt.ericsson. se...
            | > 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 =)


            Comment

            • Attila Feher

              #7
              Re: hello world program

              Greg P. wrote:[color=blue]
              > "chris" <chris@bubblesc ope.net> wrote in message
              > news:binal4$jvd $1@pump1.york.a c.uk...[color=green]
              >> I thought the C++ standard said we don't have to bother returning 0
              >> at the end, as it is done automatically?[/color]
              >
              > Correct.
              >[color=green]
              >> It is (arguably) better to write return 0;, to make it clear that
              >> return isn't a function but something special.[/color]
              >
              > Well certain people look at it differently as they do art pieces. I
              > agree that if someone is new and returns with parenthesis it may
              > confuse them. Please read the reply I gave to Atilla above for my
              > comments on this.[/color]

              If we really want to confuse them we can write return EXIT_SUCCESS. ;-)

              --
              Attila aka WW


              Comment

              • Attila Feher

                #8
                Re: hello world program

                Greg P. wrote:[color=blue]
                > "Attila Feher" <attila.feher@l mf.ericsson.se> wrote in message
                > news:bin8hv$200 $1@newstree.wis e.edt.ericsson. se...[color=green][color=darkred]
                >>> return(0);[/color]
                >>
                >> No need for that. And even if you write it there is absolutely no
                >> need for the parentheses around the 0.[/color]
                >
                > 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 =)[/color]

                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 aka WW


                Comment

                • Agent Mulder

                  #9
                  Re: hello world program

                  <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



                  Comment

                  • Peter van Merkerk

                    #10
                    Re: hello world program

                    > <chris>[color=blue]
                    > 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?[/color]

                    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
                    peter.van.merke rk(at)dse.nl


                    Comment

                    • Attila Feher

                      #11
                      Re: hello world program

                      Peter van Merkerk wrote:
                      [SNIP-SNAP][color=blue]
                      > 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).[/color]

                      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 aka WW


                      Comment

                      • chris

                        #12
                        Re: hello world program

                        Attila Feher wrote:[color=blue]
                        > Greg P. wrote:
                        >[color=green]
                        >>"Attila Feher" <attila.feher@l mf.ericsson.se> wrote in message
                        >>news:bin8hv$2 00$1@newstree.w ise.edt.ericsso n.se...
                        >>[color=darkred]
                        >>>> return(0);
                        >>>
                        >>>No need for that. And even if you write it there is absolutely no
                        >>>need for the parentheses around the 0.[/color][/color][/color]
                        .....[color=blue][color=green]
                        >> Well, I dunno any good excuses for the parentheses. ;-)[/color][/color]

                        Don't want to encourage too many parentheses, else people might mistake
                        your code for lisp :)

                        Comment

                        • White Wolf

                          #13
                          Re: hello world program

                          chris wrote:[color=blue]
                          > Attila Feher wrote:[color=green]
                          >> Greg P. wrote:
                          >>[color=darkred]
                          >>> "Attila Feher" <attila.feher@l mf.ericsson.se> wrote in message
                          >>> news:bin8hv$200 $1@newstree.wis e.edt.ericsson. se...
                          >>>
                          >>>>> return(0);
                          >>>>
                          >>>> No need for that. And even if you write it there is absolutely no
                          >>>> need for the parentheses around the 0.[/color][/color]
                          > ....[color=green][color=darkred]
                          > >> Well, I dunno any good excuses for the parentheses. ;-)[/color][/color]
                          >
                          > Don't want to encourage too many parentheses, else people might
                          > mistake your code for lisp :)[/color]

                          They reminded me of lips. Time to get a girlfriend again. :-)

                          --
                          WW aka Attila


                          Comment

                          • Bob Jacobs

                            #14
                            Re: hello world program

                            "Agent Mulder" <mbmulder_remov e_this_@home.nl > wrote in message
                            news:bing5g$kpq $1@news2.tilbu1 .nb.home.nl...[color=blue]
                            >
                            > 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?[/color]

                            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]



                            Comment

                            • Kevin Goodsell

                              #15
                              Re: hello world program

                              Volker Mosthaf wrote:
                              [color=blue]
                              > Maybe You just overlooked it, because its in front of the next prompt.
                              > Insert \n after world.[/color]

                              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
                              --
                              My email address is valid, but changes periodically.
                              To contact me please use the address from a recent posting.

                              Comment

                              Working...