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

不想你离开。 提交于 2019-12-12 05:01:55

问题


This is the code which I use to find the symbolic constant values for 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. These are the symbolic constants for LONG_MIN, LONG_MAX, LLONG_MIN and LLONG_MAX from the limits.h file in my system.

  {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.

Why is this happening?


回答1:


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. On a 64 bit system it might, for example, be 64 bits.

A long long must be at least 64 bits, but may of course be larger.

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.

Read more about integer types in this reference.




回答2:


Obviously long is 64 bits on your system.

The C standard specifies minimum limits for integer types. Meaning that for example LONG_MAX has to be at least 2 147 483 647, but it could be larger.

As a side note, this is a bug: (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. But even on a regular 32 bit two complement system, it never has that value!
It has the value -2 147 483 648. This is done intentionally to allow one's complement and sign & magnitude systems.




回答3:


It's what it says.

Minimum Acceptable Value: +2 147 483 647

The "minimum acceptable value" for LONG_MAX is that. Your value for LONG_MAX (because your system is 64-bit) is higher.

So there's no problem.



来源:https://stackoverflow.com/questions/37965521/example-2-1-kr-wrong-symbolic-constants-value-for-long-min-and-long-max

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!