Professional Documents
Culture Documents
EE1005-L03-Fundamental Data Types
EE1005-L03-Fundamental Data Types
Thinking to Programming
Lesson 3
Fundamental Data Types
(Reference: Harry H. Cheng, Chapter 3)
Binary Addition: 0 1 1
+0 +0 +1
0 1 10
3-4
Binary Examples
Use the following to convert 8 binary bits
b7b6b5b4b3b2b1b0 to Decimal D:
(b7X27) + (b6X26) + (b5X25) + (b4X24) + (b3X23) +
(b2X22) + (b1X21) + (b0X20) = D
b7b6b5b4 b3b2b1b0
For example, for the binary bits 1100 0001, the
decimal value will be:
(1X128) + (1X64) + (0X32) + (0X16) + (0X8) +
(0X4) + (0X2) + (1X1) = 193
It is not necessary to know how to do this kind of
conversion in this course.
Use your scientific calculator to do conversions, if
needed. 3-5
Units of Data Storage
Binary digit or bit:: 0 or 1
Byte: : comprises 8 bits
Humans Computers
(numbers) (off, on)
0
1
2 Represented
3 by different
bit patterns
4
5
3-10
All Possible Bit Patterns for 7 bits:
Exact Representation of Integers
. . .
3-14
Data Representation (Characters)
Humans Computers
(alphabets) (off, on)
A
Represented
B by different
C bit patterns.
D
But what are
E these
patterns?
3-15
ASCII codes
Each character has a numeric code called the
ASCII code. (Pronounced: askey). The computer
stores this numeric code because it is not able to
store the character shape that we humans use.
3 RS US SP ! “ # $ % & ‘
4 ( ) * + , - . / 0 1
5 2 3 4 5 6 7 8 9 : ;
6 < = > ? @ A B C D E
7 F G H I J K L M N O
8 P Q R S T U V W X Y
9 Z [ \ ] ^ _ ' a b c
10 d e f g h i j k l m
11 n o p q r s t u v w
12 x y z { | } ~ DEL
3-18
Extended ASCII Codes
3-19
Character or Number:
What is 1000001?
x memory cell
ch
To store an actual character such as the lower
case x in ch, we write:
ch = 'x'; //'x' is a character constant
Note that a character, x in this case, 3-21
Conversion specifier: %c
printf("The character is %c", ch);
OUTPUT:
The character is x
OUTPUT:
The character is x
If we change %c to %d:
printf("The character is %d", ch);
unsigned
char 0 to 255
3-24
Data Type: Integers
\n newline
\t horizontal tab
\" double quote
\\ backslash 3-28
Example:
How do we output a double quote character " on the
screen? Like:
This is a double quote ".
It is wrong to write:
printf("This is a double quote ".");
It should be
printf("This is a double quote \".");
" that comes after \ will be interpreted as a double
quote character ". It WILL NOT be interpreted as a
double quote enclosing a string of characters.
Similarly, to output the backslash character \, we
3-29
need to use the escape sequence \\.
Floating-point Numbers
Various types: float, double, long double
float type: represented by 4 bytes = 32 bits
The IEEE Floating-Point Representation divides
the bits in the following way:
0 10000010 10110011000001000000000
3-30
The float Data Type
3-33
The Real Line and Floats
underflow overflow
FLT_MIN FLT_MAX
= 1.2E-38 = 3.4E+38
3-36
Format/Conversion Specifiers
For output, we always need to specify what and
how to output. For example, in the printf()
below:
printf("The root is %f\n", root1);
3-37
Format/Conversion Specifiers
3-38
Float type conversion specifiers
%f, %e, %E
Suppose number = 12.34
%f gives 12.340000 (default 6 decimal places)
%e gives 1.234000e+001 (which is 1.234 x 101)
%E gives 1.234000E+001 (which is 1.234 x 101)
Note that %e and %E are the same except for the
respective display of a lower case e and an upper
case E.
3-39
Float type format/conversion
specifiers
%f, %e
Suppose number = 0.0001234
%f gives 0.000123
%e gives 1.234000e-004
3-40
Format Modifiers
We can specify:
1. A minimum field width
which is the minimum number of spaces to use
for output, e.g.
%5d (i.e. minimum 5 character spaces
are to be used for displaying an integer.)
2. Number of decimal places (for floats)
%m.nf (m is the minimum field width, n
is the number of decimal places)
3. Left justification
Default is right justification.
3-41
Format Modifiers
Examples: Each denotes a
(1) To output 123 space character
3-43
Summary
1. Integer data type
• short, int, long
• char