简体   繁体   English

例2.1,K&R:LONG_MIN和LONG_MAX的符号常量值错误吗?

[英]Example 2.1, K&R: Wrong symbolic constants value for LONG_MIN and LONG_MAX?

This is the code which I use to find the symbolic constant values for LONG 这是我用来查找LONG的符号常量值的代码

   #include <limits.h>     //These header files contains the symbolic constants
   #include <float.h>      //for different datatypes

   #include <stdio.h>

    int main(void){

            printf("\tMininum numeric value for long type: %ld\n", LONG_MIN);
            printf("\tMaximum numeric value for long type: %ld\n", LONG_MAX);

            printf("\tMaximum numeric value for unsigned long type: %lu\n", (unsigned)ULONG_MAX);
    return 0;
   }  

Output which I get is: 我得到的输出是:

  Mininum numeric value for long type: -9223372036854775808
  Maximum numeric value for long type: 9223372036854775807
  Maximum numeric value for unsigned long type: 4294967295

But if see the limits.h from the man page. 但是如果从手册页中看到limits.h。 These are the symbolic constants for LONG_MIN , LONG_MAX , LLONG_MIN and LLONG_MAX from the limits.h file in my system. 这些是系统中limits.h文件中LONG_MINLONG_MAXLLONG_MINLLONG_MAX的符号常量。

  {LONG_MIN}
          Minimum value of type long.
          Maximum Acceptable Value: -2 147 483 647

  {LONG_MAX}
          Maximum value of a long.
          Minimum Acceptable Value: +2 147 483 647

  {LLONG_MIN}
          Minimum value of type long long.
          Maximum Acceptable Value: -9223372036854775807

  {LLONG_MAX}
          Maximum value of type long long.
          Minimum Acceptable Value: +9223372036854775807

As if, the program is giving me the values from Long long symbolic constants. 好像程序正在给我Long long符号常量中的值。

Why is this happening? 为什么会这样呢?

The size of types are variable. 类型的大小是可变的。 A long is at least 32 bits (which gives the +-2 billion range), but it's allowed to be larger. long 至少为 32位(给出+ -2十亿范围),但允许更大。 On a 64 bit system it might, for example, be 64 bits. 在64位系统上,它可能是例如64位。

A long long must be at least 64 bits, but may of course be larger. long long必须至少为64位,但是当然可以更大。

These sizes depend on the underlying hardware platform and the compiler. 这些大小取决于基础硬件平台编译器。 For example on Windows 64-bit system using GCC then long is 64 bits but using the Visual C++ compiler long is "only" 32 bits. 例如,在使用GCC的Windows 64位系统上, long是64位,而在使用Visual C ++编译器时, long是“仅” 32位。

Read more about integer types in this reference . 在此参考资料中了解有关整数类型的更多信息。

Obviously long is 64 bits on your system. 系统上的64位显然很long

The C standard specifies minimum limits for integer types. C标准为整数类型指定了最小限制 Meaning that for example LONG_MAX has to be at least 2 147 483 647 , but it could be larger. 这意味着例如LONG_MAX必须是至少 2 147 483 647 ,但也可能是较大的。

As a side note, this is a bug: (unsigned)ULONG_MAX . 作为附带说明,这是一个错误: (unsigned)ULONG_MAX Should be unsigned long. 应为无符号长。


One interesting curiousity: note for example that LONG_MIN has to be at least -2 147 483 647 . 一种有趣的好奇心:例如,请注意LONG_MIN必须至少为-2 147 483 647 But even on a regular 32 bit two complement system, it never has that value! 但是,即使在常规的32位2补码系统上,它也没有那样的价值!
It has the value -2 147 483 648 . 其值为-2 147 483 648 This is done intentionally to allow one's complement and sign & magnitude systems. 这样做是有意的,以允许一个人的补全,符号和大小系统。

It's what it says. 就是这样

 Minimum Acceptable Value: +2 147 483 647 

The "minimum acceptable value" for LONG_MAX is that. LONG_MAX的“最小可接受值”是该值。 Your value for LONG_MAX (because your system is 64-bit) is higher. LONG_MAX值(因为您的系统是64位)较高。

So there's no problem. 因此没有问题。

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

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