程序打印错误的符号常数值
我目前正在学习C,我正在尝试完成K& r练习2-1。 (编写一个程序来确定char,short,int和长变量的范围 通过打印标准标头的适当值并通过直接计算来签名和未签名。)我编写了以下代码以实现这一点:
#include <stdio.h>
#include <limits.h>
#include <float.h>
int main() {
printf("Unsigned Variable Ranges\n");
printf("Unsigned char: %d %d\n", 0, UCHAR_MAX);
printf("Unsigned short: %d %d\n", 0, USHRT_MAX);
printf("Unsigned int: %d %d\n", 0, UINT_MAX);
printf("Unsigned long: %d %d\n", 0, ULONG_MAX);
}
我通过此操作的思考过程是使用以限制的符号常数。h和float.h打印我的程序中的最小值和最大值。字符和短值正正确打印,但int和long值的打印为-1。终端读取:
Unsigned Variable Ranges
Unsigned char: 0 255
Unsigned short: 0 65535
Unsigned int: 0 -1
Unsigned long: 0 -1
这是我的错误还是编译器/Visual Studio错误?
I am currently learning C and I am attempting to complete the K&R exercise 2-1. (Write a program to determine the ranges of char, short, int, and long variables, both
signed and unsigned, by printing appropriate values from standard headers and by direct computation.) I have written the following code to achieve this:
#include <stdio.h>
#include <limits.h>
#include <float.h>
int main() {
printf("Unsigned Variable Ranges\n");
printf("Unsigned char: %d %d\n", 0, UCHAR_MAX);
printf("Unsigned short: %d %d\n", 0, USHRT_MAX);
printf("Unsigned int: %d %d\n", 0, UINT_MAX);
printf("Unsigned long: %d %d\n", 0, ULONG_MAX);
}
My thought process through this is to use the symbolic constants found in limits.h and float.h to print the minimum and maximum values in my program. The char and short values are printing correctly, but the int and long values are printing as -1. The terminal reads:
Unsigned Variable Ranges
Unsigned char: 0 255
Unsigned short: 0 65535
Unsigned int: 0 -1
Unsigned long: 0 -1
Is this an error on my part or is this a compiler/Visual Studio error?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
对于未签名的整数,您必须使用转换说明符
u
而不是d
。同样,对于长整数的类型,您必须使用长度修饰符l
。因此,printf的呼叫看起来像是
类型
无符号char
和无符号短
您还可以使用转换说明器%d
提供了类型int
可以包含类型整数促销后未签名类型的所有值。For unsigned integers you have to use the conversion specifier
u
instead ofd
. Also for the type long integer you have to use the length modifierl
.So the calls of printf can look like
For the types
unsigned char
andunsigned short
you could also use the conversion specifier%d
provided that the typeint
can contain all values of the unsigned types after the integer promotions of the types.使用printf时,您可以给格式字符串指定您要打印的类型。
%d
是指整数,并且作为无符号int的最大值,而无符号长的最大值不能适合整数中的溢出,并且被打印为> -1
。When using printf you give the format string specifies what type you are printing.
%d
means integer and as both maximum value for unsigned int and the max value for unsigned long can not fit in integer it overflows and is printed as-1
.为了参数,让我们假设
unsigned int
是四个字节宽。uint_max
将为0xffffffff
。但是,您将该值打印为int
(由于%d
)。作为签名的整数,0xffffffff
表示-1
,因为两者的补充。您想做的是打印
uint_max
作为intigned int
,您可以使用%u
而不是%d 。同样,对于
ulong_max
,您需要使用%lu
。For the sake of argument, let's suppose that
unsigned int
is four bytes wide.UINT_MAX
is going to be0xffffffff
. However, you're printing that value as anint
(because of the%d
). As a signed integer,0xffffffff
means-1
because of two's complement.What you want to do is print
UINT_MAX
as anunsigned int
which you can do with%u
instead of%d
. Similarly, forULONG_MAX
, you need to use%lu
.