page was last updated: Sep 07, 2016 04:11
forum
sorry ): this page is incomplete. feel free to ask me directly for any help ^_^
Hello World
A2
Hello World!
|
In this tutorial, we'll talk about a basic c++ program structure. Our first program!
//this is a comment (single line)
/*
this is a comment (multi line)
*/
#include <iostream>
using namespace std;
int main ()
{
cout << "Hello World!" << endl;
return 0;
}
DONE! that is the class "Hello World" example. Let's go over whats going on...
#include <iostream>
Here, we are saying "hey compiler, copy the "iostream" code right here". "iostream" is where the code exists for
putting things onto the screen and getting stuff from the user. C++ is very explicit in what code does and does not exist
inside your program. It doesn't even have built in stuff for that. This way, it is explicit in being lightweight if you so desire.
The "iostream" is a standard library that is common in c++ and an easy way for everyone to do IO.
The # is a preprocessor directive... more about that in a later lesson .
using namespace std;
This is a lil complicated for beginners. It lets us implicitly use the "std" namespace all the time...
Basically it prevents us from needing to use std:: everywhere that you may
see in other tutorials or code. Let's just save this for a later tutorial.
The ; is how you end a line of code or instuction in c++.
int main () {...}
This is where all programs start! When you run a program, the operating system goes "wheres main?" and starts executing code from here. There
is actually quite a few things going on here. First, this is also a function, a block of code in a sense. Almost all of your code is
gonna be within the curly brackets { } for a while. The OS will execute everything within the brackets. At the end of
the main function, your program is done.
cout << "Hello World" << endl;
cout is short for "character out". It outputs characters to the screen! Simple. However, it's use is weird.
after the <<, it will do its best to output whatever is after it onto the screen.
"Hello World" is the string to output to the screen. Anything in the quotes will be be put onto the screen with
the exception of a few things like " and \. It MUST be in double-quotes.
endl
This adds the "end of line" character. "\n" also works (you can add it to the string like "Hello World\n".
There is a technical difference but its not important for now. And again, ; means
were done with this instruction!
NOTE: cout statements can be chained together like this....
cout << "I" << "am gonna" <<"add more << endl << "NEW LINE" << "HEHEHE" << endl;
I am gonna add more
NEW LINE HEHEHE
|
return 0;
A return statement ends the function and makes the result of the entire function "0". The OS looks for a status code after the program exits that
states what happened. A "0" means, "all good" and the OS knows that the program run successfully without errors.
// comment - Not code. Comment a single line
/* block comment */ - Not code. Comment multiple lines.
#include <iostream> - Brings in input/output code
using namespace std; - Shortcut std::. Just do it for now.
int main () {...} - A function. Program starts here. Does everything inside brackets.
cout << "Hello World" << endl; - CharacterOUT. Puts stuff on the screen.
return 0; - exit function (in this case, program). zero means "all good, no errors".
DONE! This is a typical test and intro program. NEXT... variables and basic math.