C语言 %ul 和 %lu C 格式说明符有什么区别?

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

What's the difference between %ul and %lu C format specifiers?

cformat-specifiers

提问by pedyram

In an example of "C Primer Plus", the author has used %ulformat specifier (in both scanf and printf) for unsigned long. When I try to generalize the problem, it seems that the %ulmakes something wrong in my computer. But using %lusolved the issue.

在“C Primer Plus”的一个例子中,作者%ulunsigned long使用了格式说明符(在 scanf 和 printf 中)。当我试图概括这个问题时,似乎%ul我的电脑出了问题。但是使用%lu解决了这个问题。

Actually, rather than focusing on the problem and the line of codes, I want to know about the difference between %uland %lu. Maybe I could figure out what's wrong.

实际上,与其关注问题和代码行,我想知道%ul和之间的区别%lu。也许我可以弄清楚出了什么问题。

Searching doesn't give me something useful (except that "they are different").

搜索并没有给我一些有用的东西(除了“它们是不同的”)。

Any explanation or link/reference is appreciated.

任何解释或链接/参考表示赞赏。

回答by Jonathon Reinhart

%luis correct, while %ulis incorrect.

%lu是正确的,虽然%ul不正确

A printfformat specifier follows the form %[flags][width][.precision][length]specifier.

printf格式说明如下形式%[flags][width][.precision][length]specifier

uis a specifiermeaning "unsigned decimal integer".

u是一个说明符,意思是“无符号十进制整数”。

lis a lengthmodifier meaning "long".

l是一个长度修饰符,意思是“长”。

The lengthmodifier should go before the conversion specifier, which means %luis correct.

长度在转换之前修改应该去,其手段%lu是正确的。