468,514 Members | 1,072 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,514 developers. It's quick & easy.

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<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)
Jul 19 '05 #1
15 15002
On Thu, 28 Aug 2003 23:04:03 -0700, Rahul Gandhi wrote:
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)


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
Jul 19 '05 #2
"Jim Fischer" <jf***************@now.here.com> wrote in message
news:01*****************@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);
}
Jul 19 '05 #3
Greg P. wrote:
"Jim Fischer" <jf***************@now.here.com> wrote in message
news:01*****************@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;


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).
return(0);
No need for that. And even if you write it there is absolutely no need for
the parentheses around the 0.
}


--
Attila aka WW
Jul 19 '05 #4
Greg P. wrote:
"Jim Fischer" <jf***************@now.here.com> wrote in message
news:01*****************@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):
I thought the C++ standard said we don't have to bother returning 0 at
the end, as it is done automatically?


#include <iostream>

int main()
{
std::cout << "Hello Rahul Gandhi!" << std::flush;
return(0);
}


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

Jul 19 '05 #5
"Attila Feher" <at**********@lmf.ericsson.se> wrote in message
news:bi**********@newstree.wise.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 =)
Jul 19 '05 #6
Greg P. wrote:
"chris" <ch***@bubblescope.net> wrote in message
news:bi**********@pump1.york.ac.uk...
I thought the C++ standard said we don't have to bother returning 0
at the end, as it is done automatically?


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


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.


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

--
Attila aka WW
Jul 19 '05 #7
Greg P. wrote:
"Attila Feher" <at**********@lmf.ericsson.se> wrote in message
news:bi**********@newstree.wise.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 =)


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
Jul 19 '05 #8
<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

Jul 19 '05 #9
> <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?


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.merkerk(at)dse.nl
Jul 19 '05 #10
Peter van Merkerk wrote:
[SNIP-SNAP]
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).


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
Jul 19 '05 #11
Attila Feher wrote:
Greg P. wrote:
"Attila Feher" <at**********@lmf.ericsson.se> wrote in message
news:bi**********@newstree.wise.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.
..... Well, I dunno any good excuses for the parentheses. ;-)


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

Jul 19 '05 #12
chris wrote:
Attila Feher wrote:
Greg P. wrote:
"Attila Feher" <at**********@lmf.ericsson.se> wrote in message
news:bi**********@newstree.wise.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. .... >> Well, I dunno any good excuses for the parentheses. ;-)


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


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

--
WW aka Attila
Jul 19 '05 #13
"Agent Mulder" <mb*******************@home.nl> wrote in message
news:bi**********@news2.tilbu1.nb.home.nl...

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?


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]

Jul 19 '05 #14
Volker Mosthaf wrote:
Maybe You just overlooked it, because its in front of the next prompt.
Insert \n after world.


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.

Jul 19 '05 #15
"White Wolf" <wo***@freemail.hu> wrote in message
news:bi**********@phys-news1.kolumbus.fi...
<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 =)
Jul 19 '05 #16

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

33 posts views Thread by ankursinha | last post: by
42 posts views Thread by Prashanth Badabagni | last post: by
26 posts views Thread by Santanu Chatterjee | last post: by
1 post views Thread by Tarique Jawed | last post: by
2 posts views Thread by Sourcerer | last post: by
1 post views Thread by James T. Dennis | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.