1

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.";
}
thomas
  • 111
  • 8
  • Please don't spam that tags, the question has nothing to do with C. – Pablo Mar 27 '22 at 22:57
  • 2
    Why do you think you need to use `scanf()`? The C++ iostream system is supposed to be a replacement for C's standard I/O. – Jonathan Leffler Mar 27 '22 at 22:59
  • 1
    q.v. https://stackoverflow.com/a/58455871/4641116 – Eljay Mar 27 '22 at 23:05
  • Please read [ask] and https://stackoverflow.com/help/dont-ask. We need *objective* criteria in order to have an answerable question. If you are concerned about performance, best to test it yourself first. – Karl Knechtel Mar 27 '22 at 23:21

1 Answers1

0

First, you don't need scanf and I personally feel it's a mistake to use old-school C routines for reading input. What I would do is use getline() and parse it myself with std::stoi()'. That gives you greater capability for error detection and reporting.

Next, I also consider it a mistake to use unsigned char because you want an integer value. Space is just not that short, and it's misleading. For learning to code, yeah, fine, do what you want. But if you become a professional, it's confusing. You can write that code, but someone else is going to have to maintain it. Just use an int.

Joseph Larson
  • 8,530
  • 1
  • 19
  • 36
  • 1
    Please read [answer]. While your opinions are entirely reasonable, they are still opinions - and we deal in the objective here. The question solicits opinions such as this - which is exactly why, as written, it should be closed and not answered (see also https://stackoverflow.com/help/dont-ask). – Karl Knechtel Mar 27 '22 at 23:22
  • We do not remotely "deal in the objective" here. That is a conceit held by those who think that their opinions are objective. But it's pretty to think so. – Joe Mar 28 '22 at 00:54
  • @KarlKnechtel I look forward to your better answer, then. – Joseph Larson Mar 28 '22 at 15:54
  • "I look forward to your better answer, then." What part of "as written, [the question] should be closed and not answered" was unclear? – Karl Knechtel Mar 28 '22 at 22:21