Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C++ (http://www.programmingforums.org/forum15.html)
-   -   Deciphering Hello World! (http://www.programmingforums.org/showthread.php?t=11394)

funkey_monkey Sep 25th, 2006 6:03 PM

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

DaWei Sep 25th, 2006 6:26 PM

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.

Narue Sep 25th, 2006 6:30 PM

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

bl00dninja Sep 25th, 2006 8:38 PM

:

#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!

magnus.therning Sep 26th, 2006 6:51 PM

Quote:

Originally Posted by Narue (Post 114971)
>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++).

Prm753 Sep 26th, 2006 6:56 PM

Quote:

Originally Posted by magnus.therning (Post 115036)
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

DaWei Sep 26th, 2006 6:59 PM

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.

magnus.therning Sep 26th, 2006 7:15 PM

Quote:

Originally Posted by DaWei (Post 115040)
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 ;-)

DaWei Sep 26th, 2006 7:31 PM

I understand that. It has some characteristics of a NULL pointer, wot?

Narue Sep 26th, 2006 7:56 PM

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


All times are GMT -5. The time now is 1:03 AM.

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