Hey codeforces! One day I launched MinGW Studio and checked this simple code:
#include <iostream>
using namespace std;
int main() {
int n = 50;
cin >> n;
cout << n;
return 0;
}
I wrote 'z' on the console and saw '50'. Nothing strange. But when I did the same on the last version of CodeBlocks, I saw '0'. Can someone explain me what exactly happens in there and why it happens in CodeBlocks and doesn't happen in MinGW Studio?
Let's read the fabulous manual. First go to documentation on
operator>>
. It says:Follow the link to documentation on
std::num_get::get()
. It describes the process of parsing in details. In particular, section 2 says:Clearly, the string "z" cannot be parsed by
scanf
so Stage 2 terminates and no characters are accumulated. Section 3 says:So we parse the sequence of accumulated characters (the empty string), and this parsing must fail. The way it is parsed depends on the standard.
Documentation on
scanf
doesn't specify what happens to 'receiving arguments' in case of parse error, and I don't have the text of the standard here. But on my compiler the receiving argument doesn't change, and you observed it in 'MinGW Studio'.Documentation on
strtoll
, on the other hand, says:which is exactly what you observed in CodeBlocks.
Thank you for your help!