Understanding how things work.

I just did the hello world program, and I want to make sure I understand how things work before I move on. Could someone verify if the below information is correct?

1. Int main() -->signifies where the program begins operating.

2. std::cout-->says that cout is located in the standard library of terms(functions, constants, classes, objects etc)....

3.Anything between << << will be printed on the screen. As an example If I type << Go home << then Go home will appear in the console.
1.int main() is a function that the program needs in order to run (for non <windows> based operations). It is also where the program starts.
2. is correct.
3. is correct only if you don't have it commented out example(cout<<"hello"/*<<" how are you?"*/;)

technically you could program without telling the compiler that everything is coming from the standard library on every line.
example:

#include<iostream>
using namespace std;
int main(){
cout<<"Hello World!";// no std::cout<< used because 
using namespace std;
was typed below the iostream library. system("pause"); return 0; }
Last edited on
3.Anything between << << will be printed on the screen.


Kind of. But not really. Whatever is sent to cout will be printed on screen. The << operator just says "I'm sending X to Y".

For example:

 
cout << "example";


The << operator here says we're sending "example" to cout. And remember whatever is sent to cout is output on the screen. So this will print "example".

Chaining multiple <<'s together has a similar effect:

1
2
3
4
5
6
// this line...
cout << "foo" << "bar";

// is the same as doing this:
cout << "foo";
cout << "bar";


Both are the same, they are sending "foo", then "bar" to cout.


The key thing to note is that it's cout that is outputting to the screen, not the << operator. As you'll find out soon, the << operator can send to something else, like a file. In which case it wouldn't appear on screen:

1
2
3
4
ofstream myfile("myfile.txt");

myfile << "This will be sent to myfile.txt, not to the screen";
cout << "This will be printed on the screen";
Topic archived. No new replies allowed.