Range of Int in C Programming Language

Range of Int in C


ntegral Types

 

  • Integral data types include all whole numbers, that is numbers not having any fractional component.
  • The bits of integral types are interpreted as simple powers of two:
    • The right-most bit, known as the least significant bit, represents the number of 1s. ( 2^0 )
    • The next bit represents the number of 2s. ( 2^1 )
    • The next bit represents the number of 4s. ( 2^2 )
    • The next bit represents the number of 8s. ( 2^3 )
    • In general the nth bit from the right represents 2^(n-1)
  • For unsigned integral types, the leftmost bit, known as the most significant bit, represents 2^(N-1), where N is the total number of bits in the data item.
    • The range of possible values for an unsigned integer of N bits is from 0 to 2^N - 1. ( All 0s to all 1s )
    • So for example, a 4-bit unsigned integer could range from 0 to 15, and an 8-bit unsigned integer could range from 0 to 255.
  • For signed integral types, the leftmost bit can be thought of as representing a negative 2^(N-1).
    • ( The real interpretation in the computer is more complicated, but if you think of it this way you will get the right answers. )
    • The most negative value would be the first bit a 1 and all other bits 0s, yielding negative 2^(N-1).
    • The most positive value would be the first bit a 0 and all other bits 1s, yielding 2^(N-1) - 1.
    • So for example, a 4-bit signed integer could range from -8 to +7, and an 8-bit signed integer could range from -128 to +127.
    • A signed integral type having all bits 1 is equal to -1, regardless of how many bits are in the number.
  • Signed and unsigned integers with the same number of total bits have the same number of different possible values.
    • Unsigned integers use one bit pattern ( all 0s ) to represent zero and all others to represent positive values.
    • Signed integers use half of the possible bit patterns to represent negative numbers, one pattern to represent zero, and half minus 1 to represent positive values.
  • Specific details of the integer types available on a particular implementation, along with the number of bits allocated to each one and their minimum and maximum allowable values can be found in the file limits.h

int

  • The most basic and commonly used integral type is "int".
  • The int data type is always the "best" size for the particular computer it is running on, typically 32 bits
  • Format specifiers for ints are either %d or %i, for either printf or scanf.

long int

  • A long int typically uses twice as many bits as a regular int, allowing it to hold much larger numbers.
    • ( The C standard only specifies that a long cannot use a fewer number of bits than a regular int )
  • printf and scanf replace %d or %i with %ld or %li to indicate the use of a long int.
  • long int may also be specified as just long.

long long int

  • C99 introduces the long long int, typically having twice as many bits as a long int and printed/scanned using %lld or %lli format specifiers

short int

  • A short int may use fewer bits than a regular int, thereby saving