C Plus Plus hello world tutorial
From GMpedia.org Wiki
|
This tutorial works with... |
|
C++ is a very challenging language. It is relatively more complex than the other languages, and requires more coding to do perform the same amount of tasks.
Contents |
[edit] Beginning a Project
[edit] In Visual C++ 6
- File menu>
- New...
- Click on the files tab
- Choose C++ Source File
- Click Ok
[edit] In Visual C++ 2005 Express Edition
- File menu>
- New>
- Project...
- Click on the General item
- Click on Empty Project
- Choose a name for the project
- Click Ok
- Right click in the Project Tree on Source Files>
- Add>
- New Item...
- Click on the Visual C++ item
- Click on C++ Source File
- Click Ok
[edit] The Code
#include <iostream>
int main()
{
std::cout << "Hello World!!\n";
return 0;
}
[edit] Explanation
- The article explains the code and assumes you have little programming knowlege.
- Please check C Plus Plus iostream for other information.
#include <iostream>
This is the single most important line of code in most C++ applications. It initializes the Input/Output Stream, without it, no input or output would be possible.
int main()
{
The "int" stands for integer. That means "int" initializes the function main() as an integer. This might be hard to understand when one starts programming C++, but that's normal. The important thing is knowing that the line of code "int main()" initializes the code. It is in this function that all the actions occur.
{
...
}
The curled brackets define the beginning and ending of a block of code. All the code that we will perform in this tutorial must be in the int main() block of code.
std::cout << "Hello World!!\n";
The line of code above is the actual printing-on-the-screen function. std::cout outputs a string. A string is a type of a variable, that unlike integers, can include text, numbers, and signs. A string begins with a quotation mark and ends with one. The \n part defines a newline, its importance will be discussed later in this article.
return 0;
A return statement marks an end of a function or script. It returns the outcome of the function, but in such a simple function, there's no outcome that needs to be noted, so we type 0.
[edit] Running the script
You can build the C++ project or you can use the Debug function to run it. Note: in Visual C++ 2005 Express Edition, you will not get a "Press Any Key to Continue . . ." message, and the program ends instantly if it is run in debug mode, therefore you will see no result. To fix that, click on the Debug menu on top, then click on the menu item "Start Without Debugging".
[edit] Result
When you run the program you have made, you will see the following result:
Hello World!! Press any key to continue . . .
You may wonder what happened to the \n after the exlamation marks. And the truth is they became a new line! If you hadn't put a \n at the end, the result would be like this:
Hello World!!Press any key to continue . . .

