Fundamental Data Types

Download Report

Transcript Fundamental Data Types

Fundamental Data Types
1
Declaration
• All variables must be declared before being
used.
– Tells the compiler to set aside an appropriate
amount of space in memory to hold values
associated with variables.
– Enables the compiler to perform operations
using the declared variables.
2
Basic Data Types
• char
character
• int
integer
• float
floating-point
• double
double floating-point
• void
valueless
3
Modifying the Basic Types
• Type modifier:
signed
unsigned
long
short
• When a type modifier is used by itself, then int is
assumed.
Modifier
signed
unsigned
long
short
Same As
signed int
unsigned int
long int
short int
• The char type can be modified:
char
signed char
unsigned char
4
Data Type char
Have Seen:Chars are treated as small integers &
conversely small ints are treated as chars.
char c = ‘a’;
printf (“%c”, c + 1)
b
printf (“%d”, c + 2)
99
Each char variable stored in 1 Byte 8 Bits:
27 26 25 24 23 22 21 20
128 64 32 16 8
4
2
1
a
0
1
1
0
0
0
0
1
5
• String of binary digits are called bit
strings.
• A bit string is interpreted as a binary
number. bnbn-1……b2b1b0
0 1100 0 01
a
• This bit string has the value
126 + 125 + 120= 97
6
3 types:
1) char 2) unsigned char 3) signed char
Each uses 1 Byte.
Typically char is equivalent to either signed
or unsigned char.
Signed char
Unsigned char
-128 to 127
0 to 255
7
Data Type int
INTEGERS : Include the natural counting
numbers and their negatives.
INTEGRAL DATA TYPES:
int
short
long
unsigned
8
• The magnitude of the number that an int variable
can hold depends on ___________?
word size(bits) of the machine is machine dependent
• The range, u, of values:
2wordsize-1  u  2wordsize-1-1
2 Byte word ~ -32 Thousand To
+32 Thousand
-215, -215+1, …, -3, -2, -1, 0, 1, 2, 3, …, 215-1
4 Byte word ~ -2 Billion
To
+2 Billion
-231, -231+1, …, -3, -2, -1, 0, 1, 2, 3, …, 231-1
9
• Integer overflow:
– Value too large for defined Storage Location.
– Typically program continues to run but
incorrect results.
– The programmer must strive at all times to
avoid integer overflow.
10
Types short, long, unsigned
• short - used where conserving storage is a
concern (usually 2 Bytes).
-32 Thousand To
+32 Thousand
-215, -215+1, …, -3, -2, -1, 0, 1, 2, 3, …, 215-1
• long - needed for larger integers (usually 4
bytes).
-2 Billion To
+2 Billion
-231, -231+1, …, -3, -2, -1, 0, 1, 2, 3, …, 231-1 11
• Unsigned - no sign bit, same number
bytes as int.
The range, u, of values:
0  u  2wordsize-1
2 Byte word ~ 0 To +64 Thousand
0, 1, 2, 3, …, 216-1
4 Byte word ~ 0 To +4 Billion
0, 1, 2, 3, …, 232-1
12
Suffix:
u - 20u
l – 20l
ul - 20ul
(case not significant)
IF no suffix with constant - system will choose the
first of : int - long - unsigned long
That can hold the value.
13
The Floating Types
float
double
long double
Hold Real Values.
• Suffixes for constants
f or F
float
3.7 F
l or L
long double
3.7 L
• Any unsuffixed floating constant is of type
double (working type).
14
• Notation:
exponential
decimal
Must have Exponent or Dec pt or Both.
1.234567e5
1.234567e-3
0e0 correct
.e0 wrong
123456.7
0.001234567
0.0
May not contain any blanks or special characters.
• Typical Storage: Float < Double
15
Precision: The number of significant decimal
digits that floating value carries.
Range:
Limits of largest & smallest possible
values that can be in a variable of that
type.
Float:
4 Bytes - about 6 decimal places of
accuracy- single precision.
Double: 8 Bytes - about 15 decimal places
of accuracy- double precision.
16
Internal Representation
FLOAT:
01
89
31
SEEEEEEEE FFFFFF…FF
DOUBLE:
01
11 12
63
SEEEEEEEEEEE FFFFFFFF…FFF
17
Float:
Precision:
6 significant digits.
Range:
10-38 to 10+38
0.d1d2d3d4d5d6  10n
di is a significant digit, N is range.
Double:
Precision: 15 significant digits (252  1015)
Range: 10-308 to 10+308 (2-2048 to 2+2047)
0.123451234512345  103 (15 sig. digits)
18
Note:
1. Not all Real numbers are exactly
representable in binary memory.
2. Floating Arithmetic ops, unlike integer
arithmetic, may not be exact.
19
Compile-Time Operator sizeof
• Unary operator used to find the number of Bytes
needed to store an object.
sizeof(object)
• Object
– Data type
– Expression
int , float, …
a+b
Array
Will cover
later
Structure
20
Assuming that integers are 4 bytes and
doubles are 8 bytes.
double f;
printf("%d ",sizeof (f));
8
printf(''%d", sizeof(int)); 4
21
• Sizeof primarily helps to generate portable
code that depends upon the size of the
built-in data types.
/* Write 6 integers to a disk file. */
void put_rec(int rec[6], FILE *fp)
{
int len;
len = write(fp, rec, sizeof(int)*6);
if(len != 1) printf(''Write Error");
}0
22
sizeof(char) = 1
sizeof(short)  sizeof(int)
 sizeof(long)
sizeof(signed) = sizeof(unsigned)
= sizeof(int)
sizeof(float)  sizeof(double)
 sizeof(long double)
23
Conversions
• When constants and variables of different types
are mixed in an expression, the compiler converts
all operands to the type of the largest operandCalled Type Promotion.
- First, all char and short values are automatically
elevated to int. Called integral promotion.
int + int = int
short + short = int
- Arithmetic Conversion (See pg. 217)
24
char ch;
int i;
float f;
double d;
result= (ch / i) + (f * d) – (f + i);
int
double
float
int
double
float
double
25
Casts
• Casts - Explicit conversions.
(type) expression
(float) i/2
If i is int:
(float) i will change expression value to
26
float, i is not changed.
• Apply to an Expression:
(float) (‘c’ + 3)
• Cannot apply to an Assignment:
(int) f = 3: illegal
• As an unary operator, a cast has the same
precedence as any other unary operator.
(float) i + 3
((float) i) + 3
27
/* print i and i/2 with fractions */
int main(void)
{
int i;
for(i=l; i<=100; ++i)
printf(''%d // 2 is: %f\n", i, (float) i /2);
return 0;
}
28
General forms:
(int) char expression(char) int expression-
(int) float expression(float) int expression(double) float exp-
ordinal value of char
expression
character with the
ordinal value of int
expression
truncates the float
expression
converts int to float
converts float to
double
29