![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programmer
Join Date: May 2006
Posts: 34
Rep Power: 0
![]() |
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; return 0 Thanks guys and I'd appreciate all comments. FM |
|
|
|
|
|
#2 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#3 |
|
Professional Programmer
![]() Join Date: Sep 2005
Posts: 419
Rep Power: 3
![]() |
>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");
}>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. |
|
|
|
|
|
#4 |
|
Programming Guru
![]() Join Date: Oct 2004
Location: namespace std
Posts: 1,246
Rep Power: 5
![]() |
#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. |
|
|
|
|
|
#5 |
|
Programmer
Join Date: May 2006
Location: Cambridge, UK
Posts: 36
Rep Power: 0
![]() |
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) |
|
|
|
|
|
#6 |
|
Professional Programmer
|
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 |
|
|
|
|
|
#7 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#8 | |
|
Programmer
Join Date: May 2006
Location: Cambridge, UK
Posts: 36
Rep Power: 0
![]() |
Quote:
__________________
Don't comment bad code - rewrite it. - The Elements of Programming Style (Kernighan & Plaugher) |
|
|
|
|
|
|
#9 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#10 |
|
Professional Programmer
![]() Join Date: Sep 2005
Posts: 419
Rep Power: 3
![]() |
>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. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
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 |