Professional Documents
Culture Documents
Introduction to Computing
using C
Lecture 6
Fundamental Data Types
Expressions:
Combinations of constants, variables, and
function calls.
Have value and type.
signed char
unsigned char
float
double
signed int
unsigned int
long double
Abbreviations:
signed int
short int
long int
unsigned int
int
short
long
unsigned
3
signed char
unsigned char
float
short
unsigned short
double
int
unsigned
long double
long
unsigned long
char
short
unsigned short
signed char
int
unsigned
unsigned char
long
unsigned long
Floating Types
float
double
long double
ASCII:
"American Standard Code for Information Interchange."
VT
NP
ETX
CR
SO
SI
6
DLE
BS
HT
CAN EM
SUB ESC FS
GS
30 RS
US
SP
"
&
'
40 (
50 2
60 <
>
70 F
80 P
90 Z
100 d
110 n
120 x
DEL
7
ASCII
ASCII stands for "American Standard
Code for Information Interchange."
Some are printable characters:
'A', '=', ':',
Some are non-printable characters:
newline (NL), bell (BEL), tab (HT, VT),
8
NUL
0 0 0 0 0 0 0 1
SOH
0 0 0 0 0 0 1 0
STX
0 1 0 0 0 0 0 1
65
0 1 0 0 0 0 1 0
66
127
DEL
0 1 1 1 1 1 1 1
'a'
97
'b'
98
'c'
99
'z'
122
ASCII of Uppercase
Letters
'A'
65
'B'
66
'C'
67
'Z'
90
ASCII of Digits
'0'
48
'1'
49
'2'
50
'9'
57
ASCII of Other
Characters
' '
32
'#'
38
'*'
42
'+'
43
10
ASCII Value
alert
Written in C
\a
backslash
\\
92
backspace
\b
carriage return
\r
13
double quote
\"
34
form feed
\f
12
horizontal tab
\t
newline
\n
10
null character
\0
single quote
\'
39
vertical tab
\v
11
Some Examples
printf ("%c", 'a');
97
187
printf ("H\tello");
printf ("\a");
beep
printf ("\"Hello!\"");
"Hello!"
ello
12
2-byte int
2 bytes , 16 bits
216 = 65536 distinct values can be stored.
Half for negative integers, and half for nonnegative integers.
-(215), -(215) + 1, , -2, -1, 0, 1, 2, , 215 - 1
That is,
-32768, -32767, ., -2, -1 0, 1, 2, , 32767
14
4-byte int
4 bytes , 32 bits
232 = 4294967296 distinct values can be stored.
Half for negative integers, and half for non-negative
integers.
-(231), -(231) + 1, , -2, -1, 0, 1, 2, , 231 - 1
That is,
-2147483648, , -2, -1, 0, 1, 2, , 2147483647
Exercise
Find out the range of values that can be stored if a certain
computer uses 8 bytes to store one single integer value.
15
#include <stdio.h>
int main(void)
{
printf("size of int
return (0);
}
size of int
= %d\n", sizeof(int) );
= 4
16
Integer Overflow
Integer overflow occurs when the result of an
arithmetic operation is too large to be
represented by the underlying integer
representation.
E.g., assume 8-bit integers are used
(Range: -128, , -1, , 0, 1, , 127)
Add one to the largest positive integer:
127 + 1 128 Overflow!
17
#include <limits.h>
#include <stdio.h>
int main(void)
{
int a, b = INT_MAX;
Variable a overflows
a = b + 1;
printf("a = %d, b = %d\n", a, b);
return 0;
}
19
20
Exercise
Find out the range of values that can be
represented for
2-byte unsigned short int
8-byte unsigned long int
22
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <stdio.h>
#define BIG 2000000000
int main(void)
{
unsigned int a, b=BIG, c=BIG;
printf("Size of int = %d bits\n", sizeof(int) * 8);
a = b + c;
printf ("a=%u, b=%u, c=%u\n", a, b, c);
return(0);
}
123456.7
-0.000031
?
24
Mantissa
Exponent
results in
0.123451234512345 103
27
Important
Not all real numbers are representable.
Floating arithmetic operations need not be
exact.
For very large computations, rounding
errors may accumulate and become
significant.
28
As a result,
a + b == 111111.1
false (!!!)
Avoid comparing floating
numbers for equality! 29
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <stdio.h>
int main(void)
{
printf("\n");
printf("Here are the sizes of some fundamental types:\n\n");
printf("
char:%3d byte \n", sizeof(char));
printf("
short:%3d bytes\n", sizeof(short));
printf("
int:%3d bytes\n", sizeof(int));
printf("
long:%3d bytes\n", sizeof(long));
printf("
unsigned:%3d bytes\n", sizeof(unsigned));
printf("
float:%3d bytes\n", sizeof(float));
printf("
double:%3d bytes\n", sizeof(double));
printf("long double:%3d bytes\n", sizeof(long double));
printf("\n");
return (0);
}
31
byte
bytes
bytes
bytes
bytes
bytes
bytes
bytes
32
=?
3.1 + 2
=?
4.0 == 4
true or false?
double d = 4;
int x = 4.1;
3.1 + 2.0
5.1
'A' + 'a'
65 + 97
162
35
int x = 4.1;
x = 2 * 4.1;
x = 12345678901.2;
/* 4.1 converted to 4 */
/* 8.2 converted to 8 */
/* behavior undefined */
36
Declaration
char c;
short s;
int i;
unsigned u;
float f;
double d;
long l;
Expression
Type
Expression
Type
c s / i
int
u * 7 - i
unsigned
u * 2.0 - i
double
f * 7 - i
float
c + 3
int
7 * s * ul
unsigned long
c + 5.0
double
ld + c
long double
d + s
double
u ul
unsigned long
2 * i / l
long
u - l
System dependent
37
39
Average of N integers
/* Consider the following declaration */
int total, N;
double avg;
/* x gets 4 */
/* y gets -3 */
42
Default Type
Assumed type of integer constants in a C
program is normally int.
e.g. a = 3 + 109700; // both 3 and 109700 are int
Suffixes
A suffix can be appended to a numerical
constant to specify its type explicitly.
Suffix
u or
Type
unsigned
Example
37U
long
37L
ul or
UL
unsigned long
37UL
or
float
3.7F
or
long double
3.7L
or
44
Summary
Be aware of the range of different integral types
to avoid integer overflow.
Understand the limitation of floating types
Inexactness of floating point representation and floating
point arithmetic