Question

What is the difference between signed and normal short in c++? Is the range is different?

Was it helpful?

Solution

short is signed by default, so there is no difference.

OTHER TIPS

The names signed short int, signed short, short int and short are synonymes and mean same type in C++.

Integers are signed by default in C++, which IMO brings the existence of the signed keyword into question. Technically, it is redundant, maybe it does contribute with some clarity, but hardly anyone uses it in production. Everyone is pretty much aware integers are signed by default. I honestly can't remember the last time I've seen signed in production code.

As for floats and doubles - they cannot be unsigned at all, they are always signed.

In this regard C++ syntax is a little redundant, at least IMO. There is a number of different ways to say the same thing, e.g. signed short int, signed short, short int and short , and what you say still might be platform or even compiler dependent.

Frameworks like Qt for example declare their own conventions which are shorter and informative, like for example:

quint8, quint16, quint32, quint64 are all unsigned integers, with the number signifying the size in bits, in the same logic:

qint8, qint16, qint32, qint64 are signed integers with the respective bit width.

uint is, at least for me, much more preferable to either unsigned or unsigned int, in the same logic you also have ushort which is preferable to unsigned short int. There is also uchar to complete the short-hard family.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top