Data Types

advertisement
1
Simple Data Types
Integer Types (whole numbers)
Type
Range
Short
unsigned short
int
unsigned int
long
unsigned long
-32767
0
-2147483647
0
-2147483647
0
to
to
to
to
to
to
Data Bits
32767
65535
2147483647
4294967295
2147483647
4294967295
16
16
32
32
32
32
Floating-Point Types (real numbers)
Type
Range
float
double
long double
3.4e-38
to 3.4e+38
1.7e-308 to 1.7e+308
3.4e-4932 to 3.4e+4932
Significant
Digits
Data
Bits
6
15
19
32
64
80
Representational Error (round-off error)
When representing real numbers as integers.
When using real numbers with more than the available significant digits.
When using real number fractions that are not exact base-2 fractions.
Cancellation error
When adding or subtracting large and small real numbers.
This invalidates the part of the sum or difference that is smaller than
the least significant digit of the larger number.
Can be a very serious problem with numerical integration or trigonometric
functions of angles near 0 or 90 degrees.
2
Example
This code will print:
a = 100000000, b = 2, c = 100000000, c-a = 0
x = 100000000, y = 2, z = 100000002, z-x = 2.
#include<stdio.h>
void main()
{
float a = 1e8, b = 2, c;
double x = 1e8, y = 2, z;
c = a + b;
z = x + y;
printf(“\n a = %0.0f, b = %0.0f, c = %0.0f,”
“ c-a = %0.0f”, a, b ,c , c-a);
printf(“\n x = %0.0lf, y = %0.0lf, z = %0.0lf,”
“ z-x = %0.0lf”, x, y ,z , z-x);
}
Overflow error
When the magnitude of a floating-point number is greater than allowed.
Magnitude > 3.4e+38
Magnitude > 1.7e+308
Magnitude > 3.4e+4932
for type float.
for type double.
for type long double
Causes a run-time error.
Underflow error
When the non-zero magnitude of floating-point number is smaller than
allowed.
0 < magnitude < 3.4e-38
for type float.
0 < magnitude < 1.7e-308 for type double.
0 < magnitude < 3.4e-4932 for type long double.
Causes a run-time warning.
3
Automatic Conversion of Data Types
Binary operations automatically convert the less detailed numerical data
type to the more detailed data type.
An assignment statement automatically converts the numerical data on the
right side to the numerical data type on the left side.
Most library function calls automatically convert the less detailed
numerical data in the argument to the more detailed data type needed by the
function.
Example
When
int k;
float c;
double x, y;
then
k/c
becomes a 6-digit float type.
k/x
becomes a 15-digit double type.
c/x
becomes a 15-digit double type.
k = 10/3.0; stores 3 in k.
c = 10/3.0; stores 3.33333 in c.
y = 10/3.0; stores 3.33333333333333 in y.
sin(k) converts k to the double type needed by sin.
sin(c) converts c to the double type needed by sin.
4
Explicit Data Type Conversion (Type Casting )
To cast (interpret) the data in a constant or variable as a new data type,
precede the constant or the variable name by the new data type in
parentheses. The data type of the constant or variable is not changed.
Form
(type)var
where type
var
cons
or
(type)cons
is a data type.
is a variable name.
is a constant value.
Example
The following code printed:
int
double
float
aa = 999999910
aa = 999999910
aa = 999999936
#include<stdio.h>
void main()
{
int aa = 999999910;
printf(“\n int
aa = %d”, aa);
printf(“\n double aa = %9.0lf”, (double)aa);
printf(“\n float
aa = %9.0f”, (float)aa );
}
5
Character Representation
Character constants and variables are represented by 16-bit integers.
Type
Range
char
unsigned char
-32768
0
to
to
32767
65535
Collating Sequence
The assignment of integers to represent characters is called a collating
sequence. The ASCII collating sequence is shown in Appendix A.
In the ASCII collating sequence:
The numbers
The letters
The letters
0 to 9
A to Z
a to z
are assigned integers
are assigned integers
are assigned integers
48 to 57.
65 to 90.
97 to 122.
Use a type cast to find:
The collating sequence integer assigned to a given character.
The character assigned to a given collating sequence integer.
Example
This code uses type casts to print:
The ASCII code for the letter b is 98
ASCII code 75 represents the letter K
#include<stdio.h>
void main()
{
printf(“\n The ASCII code for the letter %c"
" is %d”, 'b', (int)'b');
printf(“\n ASCII code %d represents”
“ the letter %c", 75, (char)75);
}
Download