![]() |
C - Entwicklung Variable: Typen Homepage von PS-Trainer - C-Entwicklung - Variable - an PS-Trainer |
|
| Variables | Storage classes, types, structures, pointers, arrays |
| Fundamental types | Fundamental types |
| Data type constants | Ranges of values for integral data types |
| int (short, long) | Integer type (family) specifier (whole numbers) |
| signed and unsigned | Signed and unsigned modifiers can be used with any integral type. |
| char (escape sequences) | Type used to store the value of a character set member, or to store a single byte of data. |
| float (double) (float constants) | Float type (family) specifier (fractional numbers) |
| void | Describes an empty set of values. |
| Fundamental types Fundamental types in C++ are divided into three categories: "integral," "floating," and "void."
|
||||||||||||||||||||||||||||||||||||||||||||
|
Data Type Constants
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Example: Click this link to open the source code of an <integer family type test program> |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
| int [type-specifier] [ signed | unsigned ] integer-modifier [ int ] declarator-list; type-specifier Specifies a base_type, struct, union, enum type, or type identifier. An optional storage specification can precede type-specifier. integer-modifier Specifies the keyword small, short, long, or hyper, which selects the size of the integer data. On 16-bit platforms, the size qualifier must be present. declarator-list Specifies one or more standard C declarators, such as identifiers, pointer declarators, and array declarators. (Function declarators and bit-field declarations are not allowed in structures that are transmitted in remote procedure calls. These declarators are allowed in structures that are not transmitted.) Separate multiple declarators with commas. Remarks The int and unsigned int types have the size of the system word. This is two bytes (the same as short and unsigned short) in MS-DOS and
16-bit versions of Windows, and 4 bytes in 32-bit operating systems. However,
portable code should not depend on the size of int.
On 32-bit platforms, the keyword int specifies a 32-bit signed integer. On 16-bit platforms, the keyword int is an optional keyword that can accompany the keywords small, short, and long.
DCE IDL compilers do not allow the keyword signed to specify the sign of integer types. Therefore, this feature is not available when you use the MIDL compiler /osf switch. Keywords: base_types, long, /osf, short, small |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Example: Click this link to open the source code or the console (DOS) exe-program of an <integer family type test program>, both provided in ZIP-type archives. This program allows to print all current (integral) data type constants, and to test length and bit pattern of the various integral data types. |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Examples: signed short int i = 0; int j = i; typedef struct { } INTSIZETYPE; HRESULT MyFunc([in] long int lCount); |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Examples of integer-type values and their internal representation:
In any system of numbers, a value is represented by a series x0*base^0 + x1*base^1 + x2*base^2... in reverse annotation: Decimal example: 1234 = 1*10^3 + 2*10^2 + 3*10^1 + 4*10^0 = 1*1000 + 2*100 + 3*10 + 4*1 = 1000 + 200 + 30 + 4 = 1234 Hex example: hex1A2B = 1*16^3 + 10*16^2 + 2*16^1 + 11*16^0 = 1*4096 + 10*256 + 2*16 + 11*1 = 4096 + 2560 + 32 + 11 = 6699 Oct example: oct1234 = 1*8^3 + 2*8^2 + 3*8^1 + 4*8^0 = 1*512 + 2*64 + 3*8 + 4*1 = 512 + 128 + 24 + 4 = 668 Bin example: bin 1010 = 1*2^3 + 0*2^2 + 1*2^1 + 0*2^0 = 1*8 + 0*4 + 1*2 + 0*1 = 8 + 0 + 2 + 0 = 10 Decimal exponents: 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000... Hex exponents: 1, 16, 256, 4096, 65536, 1048576, 16777216, 268435456, ... Bin exponents: 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536, ...
The leftmost "hi-end" bit must be 0 in every positive number and zero. This "sign bit" is 1 in every negative number. Watch how any positive number is "inverted" to form a negative number of the same absolute value. Unsigned types do not interpret the sign bit, their value range is positive only, but twice the absolute value of signed type of the same length. |
| signed and
unsigned Signed and unsigned are modifiers that can be used with any integral type. The char type is signed by default, but you can specify /J to make it unsigned by default. |
| char The char type is used to store the integer value of a member of the representable character set. Click this Link to a table of common character sets. That integer value is the ASCII code corresponding to the specified character. Microsoft Specific
The keyword char
identifies a data item that has 8 bits. Character values of type unsigned char have a range from 0 to 0xFF hexadecimal. A signed char has range 0x80 to 0x7F. These ranges translate to 0 to 255 decimal, and 128 to +127 decimal, respectively. The /J compiler option changes the default from signed to unsigned. END Microsoft Specific To the MIDL compiler, a char is unsigned by default and is synonymous with unsigned char. Microsoft Specific
The char type is one of
the base types of the interface definition language (IDL). The char
type can appear as a type specifier in const declarations, typedef
declarations, general declarations, and function declarators (as a function-return-type
specifier and a parameter-type specifier). In the Microsoft RPC compiler , the character translation tables that convert between ASCII and EBCDIC are built into the run-time libraries and cannot be changed by the user. END Microsoft Specific DCE IDL compilers do not accept the keyword signed applied to char types. Therefore, this feature is not available when you use the MIDL compiler /osf switch. Keywords: base_types, byte, /char, /osf, signed, string, wchar_t |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Escape Sequences Character combinations consisting of a backslash (\) followed by a letter or by a combination of digits are called "escape sequences." To represent a newline character, single quotation mark, or certain other characters in a character constant, you must use escape sequences. An escape sequence is regarded as a single character and is therefore valid as a character constant. Escape sequences are typically used to specify actions such as carriage returns and tab movements on terminals and printers. They are also used to provide literal representations of nonprinting characters and characters that usually have special meanings, such as the double quotation mark ("). The following table lists the ANSI escape sequences and what they represent. Note that the question mark preceded by a backslash (\?) specifies a literal question mark in cases where the character sequence would be misinterpreted as a trigraph.
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Examples of character-type values and their internal representation:
Remark: The characters 0...127 are the same in common character sets, whereas caracters 127...255 are interpreted differently, depending on the character set. Remark: Mind that upper case characters and the corresponding lower case characters are different in 1 bit only.
|
| float Floating types are capable of specifying values that may have fractional parts. Floating-point numbers use the IEEE (Institute of Electrical and Electronics Engineers) format. Single-precision values with float type have 4 bytes, consisting of a sign bit, an 8-bit excess-127 binary exponent, and a 23-bit mantissa. The mantissa represents a number between 1.0 and 2.0. Since the high-order bit of the mantissa is always 1, it is not stored in the number. This representation gives a range of approximately 3.4E38 to 3.4E+38 for type float. You can declare variables as float or double, depending on the needs of your application. The principal differences between the two types are the significance they can represent, the storage they require, and their range.
The long double data type (80-bit,
10-byte precision) is mapped directly to double
(64-bit, 8- byte precision) in Windows NT and Windows 95. |
||||||||||||||||||||||
| Internal representation: Floating-point variables are represented by a mantissa, which contains the value of the number, and an exponent, which contains the order of magnitude of the number.
For type float, the bias is 127; for type double, it is 1023. You can compute the actual exponent value by subtracting the bias value from the exponent value. The mantissa is stored as a binary fraction greater than or equal to 1 and less than 2. For types float and double, there is an implied leading 1 in the mantissa in the most-significant bit position, so the mantissas are actually 24 and 53 bits long, respectively, even though the most-significant bit is never stored in memory. Instead of the storage method just described, the floating-point package can store binary floating-point numbers as denormalized numbers. "Denormalized numbers" are nonzero floating-point numbers with reserved exponent values in which the most-significant bit of the mantissa is 0. By using the denormalized format, the range of a floating-point number can be extended at the cost of precision. You cannot control whether a floating-point number is represented in normalized or denormalized form; the floating-point package determines the representation. The floating-point package never uses a denormalized form unless the exponent becomes less than the minimum that can be represented in a normalized form.
Conversely, if precision is the most important criterion, use type double. Floating-point variables can be promoted to a type of greater significance (from type float to type double). Promotion often occurs when you perform arithmetic on floating-point variables. This arithmetic is always done in as high a degree of precision as the variable with the highest degree of precision. For example, consider the following type declarations: float f_short; double f_long; long double f_longer; f_short = f_short * f_long; In the preceding example, the variable f_short is promoted to type double and multiplied by f_long; then the result is rounded to type float before being assigned to f_short. In the following example (which uses the declarations from the preceding example), the arithmetic is done in float (32-bit) precision on the variables; the result is then promoted to type double: f_longer = f_short * f_short; |
||||||||||||||||||||||
|
Floating-Point Constants A "floating-point constant" is a decimal number that represents a signed real number. The representation of a signed real number includes an integer portion, a fractional portion, and an exponent. Use floating-point constants to represent floating-point values that cannot be changed. Syntax floating-point-constant : fractional-constant exponent-part opt floating-suffix opt digit-sequence exponent-part floating-suffix opt fractional-constant : digit-sequence opt . digit-sequence digit-sequence . exponent-part : e sign opt digit-sequence E sign opt digit-sequence sign : one of + digit-sequence : digit digit-sequence digit floating-suffix : one of f l F L You can omit either the digits before the decimal point (the integer portion of the value) or the digits after the decimal point (the fractional portion), but not both. You can leave out the decimal point only if you include an exponent. No white-space characters can separate the digits or characters of the constant. The following examples illustrate some forms of floating-point constants and expressions: 15.75 1.575E1 1575e-2 -2.5e-3 25E-4 Floating-point constants are positive unless they are preceded by a minus sign (). In this case, the minus sign is treated as a unary arithmetic negation operator. Floating-point constants have type float, double, long, or long double. A floating-point constant without an f, F, l, or L suffix has type double. If the letter f or F is the suffix, the constant has type float. If suffixed by the letter l or L, it has type long double. For example: 100L 100F 100D The Microsoft C compiler maps
long double to type double.
You can omit the integer portion of the floating-point constant, as shown in the following examples. The number .75 can be expressed in many ways, including the following: .0075e2 0.075e1 .075e1 75e-2 |
||||||||||||||||||||||
| Examples of floating-point values and their internal representation:
Each number can be represented using exponent and mantissa: Decimal: Binary: In float types both exponent and mantissa are stored separately: The exponent langth defines the number range, the mantissa length defines the precision ("significant digits"). The mantissa can be negative, this information is stored in a single "sign bit". Since the mantissa (normalized to 1.00...2.00) always starts with a 1-bit, this bit is omitted and not stored (hidden bit). To interpret a float number, this "most significant bit" has to be added. Exponents are stored in an unsigned form + bias 127 (\x7F = bin 0111 1111). To interpret the exponent, 127 must be added. To learn more about the internal data format of real numbers, IEEE 754, 2 tools are provided: IEEE-754 tools: The formulas are free to check or to copy, although they cannot be changed. Right-click Float_IEEE754.XLS and "save target" to your disk to use it. Both the EXE-program and the (free) C++ source files are included in the ZIP-type archive IEEE754.ZIP. Right-click IEEE754.ZIP and "save target" to your disk to use it. |
| void The void type describes an empty set of values. No variable of type void can be specified it is used primarily to declare functions that return no values or to declare "generic" pointers to untyped or arbitrarily typed data. Any expression can be explicitly converted or cast to type void. However, such expressions are restricted to the following uses: |
| Aktuelle Daten dieser Seite | Letzte Änderung: |
| |