Tuesday, November 1, 2011

Integers, signed and unsigned in C programming language

Sometimes, we know in advance that the value stored in a given integer variable will always be positive—when it is being used toonly count things, for example. In such a case we can declare the variable to be unsigned, as in,
unsigned int num_students ;
With such a declaration, the range of permissible integer values (for a 16-bit OS) will shift from the range -32768 to +32767 to the range 0 to 65535. Thus, declaring an integer as unsigned almost doubles the size of the largest possible value that it can otherwise take. This so happens because on declaring the integer as unsigned, the left-most bit is now free and is not used to store the sign of the number. Note that an unsigned integer still occupies two bytes. This is how an unsigned integer can be declared:
unsigned int i ;
unsigned i ;
Like an unsigned int, there also exists a short unsigned int and a long unsigned int. By default a short int is a signed short int and a long int is a signed long int

No comments:

Post a Comment