I haven't found yet an alternative to the scanf
function to store an input number (as an unsigned tiny integer, not really as char) into an unsigned char variable, but, reading around Internet (and A LOT on SO),
I've discovered that if I write std::ios::sync_with_stdio(false);
in my C++ code that doesn't use either printf
or scanf
(but only std::cin and std::cout), the code is faster.
And the problem is that: I need scanf
.
But then I thought if I write a sort of sync_with_stdio
guard just before and after my very very rare scanf
call, would it work?
It works but what about the speed? I don't know, maybe it has also got worse. I'm still a newbie, and I still don't know how to properly profile the performance of the code.
Would it be a good idea?
Code:
#include <iostream>
using std::cin; using std::cout;
int main()
{
unsigned char little_number_from_0_to_255 = 0;
cout << "How old are you (for example)? _";
std::ios::sync_with_stdio(true);
scanf("%hhu", &little_number_from_0_to_255);
std::ios::sync_with_stdio(false);
cout << "You are " << +little_number_from_0_to_255
<< " year/s old.";
}