c++64-bitiostreamformat-specifierslong-long

Why is the use of cin, cout or %I64d preferred over %lld specifier in C++?


Why do many of the online judges advise "do not use the %lld specifier to read or write 64-bit integers in С++"?

Is it preferred to use the cin, cout streams or the %I64d specifier?


Solution

  • I believe the answer is related to %lld means long long decimal, which isn't guaranteed to be 64-bit. It could be, for example 128 bit on some systems. (Although if the variable is long long rather than, say, uint64_t, then I expect %lld is the right thing to use - or it would go wrong the other way around)

    Unfortunately, the design of printf and scanf and their siblings is such that the compiler implementation and the format must match.

    Obviously, cout and cin are safe in the sense that the compiler will chose the right output and input translation in itself.

    It may also have something to do with what compiler(s) the "online judges" use - I think Microsoft compilers at some point supported 64-bit integers, but not long long, and thus didn't have %lld, but did have %l64d.