Data Type Modifier-2
Data Type Modifier-2
• signed
• unsigned
Character
character
float and double
• Both are used for storing floating point value.
• Based on IEEE 754 Standard:
1. Structure of a 32-bit Float:
1. 1 bit: Sign (positive or negative).
2. 8 bits: Exponent (biased by 127).
3. 23 bits: Mantissa (precision with an implicit leading 1).
2. Representation Formula:
3. Exponent:
• Range E = 1 – 254 (0 and 255 are stored).
• Actual exponent = E – 127
• Effective range = −126 to +127.
float and double
4. Mantissa:
(1.11111111111111111111111)₂ = (1 × 2⁰) + (1 × 2⁻¹) + (1 × 2⁻²) + (1 × 2⁻³)
+ (1 × 2⁻⁴) + (1 × 2⁻⁵) + (1 × 2⁻⁶) + (1 × 2⁻⁷) + (1 × 2⁻⁸) + (1 × 2⁻⁹) + (1 × 2⁻¹⁰)
+ (1 × 2⁻¹¹) + (1 × 2⁻¹²) + (1 × 2⁻¹³) + (1 × 2⁻¹⁴) + (1 × 2⁻¹⁵) + (1 × 2⁻¹⁶) + (1 ×
2⁻¹⁷) + (1 × 2⁻¹⁸) + (1 × 2⁻¹⁹) + (1 × 2⁻²⁰) + (1 × 2⁻²¹) + (1 × 2⁻²²) + (1 × 2⁻²³) =
(1.99999988079071044922)₁₀
Positive range:
1.99999988079071044922 * 2^127 = 3.4028234664×10³⁸
Negative range:
(-1)*1.99999988079071044922 * 2^127 = -
3.4028234664×10³⁸
float and double
• Both are used for storing floating point
value.
• float occupies 4 bytes and it’s range is:
-3.4e38 to +3.4e38
– Format specifier : %f
• double occupies 8 bytes and it’s range is:-–
-1.7e308 to +1.7e308
– Format specifier : %lf
long double
floating point
Type Conversions in Expressions
• C let us mix different types of data together in
one expression.
float
Left
int 11.995
Right
float
int
3 float
Left
3.0
int
11
;
Type conversion in assignment
• In an assignment statement in which the type of the
right side differs from that of the left, the type of the
right side is converted into that of the left.
• When the type of the left side is larger than the type of
the right – no problem.
• For reverse – problem!
• Example
Type Cast
• To transform the type of a variable temporarily.
• To do so, the expression must be preceded by the
name of the desired data type, enclosed in
parentheses
int number;
(float) number;
Valid or Invalid?
num % 2;
((int)num) % 2;
Valid or Invalid?
i = 7;
f = 8.5;
result = (i + f) % 4; Invalid
• Type conversion 2
Home work