110

I've seen this unsigned "typeless" type used a couple of times, but never seen an explanation for it. I suppose there's a corresponding signed type. Here's an example:

static unsigned long next = 1;
/* RAND_MAX assumed to be 32767 */
int myrand(void) {
    next = next * 1103515245 + 12345;
    return((unsigned)(next/65536) % 32768);
}
void mysrand(unsigned seed) {
    next = seed;
}

What I have gathered so far:
- on my system, sizeof(unsigned) = 4 (hints at a 32-bit unsigned int)
- it might be used as a shorthand for casting another type to the unsigned version:

signed long int i = -42;
printf("%u\n", (unsigned)i);

Is this ANSI C, or just a compiler extension?

hhaamu
  • 6,851
  • 3
  • 19
  • 13

7 Answers7

171

unsigned really is a shorthand for unsigned int, and so defined in standard C.

Martin v. Löwis
  • 124,830
  • 17
  • 198
  • 235
44

unsigned means unsigned int. signed means signed int. Using just unsigned is a lazy way of declaring an unsigned int in C. Yes this is ANSI.

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
DigitalZebra
  • 39,494
  • 39
  • 114
  • 146
20

Historically in C, if you omitted a datatype "int" was assumed. So "unsigned" is a shorthand for "unsigned int". This has been considered bad practice for a long time, but there is still a fair amount of code out there that uses it.

  • 6
    I wasn't aware that it was bad practice. Is there a rationale for this? `long` instead of `long int` is very common so I'm not sure why `unsigned` instead of `unsigned int` wouldn't be acceptable. – CB Bailey Jul 23 '09 at 19:41
  • 10
    @Charles Bailey: these days - at least if you are being pragmatic rather than formal - long, int, short and char are considered to be different data types as they can be different sizes) with unsigned (and the default, signed) being a qualifier. Thus you would tend to use "unsigned int" in the same way you would use "unsigned long" or "unsigned char" (and it makes it clear that you didn't just miss off the int). The int in "long int" or "short int" is superfluous. – Dipstick Jul 25 '09 at 21:02
  • 1
    @Dipstick, "long" is a qualifier for "int". Seems like the int in "unsigned int" is just as superfluous as the int in "unsigned long int." – Conrad Meyer Dec 12 '14 at 00:16
  • 1
    This is not the same as the implicit "int" rule. This answer is misleading. –  Jan 19 '15 at 23:43
12

in C, unsigned is a shortcut for unsigned int.

You have the same for long that is a shortcut for long int

And it is also possible to declare a unsigned long (it will be a unsigned long int).

This is in the ANSI standard

ThibThib
  • 8,010
  • 3
  • 30
  • 37
  • Yes. `unsigned` is the same as `unsigned int` but I did a search on _WG14/N1124 Committee Draft — May 6, 2005 ISO/IEC 9899:TC2_ for `unsigned` and couldn't find where is defined...could you cite the part where it is defined? – user454322 May 22 '14 at 05:23
5

In C and C++

unsigned = unsigned int (Integer type)
signed   = signed int (Integer type)

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

An unsigned integer is either positive or zero.

Signed integers are stored in a computer using 2's complement.

VigneshM
  • 157
  • 1
  • 9
Rabin Sah
  • 53
  • 1
  • 5
3

According to C17 6.7.2 §2:

Each list of type specifiers shall be one of the following multisets (delimited by commas, when there is more than one multiset per item); the type specifiers may occur in any order, possibly intermixed with the other declaration specifiers

— void
— char
— signed char
— unsigned char
— short, signed short, short int, or signed short int
— unsigned short, or unsigned short int
— int, signed, or signed int
— unsigned, or unsigned int
— long, signed long, long int, or signed long int
— unsigned long, or unsigned long int
— long long, signed long long, long long int, or signed long long int
— unsigned long long, or unsigned long long int
— float
— double
— long double
— _Bool
— float _Complex
— double _Complex
— long double _Complex
— atomic type specifier
— struct or union specifier
— enum specifier
— typedef name

So in case of unsigned int we can either write unsigned or unsigned int, or if we are feeling crazy, int unsigned. The latter since the standard is stupid enough to allow "...may occur in any order, possibly intermixed". This is a known flaw of the language.

Proper C code uses unsigned int.

Lundin
  • 195,001
  • 40
  • 254
  • 396
1

Bringing my answer from another question.

From the C specification, section 6.7.2:

— unsigned, or unsigned int

Meaning that unsigned, when not specified the type, shall default to unsigned int. So writing unsigned a is the same as unsigned int a.

Community
  • 1
  • 1
Bruno Ferreira
  • 1,621
  • 12
  • 19