C++ simple output: cout, <<, endl, \n and Others special characters

In the previous tutorial, we learned how to create the first C ++ program, famous Hello, world! or hello world !.

To do this, we use a very special command: cout.
Let's deepen our studies on this subject.
cout << endl C++ simple output

Simple output (printing on the screen):
cout and <<

What we did in the first C++ program was simply print a message on the screen.
We say that we printed something on the console (black screen of commands), or more specifically, made a C++ output to the screen.

Specifically, cout is a piece called an object. Specifically, an object of the ostream class.
Don't worry about those terms for now. Let's look at it in our C ++ object-oriented tutorials.

This command is standard output stream, ie it is the standard output of information. That is, through cout we can send information to standard output, which is the command terminal, the little black window that appears in front of you, when we run the program.

Let's now display the message: "Welcome to the Progressive C ++ course":
#include <iostream>
using namespace std;

int main()
{
    cout << "Welcome to the Progressive C++ course";
    return 0;
}
Run this program and see the result.

And the << operator signals that we are sending information (in this case, the text) to the cout command:
cout << something (something is being sent to standard output).

We can rewrite the program as follows:
#include <iostream>
using namespace std;

int main()
{
    cout << "Welcome to the"<<"Progressive C++ course";
    return 0;
}
Cool, right?

Let's now write on the screen:
cout command in C++

Just use the cout command twice, do you agree? Look:
#include <iostream>
using namespace std;

int main()
{
    cout << "Welcome to";
    cout << "Progressive C++";
    return 0;
}
Do not forget the semicolon at the end of each command. This is universal in C ++.

The result is:
\n special character

Oops! One minute! It was all stuck together, what was the problem?

End of line endl and \n

A string is nothing more than a text.
After the first sentence, "Welcome to the" there is a character, although we can't see it.

It's the line break, it's the same thing that happens when you wirte a text and hit ENTER key, it skips, it breaks to the next line.

We can solve this by using the endl (end line) command, which is a stream handler, see:
#include <iostream>
using namespace std;

int main()
{
    cout << "Welcome to the" << endl;
    cout << "Progressive C++";
    return 0;
}

Another way to break the line is by using the \ n character.

In this case, it goes inside the string, as if it were a text character:
#include <iostream>
using namespace std;

int main()
{
    cout << "Welcome to the\n";
    cout << "Progressive C++";
    return 0;
}
Note that we can even do this using just one cout command:
#include <iostream>
using namespace std;

int main()
{
    cout << "Welcome to the\nProgressive C++";
    return 0;
}
The character '\n' occupies 1 byte of memory in program execution. endl calls a system function to do the same effect as \n.

C++ special characters

\n is not called a special character. Let's look at some others:

  • \t - has the same effect as TAB in a text editor
  • \r - Carriage return: Position the screen cursor at the beginning of the current line, does not advance to the next line.
  • \a - Alert, on old machines produced a sound (a beep)
  • \\ - Used to print the character \
  • \'- Used to print the character ' (single quotes)
  • \"- Used to print character "(double quotes)

References

http://www.cplusplus.com/reference/iostream/cout/
http://www.cplusplus.com/reference/ostream/endl/

No comments:

Post a Comment