What are the max and min numbers a short type can store in C?

59,683

Solution 1

Think in decimal for a second. If you have only 2 digits for a number, that means you can store from 00 to 99 in them. If you have 4 digits, that range becomes 0000 to 9999.

A binary number is similar to decimal, except the digits can be only 0 and 1, instead of 0, 1, 2, 3, ..., 9.

If you have a number like this:

01011101

This is:

0*128 + 1*64 + 0*32 + 1*16 + 1*8 + 1*4 + 0*2 + 1*1 = 93

So as you can see, you can store bigger values than 9 in one byte. In an unsigned 8-bit number, you can actually store values from 00000000 to 11111111, which is 255 in decimal.

In a 2-byte number, this range becomes from 00000000 00000000 to 11111111 11111111 which happens to be 65535.

Your statement "it takes 8 bits to store the binary representation of a number" is like saying "it takes 8 digits to store the decimal representation of a number", which is not correct. For example the number 12345678901234567890 has more than 8 digits. In the same way, you cannot fit all numbers in 8 bits, but only 256 of them. That's why you get 2-byte (short), 4-byte (int) and 8-byte (long long) numbers. In truth, if you need even higher range of numbers, you would need to use a library.

As long as negative numbers are concerned, in a 2's-complement computer, they are just a convention to use the higher half of the range as negative values. This means the numbers that have a 1 on the left side are considered negative.

Nevertheless, these numbers are congruent modulo 256 (modulo 2^n if n bits) to their positive value as the number really suggests. For example the number 11111111 is 255 if unsigned, and -1 if signed which are congruent modulo 256.

Solution 2

The reference you read is correct. At least, for the usual C implementations where short is 16 bits - that's not actually fixed in the standard.

16 bits can hold 2^16 possible bit patterns, that's 65536 possibilities. Signed shorts are -32768 to 32767, unsigned shorts are 0 to 65535.

Solution 3

This is defined in <limits.h>, and is SHRT_MIN & SHRT_MAX.

Solution 4

The formula to find the range of any unsigned binary represented number:

2 ^ (sizeof(type)*8)
Share:
59,683
Mohamad
Author by

Mohamad

I love well designed digital products, programming, and tech. Driving digital product roadmap in Brazil for world’s third largest retailer of home decor and building materials.

Updated on November 26, 2020

Comments

  • Mohamad
    Mohamad over 3 years

    I'm having a hard time grasping data types in C. I'm going through a C book and one of the challenges asks what the maximum and minimum number a short can store.

    Using sizeof(short); I can see that a short consumes 2 bytes. That means it's 16 bits, which means two numbers since it takes 8 bits to store the binary representation of a number. For example, 9 would be 00111001 which fills up one bit. So would it not be 0 to 99 for unsigned, and -9 to 9 signed?

    I know I'm wrong, but I'm not sure why. It says here the maximum is (-)32,767 for signed, and 65,535 for unsigned.

    short int, 2 Bytes, 16 Bits, -32,768 -> +32,767 Range (16kb)

  • Mohamad
    Mohamad almost 12 years
    But it takes 8 bits to store a number, right? The 16 bits can store 2 numbers? I'm missing something here!
  • kay
    kay almost 12 years
    @Mohamad one bit is enough to store a number in range [0;1]. Two bits can store 4 numbers. And so on …
  • Linuxios
    Linuxios almost 12 years
    @Mohamad: No. 8 bits only stores numbers in the range 0-255. 16 bits stores numbers in the range 0-25535. 32-bit (usually int) can store somewhere in the 4 billions. You get the idea. It's all powers of two.
  • kay
    kay almost 12 years
    << is the shift left operator. If you shift a 1 bit n bits left, you get 2 to the power of n. ^ means bitwise xor in C.
  • Mohamad
    Mohamad almost 12 years
    Ok! Thank you. I got it. I have to think in "base-2" numbers instead of base-10. Makes sense now. My understand was way off!
  • Shahbaz
    Shahbaz almost 12 years
    The OP clearly has problems with understanding binary numbers. How is this going to help him understand?
  • Shahbaz
    Shahbaz almost 12 years
    @Mohamad, exactly. It's fairly similar to base-10. Whenever you get confused, you can use this analogy to understand better ;)
  • Linuxios
    Linuxios almost 12 years
    @Kay: Thanks. I didn't know a power of two could be just a left shift. Comment +1?
  • Linuxios
    Linuxios almost 12 years
    @Shahbaz: It's not going to make him understand, but it does give him some simple decimal numbers. <limits.h> can also be a very valuable tool when writing portable programs, and who knows if that was the question at the heart of the issue? This question has just the right balance of theory and practicality in it's answers.
  • 2501
    2501 about 8 years
    This is not correct. Integers in C may have padding.
  • Linuxios
    Linuxios about 8 years
    @2501: Not correct for a theoretical computer, or not correct for the C standard?
  • 2501
    2501 about 8 years
    C Standard permits padding bits in integers. You don't need a theoretical computer to show this, it happens on modern architectures for at least one type.