Notes on the int data type in C

Synopsis

Integers (signed and unsigned) only. No decimal places. No commas. If you want to gaurantee signed or unsigned you must explicitly define it.

[signed|unsigned] [range] = <value>

Signed integers

signed int myInt   = 12000;
signed int myInt2  = -999;
printf("Value of myInt is: %i", myInt);
printf("Value of myInt2 is: %i", myInt2);

For shorthand signed integers you can simply use int keyword.

int myInt   = 12000;
int myInt2  = -999;

Unsigned integers

unsigned int myInt3  = 12000;
printf("Value of myInt3 is: %u", myInt3);

For shorthand unsigned integers you can simply use unsigned keyword.

unsigned myInt3  = 12000;

Ranges

int is gauranteed to be at least 1 byte in size (8 bits wide). But is usually either 2 bytes (16 bits wide) or (as per most modern machines) 4 bytes (32 bits wide) in size

Test it:

printf("On this machine, int is stored in %lu bytes. (%lu bits wide).\n",
                sizeof(int),
                ((sizeof(int)) * 8));

short int is an integer value of reduced precision. For example, if the machine stores an int 32 bits wide, then short int will be 16 bits wide. A short int is guaranteed to never be wider than an int (although it may be the same).

Test it:

printf("On this machine, short is stored in %lu bytes. (%lu bits wide).\n",
                sizeof(short),
                ((sizeof(short)) * 8));

long int is an integer value of extended precision. Gauranteed to be at least 4 bytes in size (contain at least 32 bits of precision), and may or may not be larger than a standard int.

Test it:

printf("On this machine, long is stored in %lu bytes. (%lu bits wide).\n",
                sizeof(long),
                ((sizeof(long)) * 8));

long long int is an integer value of extra extended precision. Gauranteed to be at least 8 bytes in size (contain at least 64 bits of precision).

Test it:

printf("On this machine, long long is stored in %lu bytes. (%lu bits wide).\n",
                sizeof(long long),
                ((sizeof(long long)) * 8));

Ranges in real terms

8-bits wide (1 byte)

– Maximum signed range is from −128 to 127.
– Maximum unsigned range is from 0 to 255.

16-bits wide (2 bytes)

– Maximum signed range is from −32,768 to 32,767.
– Maximum unsigned range is from 0 to 65,535.

32-bits wide (4 bytes)

– Maximum signed range is from −2,147,483,648 to 2,147,483,647.
– Maximum unsigned range is from 0 to 4,294,967,295.

64-bits wide (8 bytes)

– Maximum signed range is from −9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
– Maximum unsigned range is from 0 to 18,446,744,073,709,551,615

There is more information on integer ranges here: http://en.wikipedia.org/wiki/Integer_(computer_science)

Good to know

Without explicit specification, the type of an integer constant defaults to the smallest possible type that can hold the constant’s value, unless the value is suffixed with an L , l , U , or u . The following list describes the type assignment of integer constants:

* If the constant has no suffix, and is given in decimal radix, it will have the first type from this list capable of storing the value: int , long int , unsigned long int .
* If the constant has no suffix, and is given in octal or hexadecimal radix, it will have the first type from this list capable of storing the value: int , unsigned int , long int , unsigned long int .
* If the constant has the U or u suffix, it will have the first type from this list capable of storing the value: unsigned int , unsigned long int .
* If the constant has the L or l suffix, it will have the first type from this list capable of storing the value: long int , unsigned long int .
* If the constant has both U and L suffixes (or the lowercase combination), it will have type unsigned long int .
* For example, the constant 59 is assigned the int data type by default, but the constant 59L is assigned the long data type. 59UL is typed as unsigned long int .

* from cs.auckland.ac.nz

Octal notation. (Base 8)

Integers starting with a 0 are treated as octal (base 8).

int myDecimal   = 16;
int myOctal     = 020; // 16 in decimal.
printf("Value of myOctal is: %o", myOctal); // Value of myOctal is: 50
printf("Value of myOctal is: %#o", myOctal); // Value of myOctal is: 050

Hexidecimal notation. (Base 16)

Integers starting with a 0x are treated as hexidecimal (base 16). In hexidecimal, symbols 0–9 represent values zero to nine, and A, B, C, D, E, F (or alternatively a–f) represent values ten to fifteen.

int myDecimal   = 15;
int myHex       = 0xF; // 15 in decimal.
printf("Value of myHex is: %x", myHex); // Value of myHex is: f
printf("Value of myHex is: %#x", myHex); // Value of myHex is: 0xf
printf("Value of myHex is: %X", myHex); // Value of myHex is: F
printf("Value of myHex is: %#X", myHex); // Value of myHex is: 0xF
printf("Value of myHex is: %i", myHex); // Value of myHex is: 15

The primary use of hexadecimal notation is a human-friendly representation of binary-coded values. Hexadecimal is also commonly used to represent computer memory addresses.

On some machines each hexidecimal code (e.g. 5 or D) takes up 4 bits (a nibble) as opposed to a decimal which takes up (at least) 8 bits.

Binary notation. (Base 2)

C (and C++) do not have native support for binary notation. You can only work with decimal (base 10), octal (base 8) or hexadecimal (base 16) integers as standard.

int myBinary = 0xF; // binary: 00001111

Some compliers (such as GCC) support binary notation using an 0b prefix.

int x = 0b00001111;

But as support isn’t widespread it’s safer to stick to base 10, 8 or 16.

More Information

http://en.wikipedia.org/wiki/C_data_types

Leave a Reply

Your email address will not be published. Required fields are marked *