Set
Set

Reputation: 57

Is `short` the same as `int` in C++?

I've looked at some answers that use short in C#, but I'm not sure if they really answer my question here. Is short in C++ another name for int? I know you can make short int, which seems to be able to handle a lot of values but I'm still starting out, so obviously if it's short it's not a lot of values. But in this code snippet here:

short lives,aliensKilled;

it doesn't use int short, it just uses short. So I guess my question is, can I just use short as a replacement for int if I'm not going under -32,768 or over 32,767?

Also, is it okay to just replace short with int, and it won't really mess with anything as long as I change the appropriate things? (Btw lives and aliensKilled are both variable names.)

Upvotes: 0

Views: 8800

Answers (7)

Potatoswatter
Potatoswatter

Reputation: 137790

C++ (and C# and Objective-C and other direct descendants of C) have a quirky way of naming and specifying the primitive integral types.

As specified by C++, short and int are simple-type-specifiers, which can be mixed and matched along with the keywords long, signed, and unsigned in any of a page-full of combinations.

The general pattern for the single type short int is [signed] short [int], which is to say the signed and int keywords are optional.

Note that even if int and short are the same size on a particular platform, they are still different types. int has at least the same range as short so it's numerically a drop-in replacement, but you can't use an int * or int & where a short * or short & is required. Besides that C++ provides all kinds of machinery for working with types… for a large program written around short, converting to int may take some work.

Note also that there is no advantage to declaring something short unless you really have a reason to save a few bytes. It is poor style and leads to overflow errors, and can even reduce performance as CPUs today aren't optimized for 16-bit operations. And as Dietrich notes, according to the crazy way C arithmetic semantics are specified, the short is upcast to int before any operation is performed and then if the result is assigned back to a short, it's cast back again. This dance usually has no effect but can still lead to compiler warnings and worse.

In any case, the best practice is to typedef your own types for whatever jobs you need done. Always use int by default, and leverage int16_t, uint32_t from <stdint.h> (<cstdint> since C++11), etc instead of relying on platform-dependent short and long.

Upvotes: 1

user2302436
user2302436

Reputation: 518

Yes, short is equivalent to short int, and it uses at least 2 bytes, but if you stay in the range you can replace int with short without any problem.

Upvotes: 0

Vlad Tarniceru
Vlad Tarniceru

Reputation: 741

Also notice that for the following code:

short a = 32767;
a++;
cout << a;

It will print -32768.

So, if you go over its limit, it will "go back" with the counting.

Upvotes: -2

Amit
Amit

Reputation: 697

short can at max be two bytes long. On machines where int is two bytes, short and int have same range i.e. -32767 to +32767. For most of the new platforms, int is 4 bytes, catering to much larger range of values.

I recommend to go for explicit declaration such as int16_t for short and int32_t for int to avoid any confusion.

Upvotes: -2

Dietrich Epp
Dietrich Epp

Reputation: 213318

In C++ (and C), short, short int, and int short are different names for the same type. This type is guaranteed to have a range of at least -32,767..+32,767. (No, that's not a typo.)

On most modern systems, short is 16 bits and int is 32 bits. You can replace int with short without ill effects as long as you don't exceed the range of a short. On most modern systems, exceeding the range of a short will usually result in the values wrapping around—this behavior is not guaranteed by the standard and you should not rely on it, especially now that common C++ compilers will prune code paths that contain signed integer overflow.

However, in most situations, there is little benefit to replacing int with short. I would only replace int with short if I had at least thousands of them. There's not always a benefit, by using short you can reduce the memory used and the bandwidth required, but you can potentially increase the number of CPU cycles required to convert from short to int (a short is always "promoted" to int when you do arithmetic on it).

Upvotes: 6

Mats Petersson
Mats Petersson

Reputation: 129344

short int, int short and short are all synonymous in C and C++.

These work like int, but the range is smaller (typically, but not always) 16 bit. As long as none of the code relies on the transitions when the number "wraps around" due to it being 16 bits (that is, no calculation goes above the highest value (SHORT_MAX) or below the lowest value (SHORT_MIN)), using a larger type (int, long) will work just fine.

Upvotes: 2

Zs.Zs.
Zs.Zs.

Reputation: 63

Yes, you can use it. short = short int. Signed -32768 to 32767 and unsigned 0 to 65535 .

Upvotes: -1

Related Questions