C++ 使用 scanf 读取 long int

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2852390/
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-28 11:17:07  来源:igfitidea点击:

Reading long int using scanf

c++cstdinscanf

提问by Betamoo

To read an intusing scanf we use:

要读取intusing scanf 我们使用:

scanf("%d", &i);

What if iis a longnot int??

如果ilongint

Note: when using %dwith longit gives me an irritating warning..

注意:%dlong它一起使用时会给我一个刺激性的警告..

回答by jpalecek

Just use

只需使用

long l;

scanf("%ld", &l);

it gives me an irritating warning..

它给了我一个恼人的警告..

That warning is quite right. This is begging for stack corruption.

这个警告说得很对。这是在乞求堆栈损坏。

回答by Betamoo

For gods sake:

看在上帝的份上:

long n;
scanf( "%ld", & n );

回答by John Bode

Each conversion specifier expects its corresponding argument to be of a specific type; if the argument's type does not match the expected type, then the behavior is undefined. If you want to read into a long with scanf(), you need to use the %ldconversion specifier:

每个转换说明符都期望其对应的参数是特定类型的;如果参数的类型与预期类型不匹配,则行为未定义。如果要读入 long with scanf(),则需要使用%ld转换说明符:

long i;
scanf("%ld", &i);

Check the online draft C standard (.pdf file), section 7.19.6.2, paragraph 11 for a complete listing of size modifiers and expected types.

查看在线草案 C 标准(.pdf 文件),第 7.19.6.2 节,第 11 段以获取大小修饰符和预期类型的​​完整列表。

回答by Vineet

scanf("%ld", &i);

You can also use "%Ld"for a long long(and depending on your compiler, sometimes also "%lld").

您也可以使用"%Ld"for long long(并且取决于您的编译器,有时也可以使用"%lld")。

Take a look at the Conversions section of the scanf man page for more. (Just Google it if your system doesn't have manpages).

查看 scanf 手册页的转换部分了解更多信息。(如果您的系统没有联机帮助页,请使用 Google 搜索)。

回答by SunDust

Check this, here is the answer: "%I64d"

检查这个,这是答案:“%I64d”