Integer Constants

From Appmethod Topics
Jump to: navigation, search

Go Up to Constants Overview Index

Integer constants can be decimal (base 10), octal (base 8) or hexadecimal (base 16).

Decimal Constants

Decimal constants from 0 to 4,294,967,295 are allowed. Constants exceeding this limit are truncated. Decimal constants must not use an initial zero. An integer constant that has an initial zero is interpreted as an octal constant. Thus,

int i = 10;  /*decimal 10 */
int i = 010; /*decimal 8 */
int i = 0;   /*decimal 0 = octal 0 */

Octal Constants

All constants with an initial zero are taken to be octal. If an octal constant contains the illegal digits 8 or 9, an error is reported. Octal constants exceeding 037777777777 are truncated.

Hexadecimal Constants

All constants starting with 0x (or 0X) are taken to be hexadecimal. Hexadecimal constants exceeding 0xFFFFFFFF are truncated.

Integer Constants with L and U Suffixes

The suffix L (or l) attached to any constant forces the constant to be represented as a long. Similarly, the suffix U (or u) forces the constant to be unsigned. The constant type is unsigned long if the value of the number itself is greater than decimal 65,535, regardless of which base is used.

If the constant has a U (or u ) suffix, its data type will be the first of unsigned int, unsigned long int that can accommodate its value.

If the constant has an L (or l) suffix, its data type will be the first of long int, unsigned long int that can accommodate its value.

You can use both L and U suffixes on the same constant in any order or case: ul, lu, UL, and so on. If the constant has both L and U suffixes (ul, lu, Ul, lU, uL, Lu, LU or UL) its data type will be unsigned long int.

Integer Constants Without L or U Suffixes

In the absence of any overriding suffixes (U, u, L, or l), the data type of an integer constant is derived from its value. The data type is defined as the first of the following types that can accommodate an integer constant value:

Decimal

int, long int, unsigned long int

Octal

int, unsigned int, long int, unsigned long int

Hexadecimal

int, unsigned int, long int, unsigned long int


How to define the data type according to an integer constant value is shown in the following tables.

Decimal constants - derived types:

Constant value

Derived data type

0 to 32,767

int

32,768 to 2,147,483,647

long

2,147,483,648 to 4,294,967,295

unsigned long

> 4294967295

truncated


Octal constants - derived types:

Constant value

Derived data type

00 to 077777

int

010000 to 0177777

unsigned int

02000000 to 017777777777

long

020000000000 to 037777777777

unsigned long

> 037777777777

truncated


Hexadecimal constants - derived types:

Constant value

Derived data type

0x0000 to 0x7FFF

int

0x8000 to 0xFFFF

unsigned int

0x10000 to 0x7FFFFFFF

long

0x80000000 to 0xFFFFFFFF

unsigned long

> 0xFFFFFFFF

truncated

Note that the rules vary between decimal and nondecimal constants.


Integer Constants with size-suffixes

Using a size-suffix you can specify the number of bytes occupied in memory by an integer constant (the size of an integer constant). You can use the following size-suffixes: i8, I8, i16, I16, i32, I32, i64, and I64.

A size-suffix can be preceded by the unsigned - U (or u) suffix. For example, ui8, Ui16, UI32, ui64, and so on.

The size-suffix must exactly match the declared extended integer type of the integer constant. For example, you can use the following integer constants of extended integer types:

__int16 s = 32767i16;
unsigned __int16 us = 64532Ui16;
unsigned __int32 ui = 223456789uI32;

See Also