Why is cout used in C.

Speech is silver, silence is gold. You will certainly be delighted that the computer can calculate so wonderfully. But even if curiosity isn't one of your vices, at some point you will want to know what the computer found out. An output function is needed!

At this point, the input and output on the screen is described to such an extent that you can write meaningful programs. You can find more detailed information on the subject elsewhere.

Output stream to cout

C ++ uses the data stream model for input and output. The data stream for input and output is handled in a separate library. To use it, the following line must be entered before using it for the first time:


In some listings you will also see that iostream is used without the .h extension. If you're using a reasonably up-to-date compiler, you can do both. The difference has to do with the use of namespaces.

To display data, they are on the data output cout (The "out" in cout is English and means "out". So it is the output object that is redirected to. The "c" is in front of it because it is Bjarne Stroustrup's favorite letter. That is why his language is C ++ called.) directed. First of all, the data target is mentioned. Then two smaller characters are used to indicate the direction of the data in the direction of the output.

[Output of a variable]

cout << MyNumberValue; cout << endl;

In the example, the variable MeinZahlenWert issued. In the line below it will be final sent on the data stream. This is not a self-defined variable, but a predefined constant for the end of the line. The usage of final also ensures that all data to be displayed appear immediately on the screen.


For the sake of clarity or to save typing, several output objects can be listed directly one after the other, separated by the double lowercase characters.

[Output chain]

cout << MyNumberValue << endl;

Accompanying texts

You can output not only variables, but also constants in this way. This is particularly interesting for strings with which you can add a few explanatory texts to your program output.

[Output chain]

cout << "Result:" << MyNumberValue << endl;

This skilled example of dry computer science poetry tells the user that the value that now appears on the screen is the result of the program. Such information is what defines a user-friendly program.

Formatted output

If you have multiple numerical values ​​in succession cout redirect, you will find that they are displayed next to each other without a separator. The output of a value 8 and the subsequent output of 16 would appear as 816. Accordingly, you should put at least a few spaces or a few explanatory words in between:

cout << "Input was:" << Input << "The double is:" << Output << endl;

Column by column

It is more difficult when you want to display tables. To know how much space to put between numbers, you would first need to determine how many digits each number has. To make this easier, there is something called a manipulator called. "Setw" stands for "set width", ie "set width". So the w has nothing to do with my favorite letter. This is sent before the actual output cout and is preparing to format the following output. Write the number of positions that are to be reserved for subsequent output between the brackets of. All positions that are not occupied by the number itself are padded with spaces so that the number appears right-justified. The following example should ensure that the numbers of the two output lines are right-aligned next to each other. cout << setw (7) << 3233 << setw (6) << 128 << endl; cout << setw (7) << 3 << setw (6) << 1 << endl;

You can find more information about manipulators elsewhere.

Input stream from cin

The data source is used to read input from the keyboard cin (The "in" in cin stands for "a". So it is the input object. The origin of the c was already explained in cout.) Redirected to the variable. The input operator is exactly the mirror image of the output operator and consists of two greater than signs. You basically point from the cin to the variable in which the input is to be stored.

[A simple user dialog (reinraus.cpp)]

#include main () {int number input; int double; cout << "Please enter a number!" << endl; cin >> number entry; Double = number entry * 2; cout << "Double this number is" << double << "." << endl; }