(C++) Printing text to the screen


Hello world!

Let's take apart a program that prints the phrase "Hello, world!"
#include <iostream>
using namespace std;
int main()
{
   cout << "Hello, World!" << endl;  
   return 0;    
}

Let's analyze the program line by line
Directive #include <iostream> - (from input output stream) - standard library header file for work with input-output streams.

using namespace std; - Import the entire std namespace. This namespace contains all names from the C++ standard library.
 
The namespace is a declarative namespace within which various identifiers (names of types, functions, variables, etc.) are defined. 

Namespaces are used to organise code as logical groups and to avoid naming conflicts that can arise, particularly in cases where the code base includes several libraries. Standard input and output streams with the names cin and cout are described in the std area.
 
cout  <<  "Hello, World!" << endl;

cout is the name of the output stream, that is, the sequence of characters printed on the screen (the sequence of characters printed is written in inverted commas after the two triangular brackets <<).

It is customary to end the output with a line feed, for this purpose the function endl is used, which adds a newline character to the output and clears the buffer. Instead of endl you can directly output a line feed character ("\n"). 
The line feed will allow the next output or system messages to not be "glued" to the previous output.

C++ screen output statement

Let's look at some of the features of the cin output statement.

1) You can write multiple output statements on the same line.
For example, a sequence of statements
cout << "line_of_text1";
cout << "line_of_text2";
can be written on one line
cout << "line_of_text1" << "line_of_text2";
In any case, the phrase line_of_text1 and line_of_text2 will be displayed on the same line.

2) To transfer text to a new line, you can use the sequence of characters "\n", or the endl command;
The next two lines are identical in result. You can use any method
#include <iostream>
using namespace stdl

int main()
{ 
    cout << "line_of_text1 \n" << "line_of_text2" << "\n";  //note the "\n" is written inside the inverted commas
    // or
    cout << "line_of_text2 "<< endl << "line_of_text1" << endl;
    return 0;
}


Advanced material

This material is intended for those wishing to learn the classical C language, and its differences from C ++. Knowledge of this material will help you in solving the olympiad problems.

If you want to make the program faster (for example, when solving olympiad problems), then you can use the format output operator. In general, the formatted output to the screen is as follows:

printf("<format string>", <comma-separated variable names>);   

We will deal with variables in subsequent courses. Output variables is not always needed.
 
format string - this is a line that, in addition to text, can also contain special patterns, which we will also talk about in future courses.

In the general record, the characters <> are used to indicate that the information contained between them may be different, it all depends on the task. When recording a program, the <> characters are omitted.
If you write plain text inside a format string, it will be displayed on one line in the same way as it is written, on one line.
If we need to output something from a new line, then for this a special symbol \n is used in the place where the transition to a new line is planned.

For example,

printf("Everybody \nloves \nkitten\n");  

will output each word from a new line

 

Special characters

Many programming languages have special characters that just can not be output.
For example, commonly used special characters are backslash (\), quotation marks (") and apostrophes (')
Note that a regular slash (/) is not a special character!

To output such characters, we put a \ in front of each of them. That is, if we want to display the sign \, then in the output operator it is necessary to write \\

 
REMEMBER
To display the characters \, ", ', you must put a \ in front of them

Let's practice!