105

Does the unsigned keyword default to a specific data type in C++? I am trying to write a function for a class for the prototype:

unsigned Rotate(unsigned object, int count)

But I don't really get what unsigned means. Shouldn't it be like unsigned int or something?

huu
  • 7,032
  • 2
  • 34
  • 49
Crystal
  • 28,460
  • 62
  • 219
  • 393

5 Answers5

109

From the link above:

Several of these types can be modified using the keywords signed, unsigned, short, and long. When one of these type modifiers is used by itself, a data type of int is assumed

This means that you can assume the author is using ints.

tacaswell
  • 84,579
  • 22
  • 210
  • 199
futureelite7
  • 11,462
  • 10
  • 53
  • 87
  • 1
    Is it C99 and MISRA allowed to do it so? – nowox Feb 17 '17 at 14:06
  • The link above does not contain anymore quoted text, maybe after revision. New quote should be: "The keyword int may be omitted if any of the modifiers listed below are used.... Modifiers... unsigned - on the list" Please update your answer. – Dmitry Babich Dec 05 '22 at 05:43
51

Integer Types:

short            -> signed short
signed short
unsigned short
int              -> signed int
signed int
unsigned int
signed           -> signed int
unsigned         -> unsigned int
long             -> signed long
signed long
unsigned long

Be careful of char:

char  (is signed or unsigned depending on the implmentation)
signed char
unsigned char
Martin York
  • 257,169
  • 86
  • 333
  • 562
  • 1
    @Firas : _I know it is in C_ .Implicit int is neither a part of ISO C99 nor a part of ISO C++ – Prasoon Saurav Jan 20 '10 at 08:26
  • 5
    Using 'unsigned' (by itself) to declare the 'unsigned int' type is standard in C++. In the current standard: 7.1.5.2 [dcl.simple.type] has a table that identifies each type declaration to the actual type used. That table has a correspondence from 'unsigned' declaration to 'unsigned int' type. – David Rodríguez - dribeas Jan 20 '10 at 08:43
  • 2
    Note that char has identical *behavior* to either signed char or unsigned char, but remains a separate type no matter what. –  Jan 20 '10 at 09:10
  • 1
    @Firas: See Section: 3.9.1 Fundamental types. Paragrah 2: There are five standard signed integer types : “signed char”, “short int”, “int”, “long int”, and “long long int”. – Martin York Jan 20 '10 at 14:24
25

Does the unsigned keyword default to a data type in C++

Yes,signed and unsigned may also be used as standalone type specifiers

The integer data types char, short, long and int can be either signed or unsigned depending on the range of numbers needed to be represented. Signed types can represent both positive and negative values, whereas unsigned types can only represent positive values (and zero).

An unsigned integer containing n bits can have a value between 0 and 2n - 1 (which is 2n different values).

However,signed and unsigned may also be used as standalone type specifiers, meaning the same as signed int and unsigned int respectively. The following two declarations are equivalent:

unsigned NextYear;
unsigned int NextYear;
Prasoon Saurav
  • 91,295
  • 49
  • 239
  • 345
  • 3
    Isn't that a "yes", then? It defaults to int, if used without a type? – unwind Jan 20 '10 at 10:19
  • 1
    Thank you! English is not my first language and I was baffled with the meaning of the unsigned data type. Whose signature does it contain? It never occurred to me that it could be from the noun sign... :D – user74200 Nov 08 '17 at 00:37
  • The keywords `short` and `long` are also *type modifiers*. If you write `short s`, that actually means `short int s`, just like `unsigned u` actually means `unsigned int u`. (and `unsigned short` means `unsigned short int`) – Ben Voigt May 10 '18 at 15:49
12

You can read about the keyword unsigned in the C++ Reference.

There are two different types in this matter, signed and un-signed. The default for integers is signed which means that they can have negative values.

On a 32-bit system an integer is 32 Bit which means it can contain a value of ~4 billion.

And when it is signed, this means you need to split it, leaving -2 billion to +2 billion.

When it is unsigned however the value cannot contain any negative numbers, so for integers this would mean 0 to +4 billion.

There is a bit more informationa bout this on Wikipedia.

Rogue
  • 11,105
  • 5
  • 45
  • 71
Filip Ekberg
  • 36,033
  • 20
  • 126
  • 183
3

Yes, it means unsigned int. It used to be that if you didn't specify a data type in C there were many places where it just assumed int. This was try, for example, of function return types.

This wart has mostly been eradicated, but you are encountering its last vestiges here. IMHO, the code should be fixed to say unsigned int to avoid just the sort of confusion you are experiencing.

Omnifarious
  • 54,333
  • 19
  • 131
  • 194