Difference between revisions of "Hello World"
(Created page with "<syntaxhighlight lang="cpp" line> #include <iostream> int main() { std::cout << "Hello World!"; } </syntaxhighlight> Line 1: #include <iostream><br/> Lines beginning with a...") |
(→Note - Improvement) |
||
(4 intermediate revisions by 3 users not shown) | |||
Line 1: | Line 1: | ||
+ | =Using Visual Studio= | ||
+ | |||
+ | Create a new project in Visual Studio, and choose 'C++' and I would recommend using the 'Empty Project'. | ||
+ | |||
+ | Now in the interface, find 'Solution Explorer': | ||
+ | |||
+ | [[File:CPPSE.png]] | ||
+ | |||
+ | You will notice a section for 'Header' files and a section for 'Source' files. Right click on the 'Source Files' and choose Add, and then new item. Choose a Source file, and call it 'main.cpp'. | ||
+ | |||
+ | =Helloworld= | ||
+ | Now type the following into your 'main.cpp' file: | ||
+ | |||
<syntaxhighlight lang="cpp" line> | <syntaxhighlight lang="cpp" line> | ||
#include <iostream> | #include <iostream> | ||
Line 4: | Line 17: | ||
int main() | int main() | ||
{ | { | ||
− | std::cout << "Hello World!"; | + | std::cout << "Hello World!" << std::endl; |
+ | return 0; // Must return an integer (int main) | ||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
− | Line 1: #include <iostream><br/> | + | '''Line 1: #include <iostream><br/>''' |
Lines beginning with a hash sign (#) are directives read and interpreted by what is known as the preprocessor. They are special lines interpreted before the compilation of the program itself begins. In this case, the directive #include <iostream>, instructs the preprocessor to include a section of standard C++ code, known as header iostream, that allows to perform standard input and output operations, such as writing the output of this program (Hello World) to the screen. | Lines beginning with a hash sign (#) are directives read and interpreted by what is known as the preprocessor. They are special lines interpreted before the compilation of the program itself begins. In this case, the directive #include <iostream>, instructs the preprocessor to include a section of standard C++ code, known as header iostream, that allows to perform standard input and output operations, such as writing the output of this program (Hello World) to the screen. | ||
− | Line 5: std:: | + | '''Line 5: std::cout << "Hello World!" << std::endl;<br/>''' |
This statement has three parts: First, std::cout, which identifies the Standard Character output Device (usually, this is the computer screen). Second, the insertion operator (<<), which indicates that what follows is inserted into std::cout. Finally, a sentence within quotes ("Hello world!"), is the content inserted into the standard output. | This statement has three parts: First, std::cout, which identifies the Standard Character output Device (usually, this is the computer screen). Second, the insertion operator (<<), which indicates that what follows is inserted into std::cout. Finally, a sentence within quotes ("Hello world!"), is the content inserted into the standard output. | ||
+ | |||
+ | ===Note - Improvement=== | ||
+ | Note one doesn't have to use the :: (namespace) operator, if need be one can set a using directive for the namespace so everything contained within the namespace is accessible directly from the scope of the file. | ||
+ | |||
+ | <syntaxhighlight lang="cpp" line> | ||
+ | #include <iostream> | ||
+ | using namespace std; // added line so std:: not required lower down | ||
+ | |||
+ | int main() | ||
+ | { | ||
+ | cout << "Hello World!" << endl; | ||
+ | return 0; // Must return an integer (int main) | ||
+ | } | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | in this case std::cout is no longer required & one can just use the keyword cout. |
Latest revision as of 09:50, 13 June 2019
Using Visual Studio
Create a new project in Visual Studio, and choose 'C++' and I would recommend using the 'Empty Project'.
Now in the interface, find 'Solution Explorer':
You will notice a section for 'Header' files and a section for 'Source' files. Right click on the 'Source Files' and choose Add, and then new item. Choose a Source file, and call it 'main.cpp'.
Helloworld
Now type the following into your 'main.cpp' file:
1 #include <iostream>
2
3 int main()
4 {
5 std::cout << "Hello World!" << std::endl;
6 return 0; // Must return an integer (int main)
7 }
Line 1: #include <iostream>
Lines beginning with a hash sign (#) are directives read and interpreted by what is known as the preprocessor. They are special lines interpreted before the compilation of the program itself begins. In this case, the directive #include <iostream>, instructs the preprocessor to include a section of standard C++ code, known as header iostream, that allows to perform standard input and output operations, such as writing the output of this program (Hello World) to the screen.
Line 5: std::cout << "Hello World!" << std::endl;
This statement has three parts: First, std::cout, which identifies the Standard Character output Device (usually, this is the computer screen). Second, the insertion operator (<<), which indicates that what follows is inserted into std::cout. Finally, a sentence within quotes ("Hello world!"), is the content inserted into the standard output.
Note - Improvement
Note one doesn't have to use the :: (namespace) operator, if need be one can set a using directive for the namespace so everything contained within the namespace is accessible directly from the scope of the file.
1 #include <iostream>
2 using namespace std; // added line so std:: not required lower down
3
4 int main()
5 {
6 cout << "Hello World!" << endl;
7 return 0; // Must return an integer (int main)
8 }
in this case std::cout is no longer required & one can just use the keyword cout.