47

Could you please make it clear what the difference is between unsigned and unsigned int? Maybe some example code would be helpful.

Lance Roberts
  • 22,383
  • 32
  • 112
  • 130
thetna
  • 6,903
  • 26
  • 79
  • 113
  • 2
    Try this code: `unsigned int a=-1; printf("see as signed=%d\nsee as unsigned=%u\n", a, a);` – Stan Aug 24 '11 at 14:12

5 Answers5

75

unsigned is a modifier which can apply to any integral type (char, short, int, long, etc.) but on its own it is identical to unsigned int.

Graham Borland
  • 60,055
  • 21
  • 138
  • 179
14

There is no difference. unsigned and unsigned int are both synonyms for the same type (the unsigned version of the int type).

Sander De Dycker
  • 16,053
  • 1
  • 35
  • 40
11

unsigned alone means unsigned int. You can also use unsigned char, etc. I have no idea what happens if you try unsigned double or unsigned float. Anybody know?

Daniel
  • 6,595
  • 9
  • 38
  • 70
  • 1
    The compiler will tell you that's an error. Both `double` and `float` are defined through IEEE-754 documentation. – Stan Aug 24 '11 at 14:09
  • 2
    @stan that makes sense. I was imagining the weird behavior that would occur if somehow `double` and `float` could be unsigned. Would it just ignore the sign bit? Maybe add a bit of precision to the significand or something? – Daniel Aug 24 '11 at 14:16
  • 2
    @Daniel: 6.2.5 in [the C99 Standard](http://www.open-std.org/JTC1/sc22/wg14/www/docs/n1256.pdf) is a good read. Basically it says there are floating types and integer types; and `unsigned` does not match any part of the specification of floating types :) – pmg Aug 24 '11 at 14:24
  • 3
    @Stan: C permits `float`, `double`, and `long double` to be implemented as specified by IEEE 754, but it doesn't require it. – Keith Thompson Oct 30 '13 at 04:12
4

unsigned indicates that it's unsigned int. So they are equivalent.

Mu Qiao
  • 6,941
  • 1
  • 31
  • 34
3

They are exactly the same thing.

Heisenbug
  • 38,762
  • 28
  • 132
  • 190