long int range in C programming

1.1k views Asked by At

As per the link https://www.geeksforgeeks.org/data-types-in-c/2

If we assume long int takes 8 bytes (64 bits) then its range should be -2^63 to 2^63-1 , but that is not given in the link above. Why is it so?

And similarly unsigned long int should have range between 0 to 2^64 .

Please tell what will be the range for float, double and long double as it's not mentioned in link.

4

There are 4 answers

0
RobertS supports Monica Cellio On

If we assume long int takes 8 bytes (64 bits) then its range should be -2^63 to 2^63-1, but that is not given in the link above. Why is it so?

Because GeeksForGeeks is a known source of incorrectness and falseness. Don't trust in them. Trust in official standards and established authors with decades of experience (as you can find here on Stack Overflow - me excluded :-)).

The Definitive C Book Guide and List

Under this list you can find books of higher quality.

Beside that long int has commonly on the most implementations the same size as an int, 4 byte.

But as you said it is an assumption, it is completely correct to ask for.

0
DevSolar On

if we assume long int takes 8 bytes(64 bits) then its range should be -2^63 to 2^63-1 , but that is not given in the link above. why is it so?

Because C is specified in a way that allows for fully conforming implementations to be created for platforms that do not follow either of your assumptions -- that long int takes 8 bytes, and that the value is encoded in two's complement.

The standard gives you <limits.h> which will contain the limits of long int on the given platform, LONG_MAX and LONG_MIN, and the only guarantee you are given is that either value will be no less in magnitude than 2147483647 (2^32-1), respectively.

It's a good idea to not make assumptions on platform specifics, and keeping your code portable that way.

0
0___________ On

I would rather not use geeksforgeeks as the source of knowledge. There are more "imprecise" information on this page

C standard defines minimum ranges of the integer types

— number of bits for smallest object that is not a bit-field (byte)
CHAR_BIT 8
— minimum value for an object of type signed char
SCHAR_MIN -127 // −(2 7 − 1)
— maximum value for an object of type signed char
SCHAR_MAX +127 // 2 7 − 1
— maximum value for an object of type unsigned char
UCHAR_MAX 255 // 2 8 − 1
— minimum value for an object of type char
CHAR_MIN see below
— maximum value for an object of type char
CHAR_MAX see below
— maximum number of bytes in a multibyte character, for any supported locale
MB_LEN_MAX 1
— minimum value for an object of type short int
SHRT_MIN -32767 // −(2 15 − 1)
— maximum value for an object of type short int
SHRT_MAX +32767 // 2 15 − 1
— maximum value for an object of type unsigned short int
USHRT_MAX 65535 // 2 16 − 1
— minimum value for an object of type int
INT_MIN -32767 // −(2 15 − 1)
— maximum value for an object of type int
INT_MAX +32767 // 2 15 − 1
— maximum value for an object of type unsigned int
UINT_MAX 65535 // 2 16 − 1
— minimum value for an object of type long int
LONG_MIN -2147483647 // −(2 31 − 1)
— maximum value for an object of type long int
LONG_MAX +2147483647 // 2 31 − 1
— maximum value for an object of type unsigned long int
ULONG_MAX 4294967295 // 2 32 − 1
22 Environment §5.2.4.2.1
WG14/N1256 Committee Draft — Septermber 7, 2007 ISO/IEC 9899:TC3
— minimum value for an object of type long long int
LLONG_MIN -9223372036854775807 // −(2 63 − 1)
— maximum value for an object of type long long int
LLONG_MAX +9223372036854775807 // 2 63 − 1
— maximum value for an object of type unsigned long long int
ULLONG_MAX 18446744073709551615 // 2 64 − 1

So the integer is not minimum 32 bits but 16 bits. The actual size can be checked in the limits.h defines (this comes from the C standard):

#define CHAR_BIT 8
#define CHAR_MAX UCHAR_MAX or SCHAR_MAX
#define CHAR_MIN 0 or SCHAR_MIN
#define INT_MAX +32767
#define INT_MIN -32767
#define LONG_MAX +2147483647
#define LONG_MIN -2147483647
#define LLONG_MAX +9223372036854775807
#define LLONG_MIN -9223372036854775807
#define MB_LEN_MAX 1
#define SCHAR_MAX +127
#define SCHAR_MIN -127
#define SHRT_MAX +32767
#define SHRT_MIN -32767
#define UCHAR_MAX 255
#define USHRT_MAX 65535
#define UINT_MAX 65535
#define ULONG_MAX 4294967295
#define ULLONG_MAX 18446744073709551615

C standard also sets the minimum range of the floating point types - but you need to check your .h file for the implementation values.

#define DBL_DIG 10
#define DBL_MANT_DIG
#define DBL_MAX_10_EXP +37
#define DBL_MAX_EXP
#define DBL_MIN_10_EXP -37
#define DBL_MIN_EXP
#define DECIMAL_DIG 10
#define FLT_DIG 6
#define FLT_MANT_DIG
#define FLT_MAX_10_EXP +37
#define FLT_MAX_EXP
#define FLT_MIN_10_EXP -37
#define FLT_MIN_EXP
#define FLT_RADIX 2
#define LDBL_DIG 10
#define LDBL_MANT_DIG
#define LDBL_MAX_10_EXP +37
#define LDBL_MAX_EXP
#define LDBL_MIN_10_EXP -37
#define LDBL_MIN_EXP

The values given in the following list shall be replaced by implementation-defined constant expressions with values that are greater than or equal to those shown:

#define DBL_MAX 1E+37
#define FLT_MAX 1E+37
#define LDBL_MAX 1E+37

The values given in the following list shall be replaced by implementation-defined constant expressions with (positive) values that are less than or equal to those shown:

#define DBL_EPSILON 1E-9
#define DBL_MIN 1E-37
#define FLT_EPSILON 1E-5
#define FLT_MIN 1E-37
#define LDBL_EPSILON 1E-9
#define LDBL_MIN 1E-37
2
fcdt On

There is not really a range that can be specified for float, double and long double, since these are floating point numbers. While the distance between integers is always 1, there are different distances between the individual numbers for floating point numbers (the distance increases with larger numbers), and thus also the smallest representable number in terms of amount:

  • float is a 32-bit value including a 23 mantissa, 8 bit exponent and 1 sign bit. The minimum value here is -3.40∙10³⁸ and the maximum 3.40∙10³⁸. The smallest representable value in amount is 1.18∙10⁻³⁸.
  • double is a 64-bit value including a 52 mantissa, 11 bit exponent and 1 sign bit. The minimum value here is 1.79∙10³⁰⁸ and the maximum 1.79∙10³⁰⁸. The smallest representable value in amount is 2.23∙10⁻³⁸.
  • long double is a 80-bit value including a 64 mantissa, 15 bit exponent and 1 sign bit. The minimum value here is -1.18∙10⁴⁹³² and the maximum 1.18∙10⁴⁹³². The smallest representable value in amount is 3.37∙10⁻⁴⁹³².

Addendum: The values also depend on the platform, the values above apply to the platform. There is no 80-bit floating point type under .


References: