How’s the cin Object & Insertion (>>) Operator Work in Backend in C++?
By: Talha Tariq
Tags: C Programming, Insertion Operator, Cin In Cplusplus, Input Output, Programming Languages

Today We’re going to discuss cin object with >> operator in detail in C++.

Let’s see an example first by a part of the code below!👇

At line no 7, the program will require an integer for the variable (var) from the user via some input device (most likely Key-board) and stopped further execution of the program until the user provides input.

Let’s suppose we enter 31, and you can say that it assigned to a variable (var) or stored in the memory location of var. After that Execution starts again, and 31 printed on the console due to the statement at Line no 8(see the picture below👇).

Congrats! You know this already. So, It’s not a new thing for you.😃

But do you know what’s going on in the backend? How’s this value stored in the memory location of Variable?

Here the concept of Insertion operator (>>) jumps into.

>> is an operator used to extract a formatted value from an input stream. The cin object is associated with the input device(Keyboard) on one side and the input stream on the other side.

Read the following points carefully!

→ If the stream is empty, >> suspends the program temporarily and waits for the user to supply (enter) values to stream using some input device usually the keyboard.

→User can type any sequence of characters which goes into steam when the ENTER key is pressed. On the ENTER key, typed characters go into the stream, and execution of the program's resume was suspended.

Let’s see the previous example again.👇

Now the input stream is like this box of below diagram(Suppose for understanding):👇

The stream is empty. So >> Operator stops execution of the program and waits for input from the user. Then Let’s see we enter 19 and pressed Enter after it. Stream be like (suppose):👇

Now the >> starts extracting value from the input stream and then 19 will be stored in the memory location of Variable (var) and the Program started executed again from the next Line.

This is all about cin Object and Insertion Operator (>>).

But wait! Real Surprise is still not covered yet.😋

While giving input, if we press the space key and after the space key, we entered another value. Then while extracting from the stream when the pointer reached space, it terminates extracting and the next value still in the input stream.

Now Stream is not empty. Didn’t get it?🥱 Let’s See an Example Program.👇

This program is quite simple to understand. There is two variable. One is var1, the other is var2.

First of all the process of execution will stop and waits for the user to input the 1st variable then at the next statement it stops and waits for the user input for the 2nd variable.

And at last, both values will print out on the console like this.👇

→But what will be happened if I do this?👇🤔

These values go into the Input stream and it will look like this👇

→ Now, >> operator started extracting value from the stream and it will stop when space comes, and 19 stored in memory location of var1 and will start executing program again. (Stream is not empty yet! 22 is still there)

Now, Stream is like:👇

→ But when it comes to 2nd variable(Line number 8). But now the stream is not empty. It still has the integer 22 in it. So instead of taking it from user. The >> operator store 22 to the second variable(var2) & in this way both >> and cin objects works together.

Console will be like this(You can check that >> didn't wait for user to input value for 2nd variable, because its already in input stream):👇

Hope that you would get it.😃

Thanks & Happy Coding.