What is an unsigned data type?

I saw this type of unsigned "typeless" that was used a couple of times, but never saw an explanation. I assume that the corresponding type signed exists. Here is 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 collected so far:
- on my system, sizeof(unsigned) = 4 (hints at a 32-bit unsigned int)
- it can be used as a shortened version for casting another type into an unsigned version:

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

Is it ANSI C or just a compiler extension?

+62
c types unsigned
Jul 23 '09 at 13:44
source share
6 answers

unsigned indeed a shorthand for unsigned int and is therefore defined in the C standard.

+107
Jul 23 '09 at 13:46
source share

unsigned means unsigned int . signed means signed int . Using only unsigned is the lazy way to declare unsigned int in C. Yes, this is ANSI.

+23
Jul 23 '09 at 13:48
source share

Historically in C, if you omitted the data type "int", it was assumed. Thus, "unsigned" is short for "unsigned int". This has been considered bad practice for a long time, but there is still quite a lot of code that uses it.

+11
Jul 23 '09 at 13:49
source share

in C, unsigned is a shortcut for unsigned int .

You have the same for long , which is a shortcut for long int

And you can also declare unsigned long (it will be unsigned long int ).

It is in the ANSI standard

+6
Jul 23 '09 at 13:49
source share

In C and C ++

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

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

An unsigned integer is either positive or zero.

Signed integers are stored on a computer using 2 additions.

+3
Feb 04 '15 at 6:51
source share

Sending my answer from another question .

From specification C , section 6.7.2:

- unsigned or unsigned int

The value is unsigned , if no type is specified, the default should be unsigned int . Therefore, the entry unsigned a same as unsigned int a .

0
Jan 04 '17 at 19:27
source share



All Articles