The Fundamental Types

From Appmethod Topics
Jump to: navigation, search

Go Up to Declaration Syntax Index


The fundamental type specifiers are built from the following keywords:

char

__int8

long

double

__int16

signed

float

__int32

short

int

__int64

unsigned

From these keywords you can build the integral and floating-point types, which are together known as the arithmetic types. The modifiers long, short, signed, and unsigned can be applied to the integral types. The include file limits.h contains definitions of the value ranges for all the fundamental types.

Integral types

char, short, int, and long, together with their unsigned variants, are all considered integral data types. The following table shows the integral type specifiers, with synonyms listed on the same line.

char, signed char

Synonyms if default char set to signed.

unsigned char

char, unsigned char

Synonyms if default char set to unsigned.

signed char

int, signed int

unsigned, unsigned int

short, short int, signed short int

unsigned short, unsigned short int

long, long int, signed long int

unsigned long, unsigned long int

signed long long, long long int
(ISO C99, C++0x)

unsigned long long, unsigned long long int
(ISO C99, C++0x)

Note: These synonyms are not valid in C++. See The Three char Types.

signed or unsigned can only be used with char, short, int, or long. The keywords signed and unsigned, when used on their own, mean signed int and unsigned int, respectively.

In the absence of unsigned, signed is assumed for integral types. An exception arises with char. You can set the default for char to be signed or unsigned. (The default, if you don't set it yourself, is signed.) If the default is set to unsigned, then the declaration char ch declares ch as unsigned. You would need to use signed char ch to override the default. Similarly, with a signed default for char, you would need an explicit unsigned char ch to declare an unsigned char.

Only long or short can be used with int. The keywords long and short used on their own mean long int and short int.

ANSI C does not dictate the sizes or internal representations of these types, except to indicate that short, int, and long form a nondecreasing sequence with "short <= int <= long." All three types can legally be the same. This is important if you want to write portable code aimed at other platforms.

The compiler regards the types int and long as equivalent, both being 32 bits. The signed varieties are all stored in two's complement format using the most significant bit (MSB) as a sign bit: 0 for positive, 1 for negative (which explains the ranges shown in 32-bit data types, sizes, and ranges). In the unsigned versions, all bits are used to give a range of 0 - (2n - 1), where n is 8, 16, or 32.

Floating-point types

The representations and sets of values for the floating-point types are implementation dependent; that is, each implementation of C is free to define them. The compiler uses the IEEE floating-point formats. See the topic on ANSI Implementation-specific Standards.

float and double are 32-bit and 64-bit floating-point data types, respectively. long can be used with double to declare an 80-bit precision floating-point identifier: long double test_case, for example.

The table of 32-bit data types, sizes, and ranges indicates the storage allocations for the floating-point types.

Standard arithmetic conversions

When you use an arithmetic expression, such as a + b, where a and b are different arithmetic types, The compiler performs certain internal conversions before the expression is evaluated. These standard conversions include promotions of "lower" types to "higher" types in the interests of accuracy and consistency.

Here are the steps the compiler uses to convert the operands in an arithmetic expression:

  1. Any small integral types are converted as shown in the table "Methods used in standard arithmetic conversions" in The Fundamental Types. After this, any two values associated with an operator are either int (including the long and unsigned modifiers), or they are of type double, float, or long double.
  2. If either operand is of type long double, the other operand is converted to long double.
  3. Otherwise, if either operand is of type double, the other operand is converted to double.
  4. Otherwise, if either operand is of type float, the other operand is converted to float.
  5. Otherwise, if either operand is of type unsigned long, the other operand is converted to unsigned long.
  6. Otherwise, if either operand is of type long, then the other operand is converted to long.
  7. Otherwise, if either operand is of type unsigned, then the other operand is converted to unsigned.
  8. Otherwise, both operands are of type int.

The result of the expression is the same type as that of the two operands.

Methods used in standard arithmetic conversions

Type Converts to Method

char

int

Zero or sign-extended (depends on default char type)

unsigned char

int

Zero-filled high byte (always)

signed char

int

Sign-extended (always)

short

int

Same value; sign extended

unsigned short

unsigned int

Same value; zero filled

enum

int

Same value

Special char, int, and enum conversions

Note: The conversions discussed in this section are specific to the Appmethod C++ bcc32.exe compiler.

Assigning a signed character object (such as a variable) to an integral object results in automatic sign extension. Objects of type signed char always use sign extension; objects of type unsigned char always set the high byte to zero when converted to int.

Converting a longer integral type to a shorter type truncates the higher order bits and leaves low-order bits unchanged. Converting a shorter integral type to a longer type either sign-extends or zero-fills the extra bits of the new value, depending on whether the shorter type is signed or unsigned, respectively.

See Also