Search code examples
c++istream

Difference between istream::get(char&) and operator>> (char&)


My question seems to be the same as this one, but I didn't find an answer since the original question seems to ask something more specific. In C++98, what is the difference between

char c;
cin.get(c);

and

char c;
cin >> c;

?

I've checked the cplusplus reference for get and operator>>, and they look the same to me.

I've tried above code and they seem to behave the same when I input a char.


Solution

  • The difference depends on when there is a whitespace character on the stream buffer.

    Consider the input ' foo'

    char c;
    cin.get(c);
    

    Will store ' ' in c

    However

    char c;
    cin >> c;
    

    Will skip the whitespace and store 'f' in c