简体   繁体   English

C什么是long unsigned int的简短形式

[英]C what is the short form for long unsigned int

when compiling my program with GCC I get the following warning: 当用GCC编译我的程序时,我得到以下警告:

format ‘%d’ expects type ‘int’, but argument 2 has type ‘long unsigned int

Now just by playing around I realize %lo fixes the warning. 现在只是通过玩耍我意识到%lo修复了警告。 However I don't really understand what I am doing. 但是我真的不明白我在做什么。

Is there a naming convention to get the short form of a type? 是否有一个命名约定来获得类型的简短形式? For example, int is %d, why?? 例如,int是%d,为什么?

Thanks! 谢谢!

long unsigned int = unsigned long

You use %lu to print those. 您使用%lu打印它们。

And similarly, %llu is for unsigned long long . 同样地, %llu用于unsigned long long %llu


%d    -   int
%u    -   unsigned
%ld   -   long
%lld  -   long long
%lu   -   unsigned long
%llu  -   unsigned long long

No, there is convention, you simply need to look at the documentation . 不,有惯例,您只需要查看文档

And the formatting specifiers are not a 1:1 mapping to the type, they control the output format (hence the name). 格式化说明符不是与类型的1:1映射,它们控制输出格式(因此名称)。 There can be many output formats for a single input value type. 单个输入值类型可以有许多输出格式。

For instance, %d is "decimal", since it prints an integer as a decimal number. 例如, %d是“十进制”,因为它将整数打印为十进制数。 There is also %i ("integer") that does exactly the same thing. 还有%i (“整数”)完全相同的东西。

For unsigned int values, you have both %x (print in hexadecimal) and %u (print in octal). 对于unsigned int值,您同时具有%x (以十六进制打印)和%u (以八进制打印)。

The definition of int, long, etc are specific to the target. int,long等的定义特​​定于目标。 If the size of int and long match then a 如果int和long的大小匹配则a

printf("%d",var); 

wont complain, but if long is say 64 bits and int is 32 bits as an example, then you will get the warning/error you describe. 不要抱怨,但如果长的是说64位而int是32位作为例子,那么你将得到你描述的警告/错误。 One of two solutions: 两种解决方案之一:

printf("%ld",var);
or
printf("%d",(int)var);

For the latter of course you have to insure that the compiler associates int with the %d size, if you get yet another warning then adjust accordingly. 对于后者,当然你必须确保编译器将int与%d大小关联,如果你得到另一个警告然后相应地调整。

EDIT: 编辑:

The compiler is trying to help you out, by worrying about C library stuff which is not really the business of the compiler. 编译器试图帮助你,担心C库的东西,这不是编译器的业务。 printf() uses a variable number of arguments, which hopefully you properly matched to your format string. printf()使用可变数量的参数,希望您正确匹配您的格式字符串。 When printf sees a %d on say a 32 bit system it likely will only grab the next 32 bit argument. 当printf在32位系统上看到%d时,它可能只会获取下一个32位参数。 But if you had put a 64 bit integer in as a parameter it may grab one half of that integer, and use the other half as the next item in the format string. 但是如果您将64位整数作为参数放入,则可以获取该整数的一半,并将另一半用作格式字符串中的下一项。 For example 例如

unsigned long ul;
float f;

f=3.4;
ul=0x3F9DF3B612345678;
...
printf("%X %f\n",ul,f);

Depending on your system, endianess, etc, a 32 bit system you should not at all be surprised if the above code produced this output: 根据您的系统,字节顺序等,如果上面的代码产生了这个输出,那么32位系统你根本不应该感到惊讶:

12345678 1.234000

because that is what you told it to to. 因为这就是你告诉它的。 you told it to take the lower 32 bits of ul and print that as hex (%X) and the upper 32 bits of ul and print that as float (%f) and putting f in the function call was a waste you didnt provide any formatting to use the floating point value. 你告诉它采取ul的低32位并打印为十六进制(%X)和ul的高32位并打印为float(%f)并将f放入函数调用是一种浪费你没有提供任何格式化以使用浮点值。

Now depending on the compiler on the target system on a particular day you may have the above code work as desired, take a system/compiler where unsigned long is interpreted as a 32 bit and %X is interpreted as 32 bit, then you get a warning about the 64 bit assignment but the printf makes a little bit more sense. 现在取决于特定日期目标系统上的编译器,你可以根据需要使用上面的代码,取一个系统/编译器,其中unsigned long被解释为32位,%X被解释为32位,然后你得到一个有关64位赋值的警告,但printf更有意义。

Because of this pain, compilers like gcc bother to try to make your life better by assuming that when you use a function called printf() you are using the standard C one and they parse through your format string looking for these types of common mistakes. 由于这种痛苦,像gcc这样的编译器试图通过假设当你使用一个名为printf()的函数时使用标准的C语言来解决这些类型的常见错误,从而试图让你的生活变得更好。

However I don't really understand what I am doing. 但是我真的不明白我在做什么。

What you're doing is telling the printf function how to display the data that you provide following the format string. 你正在做的是告诉printf函数如何显示你在格式字符串后面提供的数据。 You'll probably find that %lo doesn't really print the results you expect -- it prints the right data, but in octal (base 8) format. 您可能会发现%lo并不真正打印出您期望的结果 - 它会打印正确的数据,但是以八进制(基本8)格式打印。

Is there a naming convention to get the short form of a type? 是否有一个命名约定来获得类型的简短形式? For example, int is %d, why?? 例如,int是%d,为什么?

The "short form" is called a format specifier. “简短形式”称为格式说明符。 There's no convention that lets you derive the appropriate printf format specifier. 没有任何约定可以让您派生适当的printf格式说明符。 You just have to look them up in an appropriate reference . 你只需要在适当的参考中查找它们

%lo = unsigned long integer printed in octal digits. %lo =以八进制数字打印的无符号长整数。 yes, docs are a good place to start.. but somethings have a pattern, like o for octal, x for hex, l for anything long etc. Getting used to many such format specifiers will help you get the pattern wherever there is. 是的,docs是一个很好的开始......但有些东西有一个模式,比如o表示八进制,x表示十六进制,l表示长度等等。熟悉许多这样的格式说明符可以帮助你在任何地方获得模式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM