0

In C, is there a difference between unsigned as a type directly and unsigned int?

Update: This is a duplicate of this question, but the other question didn't answer about small subleties that can exist. Thanks @EOF for pointing out the implementation defined behavior in bitfields.

Community
  • 1
  • 1
FSMaxB
  • 2,280
  • 3
  • 22
  • 41
  • 1
    No, the types are the same. – EOF Feb 03 '17 at 12:14
  • So no small subleties there? – FSMaxB Feb 03 '17 at 12:15
  • No, it's like `short` vs `short int` or `long` vs `long int`: `int` is optional when qualified. – rom1v Feb 03 '17 at 12:16
  • 2
    @FSMaxB See C11 draft standard n1570: *5 Each of the comma-separated multisets designates the same type, except that for bit- fields, it is implementation-defined whether the specifier int designates the same type as signed int or the same type as unsigned int.* So the only subtlety is about bitfields. – EOF Feb 03 '17 at 12:17
  • @EOF Exactly these kinds of subleties is what I wanted to know. Although I hope that no compiler actually implements these differently in bitfields. – FSMaxB Feb 03 '17 at 12:21
  • 1
    @FSMaxB: Compilers *absolutely* use different signedness for bitfields of declared type `int`. It's one of the canonical examples of implementation-defined behavior encountered in the real world. For example, an `int i:1` in a struct can be initialized as `.i = 1;` and consequently compare *unequal* to one: `.i != 1` is true. It can also compare *equal* when the program is compiled with a different compiler or for a different target. – EOF Feb 03 '17 at 12:24

1 Answers1

0

No, unsigned and unsigned int both refer to the same type. See cppreference/fundamental types:

cppreference screenshot

Vittorio Romeo
  • 90,666
  • 33
  • 258
  • 416
  • 1
    Did you create this list, or where does it come from? As it stand this is the default to assume. Did you make your it isn't copyrighted before posting it here to the world? – alk Feb 03 '17 at 12:27
  • @alk: I thought that *"See cppreference/fundamental_types:"* was enough to understand that the list was coming from cppreference/fundamental types. The caption also says "cppreference screenshot". – Vittorio Romeo Feb 03 '17 at 12:30
  • 1
    For licensing, see [cppreference/FAQ](http://en.cppreference.com/w/Cppreference:FAQ) – Vittorio Romeo Feb 03 '17 at 12:31
  • "*For licensing*" Thank you. This is good to know. – alk Feb 03 '17 at 12:33