程序打印错误的符号常数值

发布于 2025-02-11 03:04:47 字数 770 浏览 2 评论 0原文

我目前正在学习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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

水水月牙 2025-02-18 03:04:48

对于未签名的整数,您必须使用转换说明符u而不是d。同样,对于长整数的类型,您必须使用长度修饰符l

因此,printf的呼叫看起来像是

printf("Unsigned char:  %u    %u\n", 0, UCHAR_MAX);
printf("Unsigned short: %u    %u\n", 0, USHRT_MAX);
printf("Unsigned int:   %u    %u\n", 0, UINT_MAX);
printf("Unsigned long:  %d    %lu\n", 0, ULONG_MAX);

类型无符号char无符号短您还可以使用转换说明器%d提供了类型int可以包含类型整数促销后未签名类型的所有值。

For unsigned integers you have to use the conversion specifier u instead of d. Also for the type long integer you have to use the length modifier l.

So the calls of printf can look like

printf("Unsigned char:  %u    %u\n", 0, UCHAR_MAX);
printf("Unsigned short: %u    %u\n", 0, USHRT_MAX);
printf("Unsigned int:   %u    %u\n", 0, UINT_MAX);
printf("Unsigned long:  %d    %lu\n", 0, ULONG_MAX);

For the types unsigned char and unsigned short you could also use the conversion specifier %d provided that the type int can contain all values of the unsigned types after the integer promotions of the types.

始于初秋 2025-02-18 03:04:48

使用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.

意中人 2025-02-18 03:04:48

为了参数,让我们假设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 be 0xffffffff. However, you're printing that value as an int (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 an unsigned int which you can do with %u instead of %d. Similarly, for ULONG_MAX, you need to use %lu.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文