Primitives

Range of number

Because variables are memory spaces (made up of fixed amounts of byte), they can represent limited ranges of number depending of their size.

For example, the primitive char weights 1 byte, which is made of (almost always) 8 bits. That means it can represent 256 different values (When the byte size is 8 bits). The type char is a primitive of the category integer. It therefore can only represent integers (Positive and negative). Because of its size and primitive category, it can only represent integers in the range [-128, 127] (When the byte size is 8 bits).

Except for the types char and unsigned char, the size of the primitives may vary depending of the compiler used.

Here is a list of the primitives, grouped by category:

Integer primitives

Unsigned integer primitives

Floating point primitives

Above, the primitives are sorted, inside their category, from smaller to bigger (In term of number of bytes they take in the memory and therefore in term of range of number they can represent). That means that the type int is at least as big as short int or bigger, and int long is at least as big as int or bigger.

Note that in the case of unsigned integers, the keyword int can be omitted. That means that unsigned is equivalent to unsigned int and unsigned long is equivalent to unsigned int long.

It is also possible to write long long int instead of int long long, and long double instead of double long...

Size and range example

The following tables list the primitives with possible sizes (In bytes). The range of number they can represent (In function of the size displayed as an example) is shown (For signed and unsigned integer types only). Note that in the examples below, bytes are considered to weight 8 bits.

Integer primitives

PrimitiveSize (In bytes)Range of number
char1[-128, 127]
short int2[-32768, 32767]
int4[-2147483648, 2147483647]
long int4[-2147483648, 2147483647]
long long int8[-9223372036854775808, 9223372036854775807]

Unsigned integer primitives

PrimitiveSize (In bytes)Range of number
unsigned char1[0, 255]
unsigned short int2[0, 65535]
unsigned int4[0, 4294967295]
unsigned long int4[0, 4294967295]
unsigned long long int8[0, 18446744073709551615]

Floating point primitives

PrimitiveSize (In bytes)
float4
double8
double long16

Note that floating point primitives can represent bigger numbers than even the type unsigned int long long can, but in that case, the precision will be very low.

Calculating the range of value representable by integer primitives

For unsigned integer types

The number of value a primitive can represent is 2 at the power of its size in bits. If an unsigned integer type weights 64 bits, then it can represent 2 64(18446744073709551616) values. The biggest number it can represent is then 2 64-1 (18446744073709551615), because the number zero is part of the values it can represent. The range of number that primitive can hold is then [0, 18446744073709551615].

For signed integer types

The biggest negative value an integer primitive can represent is the number of value it can represent divided by -2. The biggest positive number it can represent is the number of value it can represent, divided by 2, and then subtracted by 1 (Because of the value 0 it can represent). If an integer type weights 16 bits, then the number of value it can represent is 2 16(65536). The biggest negative number it can hold is then -32768 (65536/-2) and the biggest positive value is 32767 (65536/2-1). The range of number it can store is therefore [- 32768, 32767].

Integer vs floating point primitives

All floating point primitives can represent positive and negative numbers. They also can represent a very wide range of numbers (A huge range). Then, why using integer primitives instead? Because of precision. Floating point primitives often approximate numbers (Especially when it comes to numbers with many digits). For example, the number 123456.789 might become 123456 and 123456789 become 123456792. However, in most cases, that precision loss is not problematic. Note that we can reduce that loss by using bigger floating point primitives (double instead of float, for example).