Kanwaljeet Singh
Kanwaljeet Singh

Reputation: 1225

What is the difference between "short int" and "int" in C?

How is short int (or short) and int different in C? They have the same size and range. If they are essentially the same, what is the use of having two data types?

Upvotes: 28

Views: 117919

Answers (10)

It depends on the compiler. In some compilers int is 2 bytes and in other compilers is 4 bytes.

Upvotes: 0

Lundin
Lundin

Reputation: 213693

In theory/by the C standard, they could be of any size as long as 16 bit <= short <= int.

In the real world, this is how the sizes are implemented.

CPU             short   int
8 bit           16      16
16 bit          16      16
32 bit          16      32
64 bit          16      32

Upvotes: 33

Ajitesh Sinha
Ajitesh Sinha

Reputation: 1

short and int must be at least 16 bits, long must be at least 32 bits, and that short is no longer than int, which is no longer than long. Typically, short is 16 bits, long is 32 bits, and int is either 16 or 32 bits.

Upvotes: 0

Mateen Maldar
Mateen Maldar

Reputation: 11

I was working on the same today. My conclusion is it depends on the word length of the machine architecture on which your program is getting executed. As per C99 limits.h documentation.

/* Minimum and maximum values a `signed short int' can hold.  */
#  define SHRT_MIN  (-32768)
#  define SHRT_MAX  32767

/* Maximum value an `unsigned short int' can hold.  (Minimum is 0.)  */
#  define USHRT_MAX 65535

/* Minimum and maximum values a `signed int' can hold.  */
#  define INT_MIN   (-INT_MAX - 1)
#  define INT_MAX   2147483647

/* Maximum value an `unsigned int' can hold.  (Minimum is 0.)  */
#  define UINT_MAX  4294967295U

/* Minimum and maximum values a `signed long int' can hold.  */
#  if __WORDSIZE == 64
#   define LONG_MAX 9223372036854775807L
#  else
#   define LONG_MAX 2147483647L
#  endif
#  define LONG_MIN  (-LONG_MAX - 1L)

/* Maximum value an `unsigned long int' can hold.  (Minimum is 0.)  */
#  if __WORDSIZE == 64
#   define ULONG_MAX    18446744073709551615UL
#  else
#   define ULONG_MAX    4294967295UL
#  endif

Let me know if anyone has better answer.

Upvotes: 1

C99 N1256 standard draft

All that we now for sure is that:

2 <= sizeof(short) <= sizeof(int)

5.2.4.2.1 Sizes of integer types <limits.h> gives the minimum sizes:

1 [...] Their implementation-defined values shall be equal or greater in magnitude (absolute value) to those shown [...]

  • UCHAR_MAX 255 // 2 8 − 1
  • USHRT_MAX 65535 // 2 16 − 1
  • UINT_MAX 65535 // 2 16 − 1
  • ULONG_MAX 4294967295 // 2 32 − 1
  • ULLONG_MAX 18446744073709551615 // 2 64 − 1

6.2.5 Types then says:

8 For any two integer types with the same signedness and different integer conversion rank (see 6.3.1.1), the range of values of the type with smaller integer conversion rank is a subrange of the values of the other type.

and 6.3.1.1 Boolean, characters, and integers determines the relative conversion ranks:

1 Every integer type has an integer conversion rank defined as follows:

  • The rank of long long int shall be greater than the rank of long int, which shall be greater than the rank of int, which shall be greater than the rank of short int, which shall be greater than the rank of signed char.
  • The rank of any unsigned integer type shall equal the rank of the corresponding signed integer type, if any.
  • For all integer types T1, T2, and T3, if T1 has greater rank than T2 and T2 has greater rank than T3, then T1 has greater rank than T3

Upvotes: 1

Bharat Sharma
Bharat Sharma

Reputation: 3966

Actually everything depends on compiler and system both. But the basic rule says that int can never be less than short and can never be greater than long.

short <= int <= long

Upvotes: 3

YoyoS
YoyoS

Reputation: 669

It depends on the system. Some OSes won't have the same length for both types.

Upvotes: 1

Robbie Dee
Robbie Dee

Reputation: 1977

Never rely on a datatype being a given size in C. Always check the bounds in limits.h if in doubt.

Upvotes: 6

Ljdawson
Ljdawson

Reputation: 12229

"A short integer in one programming language may be a different size in a different language or on a different processor. In some languages this size is fixed across platforms, while in others it is machine-dependent. In some languages this datatype does not exist at all."

Source

Upvotes: 0

MByD
MByD

Reputation: 137312

They may have the same size, but it is guaranteed that int is equal to or bigger than short int.

Upvotes: 33

Related Questions