Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is the unsigned datatype?

Tags:

c

types

unsigned

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?

like image 234
hhaamu Avatar asked Jul 23 '09 13:07

hhaamu


People also ask

What is unsigned data type in C?

unsigned char is a character datatype where the variable consumes all the 8 bits of the memory and there is no sign bit (which is there in signed char). So it means that the range of unsigned char data type ranges from 0 to 255.

What is signed and unsigned datatype?

Variables such as integers can be represent in two ways, i.e., signed and unsigned. Signed numbers use sign flag or can be distinguish between negative values and positive values. Whereas unsigned numbers stored only positive numbers but not negative numbers.

Is unsigned int a data type?

Unsigned int is a data type that can store the data values from zero to positive numbers whereas signed int can store negative values also. It is usually more preferable than signed int as unsigned int is larger than signed int. Unsigned int uses “ %u ” as a format specifier.

What is unsigned data type in Java?

An unsigned integer can hold a larger positive value, and no negative value like (0 to 255) . Unlike C++ there is no unsigned integer in Java.


1 Answers

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

like image 183
Martin v. Löwis Avatar answered Nov 23 '22 10:11

Martin v. Löwis