How many bytes are in an int or a long? Most think int type sizes vary with platform between 2 or 4 bytes while the long integer is 4 bytes regardless. Though that can happen, that’s not how the C standard defines int type sizes.

The C standard allows the int types to vary as everyone expects, but C only defines a minimum size. The short integer must have at least 16 bits. Type int must also have at least 16 bits and be at least as large as the short integer. The long has a minimum of 32 bits. C99 introduced the long long type which has a minimum of 64 bits. For each type, the C Standard defines no upper boundary. Here’s a quote from the C99 draft standard:

The values given below shall be replaced by constant expressions suitable for use in #if preprocessing directives. [...] Their implementation-defined values shall be equal or greater in magnitude (absolute value) to those shown, with the same sign.

The previous passage is talking specifically about the definitions in limits.h, which, in the standard, have the following definitions:

`minimum value for type short int: SHRT_MIN -32767 // −(15`^{2}−1)

maximum value for type short int: SHRT_MAX +32767 // 15^{2}−1

maximum value for type unsigned short int: USHRT_MAX 65535// 16^{2}−1

minimum value for type int: INT_MIN -32767 // −(15^{2}−1)

maximum value for type int: INT_MAX +32767 // 15−1

maximum value for type unsigned int: UINT_MAX 65535 // 16^{2}−1

minimum value for type long int: LONG_MIN -2147483647 // −(31^{2}−1)

maximum value for type long int: LONG_MAX +2147483647 // 31^{2}−1

maximum value for type unsigned long int: ULONG_MAX 4294967295 // 32^{2}−1

minimum value for type long long int: LLONG_MIN -9223372036854775807 // −(63^{2}−1)

maximum value for type long long int: LLONG_MAX +9223372036854775807 // 63^{2}−1

maximum value for type unsigned long long int: ULLONG_MAX 18446744073709551615 // 64^{2}−1

The key point is the C standard defines minimum values and your platform may have different values than what the standard states, but if so, the values must be larger. The safe way to determine the minimum and maximum values for all C types on your platforms is to use limits.h, e.g.:

`#include <limits.h>`

[...]

printf("Minimum value for a signed int: %d\n", INT_MIN);

printf("Maximum value for a signed int: %d\n", INT_MAX);

In C++, rely on limits rather than climits. In C++, limits defines a template class, numeric_limits, specialized for each of the fundamental types (e.g. int, short, float, double, etc.). To determine the range of possible values in C++ using numeric_limits, for an int, you would do the following:

`#include <limits>`

#include <iostream>

using namespace std;

[...]

cout << "Minimum value for a signed int: " << numeric_limits<int>::min() << endl;

cout << "Maximum value for a signed int: " << numeric_limits<int>::max() << endl;

Good references on the topic are Harbison and Steele’s C A Reference Manual and Steve Summit’s C FAQs.