为什么在 C++ 中使用 cin、cout 或 %I64d 优于 %lld 说明符?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15718039/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 19:41:35  来源:igfitidea点击:

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

c++

提问by Rishabh

Why many of the Online Judges advises "do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier." ?

为什么许多在线评委建议“不要使用 %lld 说明符在 С++ 中读取或写入 64 位整数。最好使用 cin、cout 流或 %I64d 说明符。” ?

采纳答案by Mats Petersson

I believe the answer is related to %lldmeans 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 longrather than, say, uint64_t, then I expect %lldis the right thing to use - or it would go wrong the other way around)

我相信答案是与%lld手段long long decimal,这是不能保证是64位。例如,在某些系统上可能是 128 位。(虽然如果变量long long不是,比如说,,uint64_t那么我希望%lld使用正确的东西 - 否则它会出错)

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

不幸的是,在设计printfscanf与他们的兄弟姐妹是这样的编译器实现与格式必须匹配。

Obviously, coutand cinare safe in the sense that the compiler will chose the right output and input translation in itself.

显然,cout并且cin在编译器将自行选择正确的输出和输入翻译的意义上是安全的。

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.

它也可能与“在线法官”使用的编译器有关 - 我认为微软编译器在某些时候支持 64 位整数,但不支持long long,因此没有%lld,但确实有%l64d.

回答by Alexey Frunze

The <<and >>operators on cinand couthave versions for every integer and floating-point type. If you use cinand cout, you just do cin >> integer_variableor cout << integer_variableand you're done, cinand coutwill figure out what to do.

<<>>运营商的cincout对每一个整数版本和浮点型。如果您使用cinand cout,您只需执行cin >> integer_variableorcout << integer_variable就完成了,cin并且cout会弄清楚要做什么。

If you use some sort of printf(), then you must be very careful in what you pass to it. If you pass to it an int, you must also pass its type specifier "%d", for unsigned intit's "%u", for longit's "%ld", for unsigned long longit's "%llu", for size_tit's "%zu"and so on. If you pass a type specifier that doesn't match the type of your integer, you'll invoke undefined behavior. As a result, your program may print wrong numbers or corrupt itself or hang or crash or misbehave in some other mysterious way.

如果您使用某种类型的printf(),那么您必须非常小心传递给它的内容。如果你传递给它 an int,你还必须传递它的类型说明符"%d",因为unsigned int它是"%u"long它是"%ld"unsigned long long它是"%llu"size_t它是"%zu"等等。如果传递的类型说明符与整数类型不匹配,则会调用undefined behavior。结果,您的程序可能会打印错误的数字或损坏自身或挂起、崩溃或以其他一些神秘的方式出现异常行为。

Now, the C++11 language standard (and C99) has at least one integer type that's 64-bit or longer, long long(and its unsigned counterpart, unsigned long long). If you use it, you must be aware that it can be longer than 64 bits. If your compiler provides another type, __int64or int64_t(plus the unsigned version of the same), that's exactly 64-bit, you shouldn't mix and match their type specifiers as it's often mistakenly done. You should still use "%lld"and "%llu"for long longand unsigned long longand whatever's appropriate for __int64(perhaps, "%I64d") and for int64_t(PRId64macro).

现在,C++11 语言标准(和 C99)至少有一种 64 位或更长的整数类型long long(及其无符号对应物,unsigned long long)。如果你使用它,你必须知道它可以长于 64 位。如果您的编译器提供另一种类型,__int64或者int64_t(加上相同类型的无符号版本),那就是 64 位,您不应该混合和匹配它们的类型说明符,因为这通常是错误的做法。你仍然应该使用"%lld"and "%llu"for long longandunsigned long long和任何适合__int64(也许,"%I64d")和 for int64_tPRId64宏)的东西。

Basically, you should either avoid using printf()-likefunctions or be very careful with them.

基本上,您应该避免使用printf()-like函数或非常小心地使用它们。