0% found this document useful (0 votes)
11 views39 pages

Data Type Modifier-2

The document discusses data types in C programming, highlighting five basic types (void, char, int, float, double) and four modifiers (long, short, signed, unsigned). It explains the necessity of data type modifiers for efficient memory usage and provides details on type conversions in expressions, including type promotion and assignment. Additionally, it covers the representation of floating-point values and the rules governing type conversions in C.

Uploaded by

resam.zaha.hp
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views39 pages

Data Type Modifier-2

The document discusses data types in C programming, highlighting five basic types (void, char, int, float, double) and four modifiers (long, short, signed, unsigned). It explains the necessity of data type modifiers for efficient memory usage and provides details on type conversions in expressions, including type promotion and assignment. Additionally, it covers the representation of floating-point values and the rules governing type conversions in C.

Uploaded by

resam.zaha.hp
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 39

A Closer look at data types,

variables and expressions


Data Type Modifiers
• In chapter 1 we learned that C has five basic data types-
• void
• char (%c)
• int (%d)
• float (%f)
• double (%lf)

• C also have 4 data type modifiers-


• long
• short
• signed
• unsigned
What is it?
• C’s data-type modifier modifies the C’s
basic data type (except type void) to more
precisely fit our specific need.
Why Necessary?
• int data type your compiler use 4 bytes (32 bits)
• So we can represent integer value between - to

How can I represent integer


value more than this!
Why Necessary?
• Suppose, our compiler use 4 bytes (32 bits) for integer.
• In our program we use 100 int variable.
• We need to store integer values less than 100.
• 1 byte (8 bits) is enough to store those values.

Why waste memory?


How to save?
Data Type Modifiers
• In chapter 1 we learned that C has five basic data types-
• void
• char (%c)
• int (%d)
• float (%f)
• double (%lf)
• C also have 4 data type modifiers-
• long
• short
• signed
• unsigned
For integer data type
For integer data type
For 1 byte or 8 bits, the MSB is sign bit which is 0 for positive:
01111111 = (01111111)₂ = (1 × 2⁶) + (1 × 2⁵) + (1 × 2⁴) + (1 × 2³) +
(1 × 2²) + (1 × 2¹) + (1 × 2⁰) = (127)₁₀
Upper limit = 127
1 for negative:
For integer data type
• Integer data type supports all four data type
modifiers
• long
• short
• signed
• unsigned
What do they do?
long and short int
• When we declare an integer it occupies 4 bytes on a 32
bit OS.
• Sometimes it may be not enough.
• Sometimes it may be more than required.
• Why waste memory?
• How to save?
short integer

Format specifier: %hd


long integer
long long integer
• Simple double of long int!!

long long int a;

• long long int format specifier: %lld


signed and unsigned
• The range for an integer will be:
-231 to 231-1
or
-2147483648 to 2147483647
• Some times we do not need negative value.
• Why waste half of the values???
• Use signed and unsigned variable in this
case.
unsigned integer

• unsigned long int format specifier: %lu


signed integer
integers
%hd
For Character Data Type
• Character data type supports only two data
type modifiers

• 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.

• This is perfectly valid!


Type Conversions in Expressions…
• C allows this mixing because it has a strict
set of conversion rules that dictate how
type differences are resolved.
Type Convention
• Operands that differ in type may undergo type
conversion before the expression takes on its final
value.
• In general, the final result will be expressed in the
highest precision possible, consistent with the data
types of the operands.
• The following rules apply when neither operand is
unsigned.
int i = 7; ASCII Value
float f = 5.5; w = 119
char c = ‘w’ 0 = 48

i+f 12.5 float (double)

i+c 126 integer


i + c – ‘0’ 78 integer

(i + c) - (2 * f / 5) 123.8 float (double)


• Type conversion
Conversions Rule – type Promotion
• After the automatic integral promotions have been
applied, the C compiler will convert all operands “up” to
the type of the largest.
• This is called type promotion and is done on an
operation-by-operation basis.
Type Convention (Assignment)
• If the two operands in assignment expression are of
different data types.
• The value of right hand operand will automatically be
converted to the type of the operand on the left.
• The entire assignment expression will be then same
data type.

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

(data type) expression

int number;
(float) number;
Valid or Invalid?

float num = 10.5;

num % 2;

float num = 10.5;

((int)num) % 2;
Valid or Invalid?

i = 7;
f = 8.5;

result = (i + f) % 4; Invalid

• Type conversion 2
Home work

You might also like