Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Re: The worst 'hello world' example ever written...

Reply
Thread Tools

Re: The worst 'hello world' example ever written...

 
 
C
Guest
Posts: n/a
 
      09-14-2003
The_Sage <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>. ..
> >Reply to article by: http://www.velocityreviews.com/forums/(E-Mail Removed) (C)
> >Date written: 12 Sep 2003 04:59:48 -0700
> >MsgID:<(E-Mail Removed) e.com>


[Cross posted to comp.lang.c++ as an example of the worst
'hello world' example ever written in C++, please point out
any errors I have missed. (The original poster (The_Sage)
proposes this example is 100% correct, I submit it to the
experts for critique.)]

> >>you embarass yourself everytime you open your mouth.

>
> >> #include <iostream.h>
> >> void main() { cout << "Hello, World!" }

>
> >I have been letting this slip as I post enough errors
> >in my code; though I will do so no longer as this is the
> >third time you have repeated it without corrections. You
> >"embarass yourself everytime you" post this code. Here
> >are comments and corrections...

>
> >You wrote
> >#include <iostream.h> // should be no ".h"

>
> Wrong.


Not wrong, your specification is bad practice, and not the C++
idiom for the inclusion of C++ headers.

> >void // Standard says should be "int"

>
> No it doesn't, since cout doesn't return anything.


The return from 'cout' is irrelevant; this is C++ not
functional programming.

gemini(300)$ cat sag.cc
#include <iostream.h>
void main(){ cout << "Hello, World!" }
gemini(301)$ g++ -Wall sag.cc
sag.cc: At global scope:
sag.cc:2: `main' must return `int'
sag.cc:2: return type for `main' changed to `int'
sag.cc: In function `int main(...)':
sag.cc:2: parse error before `}' token
gemini(302)$ g++ --version
3.0

> >main() { // ok (can omit int n, char**v )

>
> Duh.
>
> >cout << "Hello, World!" // buffer is not flushed

>
> No need to.


Yes there is, some systems will not print anything otherwise.
Do you really want the portability which is the only real
advantage of C++ in this application?

> > // no ";"

>
> The "}" takes care of that last ";".


No it does not; this is C++ not Pascal. See the above gcc
error messages.

> > // no return

>
> That's what the following is...
>
> >}


Are you (The_Sage) completely unable to admit even the
simplest error? We all make mistakes, but when 99% of the
people on the group are confronted with one they would just
say 'whoopsy daisy, this is what I ment...', but not you.
You could have just replied 'oops, typo' and would most
probably have been duely forgiven, but instead you constantly
paint yourself into a corner as more and more evidence
proving you are wrong comes to light, loosing all credability
in the process.

I have posted this across to 'comp.lang.c++' as the people
there know C++ much better than I. I am sure they will be
able to inform you of exactly which parts of the standard
you have broken.

C
2003/9/14

PS: I am unsure whether the C++ standard specifies the auto
generation of the 'return 0;' sequence on the main() procedure
and would appreciate clarification on this matter.
 
Reply With Quote
 
 
 
 
Buster Copley
Guest
Posts: n/a
 
      09-14-2003
C wrote:
> The_Sage <(E-Mail Removed)> wrote


> [Cross posted to comp.lang.c++ as an example of the worst
> 'hello world' example ever written in C++, please point out
> any errors I have missed. (The original poster (The_Sage)
> proposes this example is 100% correct, I submit it to the
> experts for critique.)]


>>>> #include <iostream.h>
>>>> void main() { cout << "Hello, World!" }


>>>I have been letting this slip as I post enough errors
>>>in my code; though I will do so no longer as this is the
>>>third time you have repeated it without corrections. You
>>>"embarass yourself everytime you" post this code. Here
>>>are comments and corrections...


>>>You wrote
>>>#include <iostream.h> // should be no ".h"

>>
>>Wrong.

>
> Not wrong, your specification is bad practice, and not the C++
> idiom for the inclusion of C++ headers.


http://www.parashift.com/c++-faq-lit....html#faq-27.4

>>>void // Standard says should be "int"

>>
>>No it doesn't, since cout doesn't return anything.

>
> The return from 'cout' is irrelevant; this is C++ not
> functional programming.


http://www.parashift.com/c++-faq-lit....html#faq-29.3

cout is not a function; the name of the function which is called here is
'operator <<', and its return type is 'ostream &' (or more precisely,
'std::basic_ostream <char, std::char_traits <char> > &'). This is indeed
irrelevant to the return type of 'main', which is always int.

> gemini(300)$ cat sag.cc
> #include <iostream.h>
> void main(){ cout << "Hello, World!" }
> gemini(301)$ g++ -Wall sag.cc
> sag.cc: At global scope:
> sag.cc:2: `main' must return `int'
> sag.cc:2: return type for `main' changed to `int'
> sag.cc: In function `int main(...)':
> sag.cc:2: parse error before `}' token
> gemini(302)$ g++ --version
> 3.0
>
>>>main() { // ok (can omit int n, char**v )

>>
>>Duh.


Not OK. Main always returns int and there is no 'implicit int' in C or
in C++.

>>>cout << "Hello, World!" // buffer is not flushed

>>
>>No need to.

>
> Yes there is, some systems will not print anything otherwise.
> Do you really want the portability which is the only real
> advantage of C++ in this application?


The buffer will be flushed when the cout object is destroyed. A newline
might be nice though.

>>> // no ";"

>>
>>The "}" takes care of that last ";".

>
> No it does not; this is C++ not Pascal. See the above gcc
> error messages.


That's right, C++ is not Perl and you do need the semicolon.

>>> // no return

>>
>>That's what the following is...
>>
>>>}


Not really; that's just a closing brace. The return is implicit. main
implicitly returns 0 (EXIT_SUCCESS) when the closing brace is reached.

[snip]

> PS: I am unsure whether the C++ standard specifies the auto
> generation of the 'return 0;' sequence on the main() procedure
> and would appreciate clarification on this matter.


Auto generation is a funny way of looking at it, but yes, the effect is
as if there were a 'return 0;' before the closing brace.

Here's one way of doing it correctly:

#include <iostream>
int main () { std::cout << "Hello, world!\n"; }

Questions? Comments? Suggestions?
Regards,
Buster.

 
Reply With Quote
 
 
 
 
White Wolf
Guest
Posts: n/a
 
      09-14-2003
C wrote:
> The_Sage <(E-Mail Removed)> wrote in message
> news:<(E-Mail Removed)>. ..
>>> Reply to article by: (E-Mail Removed) (C)
>>> Date written: 12 Sep 2003 04:59:48 -0700
>>> MsgID:<(E-Mail Removed) .com>

>
> [Cross posted to comp.lang.c++ as an example of the worst
> 'hello world' example ever written in C++, please point out
> any errors I have missed. (The original poster (The_Sage)
> proposes this example is 100% correct, I submit it to the
> experts for critique.)]

[SNIP]

The guy is either a troll or a simpleton. Just ignore him.

--
WW aka Attila


 
Reply With Quote
 
Kevin Goodsell
Guest
Posts: n/a
 
      09-14-2003
C wrote:

>
>
> [Cross posted to comp.lang.c++ as an example of the worst
> 'hello world' example ever written in C++, please point out
> any errors I have missed. (The original poster (The_Sage)
> proposes this example is 100% correct, I submit it to the
> experts for critique.)]


We already had a thread making fun of this code earlier this week...

>
>
>>>>you embarass yourself everytime you open your mouth.

>>
>>
>>
>>>> #include <iostream.h>
>>>> void main() { cout << "Hello, World!" }


The main problems are:

* Non-standard (actually old, pre-standard) header. <iostream.h> no
longer exists in C++. The replacement is <iostream>

* Incorrect return type for main. The standard explicitly states that
main must return int. In fact, every document that has ever been
accepted as a standard for either C or C++ has required an 'int' return
type for main. Some allowed it to be implicit if the return type was
omitted. This is no longer allowed in either language.

* The output may never be seen. This is not a problem with flushing
(cout will be flushed when it is destroyed on program termination). It
is a problem with not properly terminating the line. A C++ program
should end it's output with a newline.

* Once the correct header is used, cout is placed in the std namespace,
therefore it's full name is std::cout. It can either be referred to
using this fully qualified name or the name can be brought into scope
using a 'using' statement.

* Missing semi-colon after the one and only statement in main().


(referring to <iostream.h>

>
> Not wrong, your specification is bad practice, and not the C++
> idiom for the inclusion of C++ headers.


<iostream.h> does not exist in standard C++. It's as simple as that.

>
>
>>>void // Standard says should be "int"

>>
>>No it doesn't, since cout doesn't return anything.


Clearly a fundamental misunderstanding of how the language works.

>>>cout << "Hello, World!" // buffer is not flushed

>>
>>No need to.

>
>
> Yes there is, some systems will not print anything otherwise.


The buffer is flushed, but the output still may not be seen without a
newline.

>>The "}" takes care of that last ";".


Another fundamental misunderstanding about the language. Semi-colons
terminate C++ statements, they don't separate them. A semicolon after
the 'cout' statement is necessary.

The corrected code looks like this:

#include <iostream> // no .h
int main() // returns int
{ std::cout << "Hello, World!\n"; } // std::, newline, semicolon.

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

 
Reply With Quote
 
Josh Sebastian
Guest
Posts: n/a
 
      09-14-2003
On Sun, 14 Sep 2003 20:01:54 +0000, Kevin Goodsell wrote:

> In fact, every document that has ever been
> accepted as a standard for either C or C++ has required an 'int' return
> type for main.


<OT>
This is not true. In C, an implementation is required to accept a program
which defines main with an int return type, but it is
implementation-defined whether (and which) other return types are
acceptable.
</OT>

> * The output may never be seen. This is not a problem with flushing
> (cout will be flushed when it is destroyed on program termination). It
> is a problem with not properly terminating the line. A C++ program
> should end it's output with a newline.


It's implementation-defined. From C99 draft n869, 7.19.2:

A text stream is an ordered sequence of characters composed into lines,
each line consisting of zero or more characters plus a terminating
new-line character. Whether the last line requires a terminating
new-line character is implementation-defined.

Josh
 
Reply With Quote
 
Kevin Goodsell
Guest
Posts: n/a
 
      09-14-2003
Josh Sebastian wrote:

>
> <OT>
> This is not true. In C, an implementation is required to accept a program
> which defines main with an int return type, but it is
> implementation-defined whether (and which) other return types are
> acceptable.
> </OT>


Yeah, that's true. Kind of a technicality though... if you expect your
code to work on a standard compiler, you still have to use 'int' as
main's return type.

>
>>* The output may never be seen. This is not a problem with flushing
>>(cout will be flushed when it is destroyed on program termination). It
>>is a problem with not properly terminating the line. A C++ program
>>should end it's output with a newline.

>
>
> It's implementation-defined. From C99 draft n869, 7.19.2:
>
> A text stream is an ordered sequence of characters composed into lines,
> each line consisting of zero or more characters plus a terminating
> new-line character. Whether the last line requires a terminating
> new-line character is implementation-defined.
>
> Josh


2 things: 1, I'm not sure why you are referencing C99 for a C++
discussion - it may or may not be applicable. 2, that wording might be
intended as a warning not to count on the last line of your *input*
being properly terminated. I wonder if there's a more explicit statement
about terminating the last line of the standard output - I think I've
heard that there is.

I'm sure it's either undefined or implementation defined, though. In any
case, it's best to terminate output with a newline.

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

 
Reply With Quote
 
Josh Sebastian
Guest
Posts: n/a
 
      09-14-2003
On Sun, 14 Sep 2003 21:06:58 +0000, Kevin Goodsell wrote:

> Josh Sebastian wrote:


>>>* The output may never be seen. This is not a problem with flushing
>>>(cout will be flushed when it is destroyed on program termination). It
>>>is a problem with not properly terminating the line. A C++ program
>>>should end it's output with a newline.

>>
>>
>> It's implementation-defined. From C99 draft n869, 7.19.2:
>>
>> A text stream is an ordered sequence of characters composed into lines,
>> each line consisting of zero or more characters plus a terminating
>> new-line character. Whether the last line requires a terminating
>> new-line character is implementation-defined.

>
> 2 things: 1, I'm not sure why you are referencing C99 for a C++
> discussion - it may or may not be applicable.


Section 7 of C90, with a few exceptions, is normative in C++98. I don't
have a copy of C90, but I assume (I know -- dangerous) that the wording is
similar.

> 2, that wording might be
> intended as a warning not to count on the last line of your *input*
> being properly terminated. I wonder if there's a more explicit statement
> about terminating the last line of the standard output - I think I've
> heard that there is.


I couldn't find one, but that doesn't mean it isn't there.

> I'm sure it's either undefined or implementation defined, though. In any
> case, it's best to terminate output with a newline.


True.

Josh
 
Reply With Quote
 
The_Sage
Guest
Posts: n/a
 
      09-15-2003
>Reply to article by: (E-Mail Removed) (C)
>Date written: 14 Sep 2003 05:42:24 -0700
>MsgID:<(E-Mail Removed) e.com>


>[Cross posted to comp.lang.c++ as an example of the worst
>'hello world' example ever written in C++, please point out
>any errors I have missed. (The original poster (The_Sage)
>proposes this example is 100% correct, I submit it to the
>experts for critique.)]


Yes, and they whipped your ass too.

The Sage

================================================== ===========
My Home Page : http://members.cox.net/the.sage

"The men that American people admire most extravagantly are
most daring liars; the men they detest the most violently are
those who try to tell them the truth" -- H. L. Mencken
================================================== ===========
 
Reply With Quote
 
The_Sage
Guest
Posts: n/a
 
      09-15-2003
>Reply to article by: Kevin Goodsell <(E-Mail Removed)>
>Date written: Sun, 14 Sep 2003 20:01:54 GMT
>MsgID:<SO39b.3450$(E-Mail Removed) thlink.net>


>>[Cross posted to comp.lang.c++ as an example of the worst
>>'hello world' example ever written in C++, please point out
>>any errors I have missed. (The original poster (The_Sage)
>>proposes this example is 100% correct, I submit it to the
>>experts for critique.)]


>We already had a thread making fun of this code earlier this week...


As we will see, the laugh is on you...

>>>>>you embarass yourself everytime you open your mouth.


>>>>> #include <iostream.h>
>>>>> void main() { cout << "Hello, World!" }


>The main problems are:


>* Non-standard (actually old, pre-standard) header. <iostream.h> no
>longer exists in C++. The replacement is <iostream>


It isn't non-standard, is just isn't specified in the standard that way. It is
on your hard drive as "iostream.h" and you can use it both ways, just one way is
standard and the other is not.

>* Incorrect return type for main. The standard explicitly states that
>main must return int. In fact, every document that has ever been
>accepted as a standard for either C or C++ has required an 'int' return
>type for main. Some allowed it to be implicit if the return type was
>omitted. This is no longer allowed in either language.


The C standard (ISO/IEC 9899:1999) does not require main() to return anything
although the C++ standard does. But ISO C++ Standard (ISO/IEC 14882:199
specifically requires main to return int although you *can* use void main() in
IBM, WATCOM, or MS C++ (as well as other) ISO compliant compilers.
http://homepages.tesco.net/~J.deBoyn...void-main.html

>* The output may never be seen. This is not a problem with flushing
>(cout will be flushed when it is destroyed on program termination). It
>is a problem with not properly terminating the line. A C++ program
>should end it's output with a newline.


Obviously you are an armchair programmer since you are merely guessing. All that
will happen is this...

C:\>Hello
Hello World
C:\

Instead of...

C:\>Hello
Hello World

C:\

No guessing needed! Funny how the real world works like that, eh?

>* Once the correct header is used, cout is placed in the std namespace,
>therefore it's full name is std::cout. It can either be referred to
>using this fully qualified name or the name can be brought into scope
>using a 'using' statement.


Or you can set it in some compilers to default to namespaces by default...which
most do.

>* Missing semi-colon after the one and only statement in main().


That's because it isn't required in all cases. The bracket takes care of that
one special case where it isn't required.

>(referring to <iostream.h>


>>Not wrong, your specification is bad practice, and not the C++
>>idiom for the inclusion of C++ headers.


><iostream.h> does not exist in standard C++. It's as simple as that.


Yes it does. Just do a file search for it on your computer (presuming you have a
ISO compliant C++ compiler installed on it).

>>>>void // Standard says should be "int"


>>>No it doesn't, since cout doesn't return anything.


>Clearly a fundamental misunderstanding of how the language works.


Oh yes, "clearly". Not even close. Haha! You are so full of ****.

The Sage

================================================== ===========
My Home Page : http://members.cox.net/the.sage

"The men that American people admire most extravagantly are
most daring liars; the men they detest the most violently are
those who try to tell them the truth" -- H. L. Mencken
================================================== ===========
 
Reply With Quote
 
Kevin Goodsell
Guest
Posts: n/a
 
      09-15-2003
The_Sage wrote:
>>Reply to article by: Kevin Goodsell <(E-Mail Removed)>

>
>>* Non-standard (actually old, pre-standard) header. <iostream.h> no
>>longer exists in C++. The replacement is <iostream>

>
>
> It isn't non-standard, is just isn't specified in the standard that way. It is
> on your hard drive as "iostream.h" and you can use it both ways, just one way is
> standard and the other is not.


My hard drive does not define what is and is not standard. Neither does
yours. <iostream.h> is not standard. Even when it is supplied with a
compiler it is probably not the same as <iostream>. Things have changed
since ARM C++.

> The C standard (ISO/IEC 9899:1999) does not require main() to return anything


A technicality. You still can't use 'void' if you expect your code to
compile on a standard compliant compiler.

> although the C++ standard does.


Yes, it does. And you used void anyway. Therefore you were wrong.

> But ISO C++ Standard (ISO/IEC 14882:199
> specifically requires main to return int although you *can* use void main() in
> IBM, WATCOM, or MS C++ (as well as other) ISO compliant compilers.


Are you sure? The lack of a compiler error does not make it correct. The
standard does not require a diagnostic for an incorrect return type for
main.

> http://homepages.tesco.net/~J.deBoyn...void-main.html


Wow, you can use a search engine.

>
>
>>* The output may never be seen. This is not a problem with flushing
>>(cout will be flushed when it is destroyed on program termination). It
>>is a problem with not properly terminating the line. A C++ program
>>should end it's output with a newline.

>
>
> Obviously you are an armchair programmer since you are merely guessing. All that
> will happen is this...
>
> C:\>Hello
> Hello World
> C:\
>
> Instead of...
>
> C:\>Hello
> Hello World
>
> C:\
>
> No guessing needed! Funny how the real world works like that, eh?


Yeah, funny. There are any number of compilers/systems out there that
won't display this output. We've seen several posts here where somebody
said "My hello world program isn't working" and the problem was actually
that they failed to properly terminate the output. It seems you are the
one that's guessing, based on very limited experience.

>
>
>>* Once the correct header is used, cout is placed in the std namespace,
>>therefore it's full name is std::cout. It can either be referred to
>>using this fully qualified name or the name can be brought into scope
>>using a 'using' statement.

>
>
> Or you can set it in some compilers to default to namespaces by default...which
> most do.


That sentence doesn't even make sense.

A standard-compliant compiler must issue a diagnostic for code that
fails to properly qualify names, or bring them into scope with a 'using'
statement.

>
>
>>* Missing semi-colon after the one and only statement in main().

>
>
> That's because it isn't required in all cases.


It is in this case.

> The bracket takes care of that
> one special case where it isn't required.


I don't know what this means, but the bracket has nothing to do with
your missing semicolon. Though you may be able to find (broken)
compilers that allow 'void main' and no namespaces, I doubt you can find
any compiler that will accept the missing semicolon.

>
>><iostream.h> does not exist in standard C++. It's as simple as that.

>
>
> Yes it does. Just do a file search for it on your computer (presuming you have a
> ISO compliant C++ compiler installed on it).


Again, what's on my computer has nothing to do with what is standard.
<iostream.h> is not standard.

>
> Oh yes, "clearly". Not even close. Haha! You are so full of ****.


Everyone here knows which of us is full of ****.

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Firefox is the worst browser I've ever used. CRILL Firefox 28 05-21-2006 09:09 AM
Worst web page that I ever worked on? Deryck HTML 17 09-12-2004 11:56 AM
Re: The worst 'hello world' example ever written... Greg Comeau C++ 1 09-29-2003 04:15 PM
Re: The worst 'hello world' example ever written... The Saqe C++ 2 09-24-2003 02:23 PM
Worst ever? Clive Moss HTML 14 08-01-2003 12:42 PM



Advertisments