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. Integral types shows the integral type specifiers, with synonyms listed on the same line.
Integral types
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 |
|
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.
float and double are 32- 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:
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
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.
Copyright(C) 2008 CodeGear(TM). All Rights Reserved.
|
What do you think about this topic? Send feedback!
|