关于 C 整数类型和 printf() 说明符的奇怪问题

发布于 2024-11-06 05:50:17 字数 714 浏览 1 评论 0原文

好吧,我知道这个问题可能有些奇怪,但我仍然想揭开它的神秘面纱。

1.) C 中的 int 类型可以存储 -2147483648 到 2147483647 范围内的数字。

2.) 如果我们在其前面附加一个 unsigned ,则该范围将变成 0 到 2147483647。

3.) 问题是,当下面的代码时,为什么我们还要费心使用关键字 unsigned实际上可以起作用。


代码:

#include <stdio.h>

int main(void)
{
    int num = 2147483650;

    printf("%u\n" , num);

    return 0;
}


4.)如您所见,如果我使用 %u 说明符,我仍然可以将整数打印为无符号类型,并且它将向我打印值 2147483650

5.)即使我创建另一个值为 50 的整数类型并用 num 求和,虽然它溢出了,但我仍然可以通过使用打印出正确的总和值%u 说明符。那么为什么 unsigned 关键字仍然是必要的?

感谢您花时间阅读我的问题。

Alright i know this question might some weird , but still i wanted to demystify it.

1.)an int type in C can stores number in the range of -2147483648 to 2147483647.

2.)If we append an unsigned it front of it , the range would become 0 to 2147483647.

3.)The thing is , why do we even bother to use the keyword unsigned when the code below could actually works.

The Code:

#include <stdio.h>

int main(void)
{
    int num = 2147483650;

    printf("%u\n" , num);

    return 0;
}

4.)As you see , i can still print out the integer as unsigned type if I use the %u specifier and it will print me the value 2147483650.

5.)Even if I create another integer type with value 50 and sum it up with num , although it's overflow but yet I still can print out the correct sum value by using %u specifier.So why unsigned keyword is still a necessity??

Thanks for spending time reading my question.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

咆哮 2024-11-13 05:50:17
  1. 不,这仅在某些平台上成立(其中 int 是 32 位,2 的补码)。

  2. 否,在这种情况下,范围将为 0 到 4294967295。

  3. 该代码表现出未定义的行为。< /p>

  4. 参见 3。

  5. 参见 2.和 3。

  1. No, this is true only on certain platforms (where an int is 32-bit, 2's-complement).

  2. No, in that case the range would be 0 to 4294967295.

  3. That code exhibits undefined behaviour.

  4. See 3.

  5. See 2. and 3.

姜生凉生 2024-11-13 05:50:17

仅考虑问题 3“为什么我们要费心使用 unsigned”,请考虑以下程序片段:

int main(void) {

  int num = MAX_INT;
  num += 50;

  printf("%u\n", num); /* Yes, this *might* print what you want */

  /* But this "if" almost certainly won't do what you want. */
  if(num > 0) {
    printf("Big numbers are big\n");
  } else {
    printf("Big numbers are small\n");
  }
} 

我们使用“unsigned”,因为 unsigned int 的行为与 int 不同。除了 printf 的工作原理之外,还有更多有趣的行为。

Considering only Q3, "Why do we bother to use unsigned", consider this program fragment:

int main(void) {

  int num = MAX_INT;
  num += 50;

  printf("%u\n", num); /* Yes, this *might* print what you want */

  /* But this "if" almost certainly won't do what you want. */
  if(num > 0) {
    printf("Big numbers are big\n");
  } else {
    printf("Big numbers are small\n");
  }
} 

We use "unsigned" because unsigned int behaves differently from int. There are more interesting behaviors than just how printf works.

柠檬心 2024-11-13 05:50:17

好吧,首先,分配超出范围的数字的结果是实现定义的 - 它不必提供使用 %u 格式说明符打印时“起作用”的值。但要真正回答您的问题,请考虑对您的代码进行以下修改:(

#include <stdio.h>

int main(void)
{
    int num = 2147483650;
    unsigned num2 = 2147483650;

    num /= 2;
    num2 /= 2;

    printf("%u, %u\n" , num, num2);

    return 0;
}

如果 2147483650 超出您平台上的 int 范围,但在 unsigned 范围内,那么您使用 unsigned 类型只能得到正确答案 1073741825)。

Well, firstly the result of assigning an out-of-range number is implementation-defined - it doesn't have to give the value that will "work" when printed with the %u format specifier. But to really answer your question, consider this modification to your code:

#include <stdio.h>

int main(void)
{
    int num = 2147483650;
    unsigned num2 = 2147483650;

    num /= 2;
    num2 /= 2;

    printf("%u, %u\n" , num, num2);

    return 0;
}

(If 2147483650 is out of range of int on your platform, but within the range of unsigned, then you will only get the correct answer of 1073741825 using the unsigned type).

独行侠 2024-11-13 05:50:17
  1. 错了。范围是 INT_MIN(在 2 个补码系统中通常是 -INT_MAX+1)到 INT_MAX; INT_MAX 和 INT_MIN 取决于编译器、体系结构等。

  2. 错误。范围是 0 到 UINT_MAX,通常是 INT_MAX*2 + 1

  3. 无符号整数在溢出和语义方面具有不同的行为。在 2 补码中,有一个未定义的有符号整数值(即,如果仅设置了最高位,其余为零),这在某种程度上具有双重含义。无符号整数可以使用全部位模式。

  4. 这是一个练习:在 32 位计算机上比较 printf("%d %u", 0xffffffff, 0xffffffff);

  5. 因为无符号整数的行为与有符号整数不同。

  1. Wrong. The range is INT_MIN (which in 2 completment systems usually is -INT_MAX+1) to INT_MAX; INT_MAX and INT_MIN depends on the compiler, architecture, etc.

  2. Wrong. The range is 0 to UINT_MAX which is usually INT_MAX*2 + 1

  3. Unsigned integers have a different behaviour regarding overflow and semantics. In 2 complement there's one value undefined for signed integers (that is if only the uppermost bit is set, the rest zero), that has somewhat a double meaning. Unsigned integers can make use of the full range of bit patterns.

  4. Here's an exercise: On a 32 bit machine compare the output of printf("%d %u", 0xffffffff, 0xffffffff);

  5. Because unsigned integers behave differently than signed ones.

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