Reading long int using scanf

喜欢而已 提交于 2019-12-04 16:28:18

问题


To read an int using scanf we use:

scanf("%d", &i);

What if i is a long not int??

Note: when using %d with long it gives me an irritating warning..


回答1:


Just use

long l;

scanf("%ld", &l);

it gives me an irritating warning..

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




回答2:


For gods sake:

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



回答3:


scanf("%ld", &i);

You can also use "%Ld" for a long long (and depending on your compiler, sometimes also "%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).




回答4:


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 %ld conversion specifier:

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.




回答5:


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



来源:https://stackoverflow.com/questions/2852390/reading-long-int-using-scanf

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!