hello world program

R

Rahul Gandhi

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)
 
V

Volker Mosthaf

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

Greg P.

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

Attila Feher

Greg said:
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.
 
C

chris

Greg said:
| 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.
 
G

Greg P.

| > 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 =)
 
A

Attila Feher

Greg said:
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. ;-)
 
A

Attila Feher

Greg said:
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. ;-)
 
A

Agent Mulder

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

Peter van Merkerk

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

Attila Feher

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. ;-)
 
C

chris

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

White Wolf

chris said:
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. :)
 
B

Bob Jacobs

Agent Mulder said:
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]
 
K

Kevin Goodsell

Volker said:
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
 
G

Greg P.

<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 =)
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top