10 January 2015

Hello World in C++

In coding, the most basic program is called Hello World. This is true for all languages. I'm going to walk you through how to do one in C++ and how everything works.

#include <iostream>
using namespace std;

int main() {

           cout << "Hello, World !!" << endl;
           return 0;
}


If you run this program, the message between the quotations will output to the console.

The first line #include <iostream>  lets you use everything in the iostream header file. This includes cout, which is used to output to the console. If you were to want a user to input an answer, you would use cin, which is for console input. Because later in the program we use cout, we must include this header file.

using namespace std; is to let the program know that you want to use the standard namespace. You don't need to have this line if you were to type std::cout later.

Note : The semicolons !! C++ requires semicolons when you're ending a line. Sometimes you don't need one, like after functions. int main() is a function, which is defined between the { }.

int main() is indeed a function. Functions always have parenthesis. When there are parameters going into the function, the parenthesis are not empty, but contain the parameter(s). The int part means that the function is going to return an int, or integer. You can see that later where there is a return 0;.

cout << "Hello, World !!" << endl; contains lots of things. We've already covered the cout. The less-than signs are just C++ syntax. The message is in quotations, because it's what you call a string, which is a data type, like int. The endl; means that after that message, that line on the console is over. If something else were to be printed to the console, it would be on the next line, whereas if that endl; weren't there, the next thing would be printed next to the first message on the same line.

The last line is just a curly bracket, because you must close all your brackets and parenthesis.



Farewell pic :

#MeanGirls



                                                                                 ~ Cici

No comments:

Post a Comment