2. Objectives
• Be able to explain to others what a data type is
• Be able to use basic data types in C programs
• Be able to see the inaccuracies and limitations introduced
by machine representations of numbers
CoxSimple Data Types2
6. CoxSimple Data Types6
Everything is Just a Bunch of Bits
• Bits can represent many different things
• Depends on interpretation
• You and your program must keep track of what kind of data
is at each location in the computer’s memory
• E.g., program data types
7. CoxSimple Data Types7
Big Picture
• Processor works with finite-sized data
• All data implemented as a sequence of bits
• Bit = 0 or 1
• Represents the level of an electrical charge
• Byte = 8 bits
• Word = largest data size handled by processor
• 32 bits on most older computers
• 64 bits on most new computers
8. CoxSimple Data Types8
Data types in C
•Only really four basic types:
• char
• int (short, long, long long, unsigned)
• float
• double
•Size of these types on
•CLEAR machines:
•Sizes of these types
•vary from one machine
•to another!
Type Size (bytes)
char 1
int 4
short 2
long 8
long long 8
float 4
double 8
9. CoxSimple Data Types9
Characters (char)
• Roman alphabet, punctuation, digits, and other symbols:
• Encoded within one byte (256 possible symbols)
• ASCII encoding (man ascii for details)
• In C:
char a_char = ’a’;
char newline_char = ’n’;
char tab_char = ’t’;
char backslash_char = ’’;
10. CoxSimple Data Types10
ASCII
From “man ascii”:
| 0 NUL| 1 SOH| 2 STX| 3 ETX| 4 EOT| 5 ENQ| 6 ACK| 7 BEL|
| 8 BS | 9 HT | 10 NL | 11 VT | 12 NP | 13 CR | 14 SO | 15 SI |
| 16 DLE| 17 DC1| 18 DC2| 19 DC3| 20 DC4| 21 NAK| 22 SYN| 23 ETB|
| 24 CAN| 25 EM | 26 SUB| 27 ESC| 28 FS | 29 GS | 30 RS | 31 US |
| 32 SP | 33 ! | 34 " | 35 # | 36 $ | 37 % | 38 & | 39 ' |
| 40 ( | 41 ) | 42 * | 43 + | 44 , | 45 - | 46 . | 47 / |
| 48 0 | 49 1 | 50 2 | 51 3 | 52 4 | 53 5 | 54 6 | 55 7 |
| 56 8 | 57 9 | 58 : | 59 ; | 60 < | 61 = | 62 > | 63 ? |
| 64 @ | 65 A | 66 B | 67 C | 68 D | 69 E | 70 F | 71 G |
| 72 H | 73 I | 74 J | 75 K | 76 L | 77 M | 78 N | 79 O |
| 80 P | 81 Q | 82 R | 83 S | 84 T | 85 U | 86 V | 87 W |
| 88 X | 89 Y | 90 Z | 91 [ | 92 | 93 ] | 94 ^ | 95 _ |
| 96 ` | 97 a | 98 b | 99 c |100 d |101 e |102 f |103 g |
|104 h |105 i |106 j |107 k |108 l |109 m |110 n |111 o |
|112 p |113 q |114 r |115 s |116 t |117 u |118 v |119 w |
|120 x |121 y |122 z |123 { |124 | |125 } |126 ~ |127 DEL|
Special
control
characters
11. CoxSimple Data Types11
Characters are just numbers
•What does this function do?
Char
fun(char c)
{
char new_c;
if ((c >= ’A’) && (c <= ’Z’))
new_c = c - ’A’ + ’a’;
else
new_c = c;
return (new_c);
}
return type
procedure
name
argument type
and name
local variable
type and name
Math on
characters!
comparisons
with characters!
12. CoxSimple Data Types12
FP vs. Integer Results
int i = 20 / 3;
float f = 20.0 / 3.0;
True mathematical answer: 20 3 = 6 2/3
i = ?
f = ?
6
6.666667
Integer division ignores remainder
FP arithmetic rounds result
13. CoxSimple Data Types13
Integer Representations
Base 2 Base 16 Unsigned 2’s Comp.
0000 0 0 0
0001 1 1 1
0010 2 2 2
0011 3 3 3
0100 4 4 4
0101 5 5 5
0110 6 6 6
0111 7 7 7
1000 8 8 -8
1001 9 9 -7
1010 A 10 -6
1011 B 11 -5
1100 C 12 -4
1101 D 13 -3
1110 E 14 -2
1111 F 15 -1
0000 0001
0010
0011
0100
0101
0110
011110001001
1011
1100
1101
1010
1110
1111
0
1
2
3
4
5
6
789
A
D
C
B
E
F
0 1
2
3
4
5
6
789
10
11
12
13
14
15
0 1
-8
3
4
5
6
7
2
-7
-6
-5
-4
-3
-2
-1
Why one more negative than positive?
14. CoxSimple Data Types14
Integer Representations
Math for n bits:
Define
1
0
2)(2
n
i
i
i
xxUB
2
0
1
1
22)(2
n
i
i
i
n
n
xxxTB
01 xxx n
sign bit
0=non-negative
1=negative
Base 2 Base 16 Unsigned 2’s Comp.
0000 0 0 0
0001 1 1 1
0010 2 2 2
0011 3 3 3
0100 4 4 4
0101 5 5 5
0110 6 6 6
0111 7 7 7
1000 8 8 -8
1001 9 9 -7
1010 A 10 -6
1011 B 11 -5
1100 C 12 -4
1101 D 13 -3
1110 E 14 -2
1111 F 15 -1
15. 15
Memory Layout of Records
initials
(2 bytes)
married
(1 byte)
ss_number
(4 bytes)
• Fields are stored adjacently in memory.
• Memory is allocated for records based on
the order the fields are created.
• Variables are aligned for easy reference.
initials
(2 bytes)
ss_number
(4 bytes)
married
(1 byte)
Optimized for space Optimized for memory alignment
4 bytes / 32 bits 4 bytes / 32 bits