Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Sep 25th, 2006, 5:03 PM   #1
funkey_monkey
Programmer
 
Join Date: May 2006
Posts: 34
Rep Power: 0 funkey_monkey is on a distinguished road
Deciphering Hello World!

Hi,

I've been looking at the program Hello World and I've got some questions about a few of the lines contained in it.

Firstly

#include <iostream>

I believe that this line calls part of C++ Standard Header called IOStream. What is IOStream and where can I get a listing and definition of all headers that can be called?

int main()

This is starting the main part of the program - i.e. where the code is wrote.

std::cout << "Hello World!" << std::endl;
I know this prints the line Hello World! to the command prompt but is there a more simplistic way to do this and can someone please explain the operation of this line to me. Is cout and endl contained within the iostream header file?

return 0
Return 0 means return nothing? I assume thaty the main will always return 0 as it is the top of the hierarchy, but that child procedures called from it and subsequently can return different types of parameters and values?


Thanks guys and I'd appreciate all comments.

FM
funkey_monkey is offline   Reply With Quote
Old Sep 25th, 2006, 5:26 PM   #2
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Look in your compiler's include directory; it's full of include files. Generally, when you consult your documentation regarding library function, it will tell you what headers you need to include.

Main does not have to return a zero. A zero is considered indicative of success.

The '<<' operator of cout puts information into the stream. Endl adds a newline and causes the stream to be flushed. In my view, it couldn't get much simpler.

I would suggest you get a good book on the language. A search of the forum will turn up a number of recommended titles.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Sep 25th, 2006, 5:30 PM   #3
Narue
Professional Programmer
 
Narue's Avatar
 
Join Date: Sep 2005
Posts: 419
Rep Power: 3 Narue is on a distinguished road
>I believe that this line calls part of C++ Standard Header called IOStream.
It doesn't "call" anything. #include is a preprocessor directive equivalent to cut and paste. The entire contents of the file designated by <iostream> are pasted into your file and replace the directive.

>What is IOStream and where can I get a listing and definition of all headers that can be called?
www.dinkumware.com gives a complete listing.

>but is there a more simplistic way to do this
Yes:
#include <cstdio>

int main()
{
  std::printf("Hello, world!\n");
}
It's simpler in concept and implementation, but cout is better.

>can someone please explain the operation of this line to me.
For now, just trust that it works. The iostreams part of the standard library is extremely advanced and intense even for experienced C++ programmers. Using it is much easier than understanding it, which is a part of the design goal.

>Is cout and endl contained within the iostream header file?
Let's say yes and leave it at that.

>Return 0 means return nothing?
It means return success. Any non-zero value signifies failure.

>I assume thaty the main will always return 0 as it is the top of the hierarchy,
>but that child procedures called from it and subsequently can return different
>types of parameters and values?
As written, yes. However, you can also call a function called exit which will immediately return potentially another value.
__________________
Even if the voices aren't real, they have some pretty good ideas.
Narue is offline   Reply With Quote
Old Sep 25th, 2006, 7:38 PM   #4
bl00dninja
Programming Guru
 
bl00dninja's Avatar
 
Join Date: Oct 2004
Location: namespace std
Posts: 1,246
Rep Power: 5 bl00dninja is on a distinguished road
#include <iostream>
// copy that library into your code so you can cin & cout 
//(pastes the whole thing into your code)
using namespace std;
//so you don't have to qualify everything in iostream with
//the namespace

int main()//there is only 1 main and it returns an int
//0 for success, something else for failure
//body of main
{
cout<<"hello n00b"<<endl;
//we have stated that the namespace is std, so we don't have to do THIS:
//std::cout<<"hello n00b"<<std::endl;
return 0;
//main must return an int, 0 says no problems, you can insert your own #'s
//if main failed to allocate memory then you can return 1, or 2,or 87267659265

/*when you see an app that says "returned 24875287, memory fault", that's what we're talking about here, error codes let the programmer define bad situations so they can fix them */
}

good luck!
__________________
i put on my robe and wizard hat...

Have you ever heard of Plato, Aristotle, Socrates?...Morons.
bl00dninja is offline   Reply With Quote
Old Sep 26th, 2006, 5:51 PM   #5
magnus.therning
Programmer
 
Join Date: May 2006
Location: Cambridge, UK
Posts: 36
Rep Power: 0 magnus.therning is on a distinguished road
Quote:
Originally Posted by Narue View Post
>Return 0 means return nothing?
It means return success. Any non-zero value signifies failure.
To be picky, and difficult--I believe 0==success is system dependent as well. To be sure return EXIT_SUCCESS, which is defined in stdlib.h (cstdlib in C++).
__________________
Don't comment bad code - rewrite it.
- The Elements of Programming Style (Kernighan & Plaugher)
magnus.therning is offline   Reply With Quote
Old Sep 26th, 2006, 5:56 PM   #6
Prm753
Professional Programmer
 
Prm753's Avatar
 
Join Date: Oct 2005
Location: United States
Posts: 447
Rep Power: 3 Prm753 is on a distinguished road
Send a message via AIM to Prm753 Send a message via MSN to Prm753
Quote:
Originally Posted by magnus.therning View Post
To be picky, and difficult--I believe 0==success is system dependent as well. To be sure return EXIT_SUCCESS, which is defined in stdlib.h (cstdlib in C++).
What system are you talking about? My C++ book for Windows and my C book for Linux both use return 0; at the end of an example, and I thought that returning EXIT_SUCCESS was very similar (if not the same) to returning 0. Could be wrong about that though. :p
__________________
The world's first athletic computer geek!
The home of PrProgramsStudios
How not to post a question: <-- Please don't reply
Prm753 is offline   Reply With Quote
Old Sep 26th, 2006, 5:59 PM   #7
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
From the standard: "Finally, control is returned to the host environment. If the value of status is zero or EXIT_SUCCESS, an implementation-defined form of the
status successful termination is returned."

This seems to imply that EXIT_SUCCESS might not be 0, but that a zero will indicate a successful termination, even should the implementation care to remap it to a non-zero value.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Sep 26th, 2006, 6:15 PM   #8
magnus.therning
Programmer
 
Join Date: May 2006
Location: Cambridge, UK
Posts: 36
Rep Power: 0 magnus.therning is on a distinguished road
Quote:
Originally Posted by DaWei View Post
From the standard: "Finally, control is returned to the host environment. If the value of status is zero or EXIT_SUCCESS, an implementation-defined form of the
status successful termination is returned."

This seems to imply that EXIT_SUCCESS might not be 0, but that a zero will indicate a successful termination, even should the implementation care to remap it to a non-zero value.
Apparently it was not 0 on some historic VAX system. I said I was knit-picking ;-)
__________________
Don't comment bad code - rewrite it.
- The Elements of Programming Style (Kernighan & Plaugher)
magnus.therning is offline   Reply With Quote
Old Sep 26th, 2006, 6:31 PM   #9
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
I understand that. It has some characteristics of a NULL pointer, wot?
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Sep 26th, 2006, 6:56 PM   #10
Narue
Professional Programmer
 
Narue's Avatar
 
Join Date: Sep 2005
Posts: 419
Rep Power: 3 Narue is on a distinguished road
>Apparently it was not 0 on some historic VAX system. I said I was knit-picking ;-)
0 is 0, but this concept causes untold confusion. Usually the confusion is with null pointers, where 0 is a null pointer, but a null pointer isn't necessarily 0.
__________________
Even if the voices aren't real, they have some pretty good ideas.
Narue is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
World cup 2006 Edgar Coder's Corner Lounge 127 Jul 11th, 2006 3:09 AM
World of WarCraft Ghost Project Ideas 19 Nov 1st, 2005 12:55 AM
Hello World! - C++ Tutorial coldDeath C++ 27 Sep 27th, 2005 1:15 PM
Hello World! Markpy Community Introductions 13 Sep 1st, 2005 6:34 PM
the silmarillion arod199113 Coder's Corner Lounge 45 Aug 30th, 2005 3:11 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 7:55 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC