In just about any textbook ever written on C++, you'll find this exercise as the first thing to be done. Say hello to the world of programming. There's really not much to this program, so I'll spend a bit of time explaining what each of the different elements in your code. Since the textbook gives you the code for it, I don't feel that posting the code is a violation of any ethical standard. Here it is:
#include <iostream>
using namespace std;
int main()
{
cout << "Hello World\n";
return 0;
}
There you have it. Run that through the compiler and you'll have the console pop up and say "Hello World", and then return 0. But what does any of that jargon mean? We'll take things one at a time.
#include <iostream>
This command has two parts to it. Part 1 is "#include". The pound sign causes the computer to evaluate that statement in what's called pre-compilation. Before the computer attempts to compile your code, it's going to evaluate the statement following the '#' sign. the "include" tells the computer to include the following statement. In this case, it's a library. Part 2 is the "<iostream>". This library handles input and output from the console. It's short for "input output stream". You're including this library because you want the console to be pulled up and used to display something, which is all handled from this library. You will use the #include for a lot of other libraries throughout your career, so get a good grasp on why it's there, now.
using namespace std;
A namespace is just a library which has certain keywords reserved. For example, "cout" is part of the std (or standard) namespace. When you use that a keyword from a namespace, it can only mean one thing or it becomes an issue. If "cout" had 6 different meanings, how would the compiler know what to do with it?
int main()
{
}
This is called a function. Specifically, the main function. This is what the computer looks for when it compiles a program so it knows what it's supposed to be doing. It's the starting point for any c++ program. The "int" at the beginning indicates that at the end of the function, it's going to return an integer. What is returned from the main function is how you know if a program finished successfully or not. If it's not the number you say to return, which should always be 0, then something went wrong. The parenthesis at the end of the function name (main) are there to indicate that no variables are being passed into main to start with. I could be corrected, but as far as I know Main() should never have any variables being passed into it. I've yet to come across a program that needed a variable passed into main.
cout << "Hello World\n";
This statement is really simple to figure out. "cout <<" means "put the following thing onto the console screen." The '<<' is called an extraction operator, but you don't need to know much about that right now. Just know that if you're putting something out, the greater-than sign is used twice after cout. "Hello World\n" is the text that's being put out by the cout operation. You're literally writing "Hello World", and then a new line. The \n is like pressing enter in a word document. There's another way to do this, by saying
cout << "Hello World" << endl;
but the \n form is easiest to use if you're already putting things in quotation marks. Note that \n ONLY works inside quotation marks. the endl option is done outside quotation marks.
return 0;
This just means you're at the end of your program and it should end. Returning 0 from main tells the program that it has successfully finished. You're done!
I know that was a pretty lengthy post for such a simple program, but I figured I should handle all the basics right from the start so you know what you're actually doing rather than just copying what's in the book. I hope this helped!
#include <iostream>
using namespace std;
int main()
{
cout << "Hello World\n";
return 0;
}
There you have it. Run that through the compiler and you'll have the console pop up and say "Hello World", and then return 0. But what does any of that jargon mean? We'll take things one at a time.
#include <iostream>
This command has two parts to it. Part 1 is "#include". The pound sign causes the computer to evaluate that statement in what's called pre-compilation. Before the computer attempts to compile your code, it's going to evaluate the statement following the '#' sign. the "include" tells the computer to include the following statement. In this case, it's a library. Part 2 is the "<iostream>". This library handles input and output from the console. It's short for "input output stream". You're including this library because you want the console to be pulled up and used to display something, which is all handled from this library. You will use the #include for a lot of other libraries throughout your career, so get a good grasp on why it's there, now.
using namespace std;
A namespace is just a library which has certain keywords reserved. For example, "cout" is part of the std (or standard) namespace. When you use that a keyword from a namespace, it can only mean one thing or it becomes an issue. If "cout" had 6 different meanings, how would the compiler know what to do with it?
int main()
{
}
This is called a function. Specifically, the main function. This is what the computer looks for when it compiles a program so it knows what it's supposed to be doing. It's the starting point for any c++ program. The "int" at the beginning indicates that at the end of the function, it's going to return an integer. What is returned from the main function is how you know if a program finished successfully or not. If it's not the number you say to return, which should always be 0, then something went wrong. The parenthesis at the end of the function name (main) are there to indicate that no variables are being passed into main to start with. I could be corrected, but as far as I know Main() should never have any variables being passed into it. I've yet to come across a program that needed a variable passed into main.
cout << "Hello World\n";
This statement is really simple to figure out. "cout <<" means "put the following thing onto the console screen." The '<<' is called an extraction operator, but you don't need to know much about that right now. Just know that if you're putting something out, the greater-than sign is used twice after cout. "Hello World\n" is the text that's being put out by the cout operation. You're literally writing "Hello World", and then a new line. The \n is like pressing enter in a word document. There's another way to do this, by saying
cout << "Hello World" << endl;
but the \n form is easiest to use if you're already putting things in quotation marks. Note that \n ONLY works inside quotation marks. the endl option is done outside quotation marks.
return 0;
This just means you're at the end of your program and it should end. Returning 0 from main tells the program that it has successfully finished. You're done!
I know that was a pretty lengthy post for such a simple program, but I figured I should handle all the basics right from the start so you know what you're actually doing rather than just copying what's in the book. I hope this helped!