Difference between revisions of "Hello World"

From TRCCompSci - AQA Computer Science
Jump to: navigation, search
(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...")
 
Line 12: Line 12:
 
Line 5: std::count << "Hello World!";<br/>
 
Line 5: std::count << "Hello World!";<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 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 it is moved directly to the local namespace.
 +
 +
<syntaxhighlight lang="cpp" line>
 +
#include <iostream>
 +
using namespace std;
 +
 +
int main()
 +
{
 +
  cout << "Hello World!";
 +
}
 +
</syntaxhighlight>
 +
 +
in this case std::cout is no longer required &amp one can just use cout.

Revision as of 18:07, 30 April 2017

1 #include <iostream>
2 
3 int main()
4 {
5   std::cout << "Hello World!";
6 }

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::count << "Hello World!";
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 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 it is moved directly to the local namespace.

1 #include <iostream>
2 using namespace std;
3 
4 int main()
5 {
6   cout << "Hello World!";
7 }

in this case std::cout is no longer required &amp one can just use cout.