简体   繁体   中英

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

   #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?

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 .

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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