tags:

views:

139

answers:

7

I was referring a tutorial on c,I found that signed int & short signed int range are -32768 to 32767 and it's of 2 bytes, is their any difference, if not then why two kinds of declarations used.

+1  A: 

A signed int is at least as large as a short signed int. On most modern hardware a short int is 2 bytes (as you saw), and a regular int is 4 bytes. Older architectures generally had a 2-byte int which may have been the cause of your confusion.

There is also a long int which is usually either 4 or 8 bytes, depending on the compiler.

Gabe
jerry
+8  A: 

It's platform specific - all that you can be sure of in this context is that sizeof(int) >= sizeof(short) >= 16 bits.

Paul R
@Paul It is platform specific. But, as per the standard, there are lower limits that a conforming implementation should enforce. (Also see my answer below).
Vijay Mathew
@Vijay: if you re-read carefully what I wrote above you should see that it covers these lower limits.
Paul R
No, as far as C is concerned, you can never be sure `sizeof(short)>=2`. `char` may be 32 or even 64 bits and then all types would have size 1 (except maybe `long long`). You **do** however have a guarantee that `char` is at least 8 bits, `short` and `int` are at least 16 bits, `long` is at least 32 bits, and `long long` is at least 64 bits. The natural `<=` relations apply between successively "larger" types too.
R..
@R..: good point - I was implying *"2 8 bit bytes"* when I wrote "2", but I should have been more careful - I'll edit the answer to fix this.
Paul R
Cool, reverted the -1.
R..
A: 

It depends on the platform.

Int is 32-bit wide on a 32-bit system and 64 bit wide on a 64-bit system(i am sure that this is ever the case).

Quonux
x86-64 is commonly 32-bit int on 64-bit hardware, IA64 is 64-bit int, there are a few different models:http://www.unix.org/version2/whatsnew/lp64_wp.html
Steve-o
It depends on the C implementation. In an 'LP64' implementation, ints are 32 bit, long ints are 64 bit and pointers are 64bit. In an ILP64 implementation ints are also 64 bit.
JeremyP
+2  A: 

C and C++ only make minimum size guarantees on their objects. There is no exact size guarantee that is made. You cannot rely on type short being exactly 2 bytes, only that it can hold values in the specified range (so it is at least two bytes). Type int is at least as large as short and is often larger. Note that signed int is a long-winded way to say int while signed short int is a long-winded way to say short int which is a long-winded way to say short. With the exception of type char (which some compilers will make unsigned), all the builtin integral types are signed by default. The types short int and long int are longer ways to say short and long, respectively.

Michael Aaron Safyan
+8  A: 

The best answer to your question can be found in the ANSI standard for C, section 2.2.4.2 - Numerical Limits. I reproduce the relevant parts of that section here for your convenience:

2.2.4.2 Numerical limits

A conforming implementation shall document all the limits specified in this section, which shall be specified in the headers and .

"Sizes of integral types "

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.

  • maximum 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

  • maximum value for an object of type signed char SCHAR_MAX
    +127

  • maximum value for an object of type unsigned char UCHAR_MAX
    255

  • 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

  • maximum value for an object of type short int SHRT_MAX
    +32767

  • maximum value for an object of type unsigned short int USHRT_MAX
    65535

  • minimum value for an object of type int INT_MIN
    -32767

  • maximum value for an object of type int INT_MAX
    +32767

  • maximum value for an object of type unsigned int UINT_MAX
    65535

  • minimum value for an object of type long int LONG_MIN
    -2147483647

  • maximum value for an object of type long int LONG_MAX
    +2147483647

  • maximum value for an object of type unsigned long int ULONG_MAX
    4294967295

The not so widely implemented C99 adds the following numeric types:

  • minimum value for an object of type long long int LLONG_MIN -9223372036854775807 // -(263 - 1)
  • maximum value for an object of type long long int LLONG_MAX +9223372036854775807 // 263 - 1
  • maximum value for an object of type unsigned long long int ULLONG_MAX 18446744073709551615 // 264 - 1
Vijay Mathew
You should cite the updated C99 standard with long long.
R..
A: 

I was referring a tutorial on c,I found that signed int & short signed int range are -32768 to 32767 and it's of 2 bytes.

That's a very old tutorial. The modern C standard is as per Paul R's answer. On a 32 bit architecture, normally:

    short int is 16 bits
          int is 32 bits
     long int is 32 bits
long long int is 64 bits

the size of an int would normally only be 16 bits on a 16 bit machine. 16 bit machines are presumably limited to embedded devices these days.

On a 16 bit machine, sizes amay be like this:

    short int is 16 bits
          int is 16 bits
     long int is 32 bits
long long int is 64 bits
JeremyP
+1  A: 

A couple of other answers have correctly quoted the C standard, which places minimum ranges on the types. However, as you can see, those minimum ranges are identical for short int and int - so the question remains: Why are short int and int distinct? When should I choose one over the other?

The reason that int is provided is to provide a type that is intended to match the "most efficient" integer type on the hardware in question (that still meets the minimum required range). int is what you should use in C as your general purpose small integer type - it should be your default choice.

If you know that you'll need more range than -32767 to 32767, you should instead choose long int or long long int. If you are storing a large number of small integers, such that space efficiency is more important than calculation efficiency, then you can instead choose short (or even signed char, if you know that your values will fit into the -127 to 127 range).

caf