>>   <<  are just syntactical sugar!!!
Разница между en3 и en4, 119 символ(ов) изменены
`cin >> x`, just makes so much sense now.↵

#### 
**Analogy**

This thing is just _**syntax sugar**_ which c++ provides.↵

Like when we want to write: `x = x + a`↵
We can *instead* write: `x += a`↵

Which is exactly the same thing.↵

<br>↵
#### **What is** `operator>>`↵

Similarly we have `operator>>`, which is used to take in input from an input stream. And depending on the type for the `x` variable two things can happen.↵

If `x` is a built-in type then `cin.operator>>(x):` is called, which is a **member function** of the `cin` class, and has been defined for a built-in type.↵

But if `x` is not a built-in type, then we have to **manually** provide how the input is going to be interpreted.↵

By defining a function with the below signature:↵


```C++↵
   std::istream operator>>(std::istream& is, my_type& x){};↵
```↵


This is a function which takes in a reference to an input stream and a reference to our variable `x` and defines **how** `x` is going to be filled.↵

and when we do: `cin >> x;`↵

For our custom type `x`, the above function is called: `operator>>(cin,x);`↵

And not: `cin.operator>>(x);`↵

Since there is no **member function** of `cin` defined for type of `x`.↵

That is **crazy**. And just makes so much sense now, `>>` and `<<` are just **syntactical sugar** over the actual function calls.↵

<br>↵
#### **same for** `operator<<`↵

Again for outputting two things could happen depending on the type of `x`.↵

If x is a built-int type then: `ostream.operator<<(x);`↵

If x is not a built-in type then: `operator<<(ostream,x);`↵

<br>↵
#### **why** `cin >> x >> y >> z;` **works?**↵

Now the amazing thing about why we can do this: `cin >> x >> y >> z;` ( symmetrically the same applies to `operator<<` for outputting)↵

This is because `operator>>` returns an **input stream** and if we remove the syntactical sugar with the *actual function* calls being made **in order**, we have something like this:↵



```C++↵
cin >> x >> y >> z;↵

cin.operator>>(x) >> y >> z;↵

cin.operator>>(x).operator>>(y) >> z;↵

cin.operator>>(x).operator>>(y).operator>>(z);↵

```↵


And this makes sense because `cin.operator>>(x)` returns an **input stream** and then we get the `.operator>>()`  **member function for that returned input stream** and call that with `y` and so on with `z`.↵

<span style="font-size:16px; font-weight:500;">That is just so cool and everything seems to be demystified now. That is crazy. really.</span>

![ ](/predownloaded/c4/f5/c4f5296e0b383569c3d989abf0206e001fc491e0.png)↵

<span style="font-size:16px; font-weight:500;">PS: This is my first blog, please point out the mistakes I made.</span>

История

 
 
 
 
Правки
 
 
  Rev. Язык Кто Когда Δ Комментарий
en8 Английский walrus137 2025-09-23 17:42:10 18
en7 Английский walrus137 2025-09-23 17:32:17 109
en6 Английский walrus137 2025-09-23 07:49:55 1 Tiny change: 'd::istream operator>' -> 'd::istream& operator>'
en5 Английский walrus137 2025-09-23 06:54:45 9
en4 Английский walrus137 2025-09-23 06:54:20 119 Tiny change: '.\n\n#### Analogy\n\nThis t' -> '.\n\n#### **Analogy**\n\nThis t' (published)
en3 Английский walrus137 2025-09-23 06:46:54 326 Tiny change: 'ere is no member function of `cin` ' -> 'ere is no _member function_ of `cin` '
en2 Английский walrus137 2025-09-23 06:12:48 207 Tiny change: 'cin >> x, just mak' -> '`cin >> x`, just mak'
en1 Английский walrus137 2025-09-23 05:57:52 2232 Initial revision (saved to drafts)