You are on page 1of 44

EE1005 From Computational

Thinking to Programming

Lesson 3
Fundamental Data Types
(Reference: Harry H. Cheng, Chapter 3)

Dr. Tan Chee Wah, Wesley (CoPaCE/School of EEE)


Email: wesleytan@ntu.edu.sg
Office: S1-B1b-54
Phone: 6790 6009
3-1
“Be Prepared, Give Feedback”
Special Announcement
Please stick to your registered Tutorial Group / Class,
and do not attend another class.

This is because the Computer Laboratory has just


enough computers for the registered students.

Also, you can only take the Continual Assessments in


your registered class.

**Please check your NTU e-mailbox regularly as your


tutor/lecturers will communicate with you via your NTU
email account.

We seek your cooperation in this. Thank you very much!


3-2
Recap: Lecture Lesson 2
Program Structure
/* Comment */
#include <stdio.h> /* load header files*/
int main (void) /* main function */
{
D Declaration of variables /* name, type, size of variables*/

I Initialization of variables /* give values to variables*/

C Data processing and Computation /* operation and


expression*/

O Termination and Output of result /* printf(“xxx”); */

return 0 /* exit a function */


}
3-3
Binary Number System
Computer’s basic design depends on two states:
off and on (symbolized as 0 and 1) in the CPU,
RAM, storage devices, etc.

This 2-state design (off, on) corresponds to the


Binary Number System (base 2) in
mathematics.

Binary digit or bit: 0 or 1

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

1 Kilobyte (KB) = 1024 bytes = 210 bytes


1 Megabyte (MB) = 1024 KB = 220 bytes
1 Gigabyte (GB) = 1024 MB = 230 bytes
1 Terabyte (TB) = 1024 GB = 240 bytes

* Note: the above conversion only applies when


referring to memory size
3-6
Data Types: Integers
Data in computing are stored in various
forms known as data types.

C has the following fundamental data types:


char, int, float, double

Integers (int) are whole numbers (no


decimal point). They are represented
exactly using bit patterns of 0s and 1s.

Example: 20 (=16+4) is represented exactly


by 10100 (see slide 3-5)
3-7
Data Representation (Integers)

Humans Computers
(numbers) (off, on)
0
1
2 Represented
3 by different
bit patterns
4
5

Question: How many bits are required


to represent a number? 3-8
1 bit can only represent 2 cases: 0, 1
Hence we must use more bits to represent
various types of data.

If we use 2 bits, we get 4 possibilities, to


represent 4 different numbers:
00 01 10 11

For 3 bits: 000 001 010 011


100 101 110 111
There are 23 = 8 possibilities  can represent 8
different numbers
3-9
No of Bits Possible states
(to represent different numbers)
4 16 (=24)
6 64 (=26)
7 128 (=27)
8 256
16 65,536
32 4,294,967,296 (=232)

3-10
All Possible Bit Patterns for 7 bits:
Exact Representation of Integers

0000000 (010 : decimal 0)

0000001 (110 : decimal 1)

0000010 (210 : decimal 2)

. . .

1111110 (12610 : decimal 126)

1111111 (12710 : decimal 127)


3-11
Numbers: Integers
C provides several integer types, with different
storage requirements:
short: uses 16 bits = 2 bytes
(short data type can represent 216 = 65536
different numbers, from -32768 to 32767)
int: uses 32 bits = 4 bytes

We also have unsigned short, unsigned int.


Unsigned short: uses 2 bytes (but number range
is from 0 to 65535) 3-12
Characters in Computing
26 x 2 letters (upper & lower case)
10 numeric digits,
punctuation marks,
special characters (~ @ $ #, …)
control characters, etc.

How are these stored in a computer?


Remember that computers can only deal with 0s
and 1s.
3-13
No of Bits Possible states
(to represent different characters)
4 16
6 64
7 128
8 256
16 65,536

Need at least 7 bits to represent characters in


computing. Normally use 8 bits (1 byte).

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.

ASCII = American Standard Code for


Information Interchange
Char A B a b 0 1 + lf sp
ASCII 65 66 97 98 48 49 43 10 32

lf = line feed, sp = space


3-16
The ASCII Table
0 1 2 3 4 5 6 7 8 9

0 NUL SOH STX ETX EOT ENQ ACK BEL BS HT

1 LF VT FF CR SO SI DLE DC1 DC2 DC3

2 DC4 NAK SYN ETB CAN EM SUB ESC FS GS

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

To get equivalent integer value of a character:


3-17
1st Digit is row number, 2nd Digit is column number
E.g. ‘A’ = 65 (from row 6, column 5)
Upper ASCII & UNICODE

For the PC, there is also a set of upper (or


extended) ASCII codes, from 128 to 255
(so needs 8 bits). They represent European
characters and graphics characters.

CJK (Chinese, Japanese and Korean)


character sets need 16 bits – UNICODE
which includes characters/symbols from all
major languages in the world.

3-18
Extended ASCII Codes

3-19
Character or Number:
What is 1000001?

Using the conversion method from slide 3-5:


b6b5b4 b3b2b1b0

1000001 (binary) = 64 + 1 = 65 (decimal)


The bit string 1000001 represents both
character ‘A’ and the integer 65. In fact, it may
also represent an instruction or part of an
instruction.
How does the computer know which is which?
The context, defined by the program,
determines the correct meaning.
3-20
Data Type: Characters
Declaration of a char variable: char ch;
The computer will allocate a 1-byte cell (with
the name ch) in RAM.

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

must be enclosed with ' '


Data Type: Characters

Conversion specifier: %c
printf("The character is %c", ch);

OUTPUT:
The character is x

String constant: comprises a list of characters,


enclosed with " "

Example: "FE1008 Computing"


3-22
Conversion Specifier
char ch = 'x'; (ASCII value = 120, or 11110002)

printf("The character is %c", ch);

OUTPUT:
The character is x

If we change %c to %d:
printf("The character is %d", ch);

the output is (the associated ASCII integer value)


The character is 120
3-23
Char data type and numeric
values
The char data type is also one of C’s integer
data types.
Type Meaning Value Range
char character -128 to 127

unsigned
char 0 to 255

3-24
Data Type: Integers

Type Meaning Value Range

char character -128 to 127


short short integer -32,768 to 32,767
int integer -2,147,483,648 to
2,147,483,647
long long integer Same as int(not always)
unsigned
char 0 to 255
unsigned
short 0 to 65535
3-25
Falling off the Edge?
Range for short int: -32,768 to 32,767

Suppose we have: 32767+1 wraps around


and becomes -32768
short old_bal = 32767, new_bal;
new_bal = old_bal + 1;

What do you think will happen if we execute


printf("New balance = %d", new_bal); ?

Output: New balance = -32768


When max value 32767 is reached,
32767+1 wraps around and goes back to 3-26
the beginning (i.e. -32768)
The storage sizes of short, int, long are
system dependent.
On some, sizes of short and int are the same:
2 bytes.
For CodeBlocks, sizes of int and long: 4 bytes.
To find out the correct size of a data type on a
system, use sizeof().
printf("The size of int is %d bytes.",
sizeof(int));
Output (CodeBlocks):
The size of int is 4 bytes.
3-27
Escape Sequences

\n: an escape sequence, \: escape character

Some common escape sequences are listed


below:
Sequence Meaning

\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:

Sign 8-bit 23-bit mantissa


bit exponent

0 10000010 10110011000001000000000
3-30
The float Data Type

This representation gives the following range of


values:
1.2E-38 to 3.4E+38
with a corresponding negative range.
Here, E-38 means 10-38.
Because of the limited number of bits allocated
for the mantissa (i.e., the fractional part), a
number of the float type only has 6 or 7 digits
of precision (which is commonly referred to as
single-precision).
3-31
Which real numbers can be
represented exactly?
An example:
0 10000001 01110000000000000000000
= 5.7500000 (decimal)
The next larger number is:
0 10000001 01110000000000000000001
= 5.750000476837158203125 (decimal)
These two decimal numbers can be represented
exactly but there are infinitely many real
numbers (e.g. 5.7500001, 5.75000023) between
these two numbers, and these other numbers
cannot be represented exactly. 3-32
Overflow & Underflow
When the result of a computation is greater than
the largest floating point value (about
3.4E+38) , we get an overflow condition.
This usually indicates that something is wrong
because 1038 is a huge number which is not
commonly encountered in applications.
If smaller than 1.2E-38, we get an underflow
condition.

3-33
The Real Line and Floats

underflow overflow

FLT_MIN FLT_MAX
= 1.2E-38 = 3.4E+38

Note: FLT_MIN and FLT_MAX are macros


defined in the system (in float.h).
3-34
More accuracy & wider range
Double: 8 bytes = 64 bits

Sign 11-bit 52-bit mantissa


bit exponent

Range: 2.2E-308 to 1.8E+308 (-ve range also)


Gives 14 or 15 digits of precision (double-
precision).
It is the default data type for floating-point
numbers. A numeric constant with a decimal point
(such as 2.0) is considered as a double. 3-35
What ANSI C says

ANSI C only guarantees (accuracy wise) that


• a double is greater than or equal to a
float, and
• a long double is greater than or equal to
a double.

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);

the format string "The root is %f\n" is for


this purpose. %f is known as a conversion or
format specifier.
We have used conversion specifiers such as %d
and %c. There are several others.

3-37
Format/Conversion Specifiers

Character Argument Output


%d integer signed decimal integer
%f float signed floating point no.
%e float float using e notation
%E float float using E notation
%c character a single character
%% % sign

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

Note: Use %e if you are not sure about the size


of output numbers.

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

(a) using %5d gives [ ][ ]123 (right justification)


(b) using %-5d gives 123[ ][ ] (left justification)

(2) Suppose the number is 12.34


%8.3f ________
(A total of 8 space are to be used)
Try %1.3f, %0.3f, %.3f (If the field
width is too small, the computer will ignore it.)
See Practical Lesson 3 (printf.c)
3-42
Outputting the % sign
To output the percentage sign %, we need to
type two % signs:

printf("This is the %% sign.");

Note: Just like the backslash (\) character,


% also has a special meaning in the
printf() statement. As you have seen, it is
used in the form %c, %d, %f, etc.

3-43
Summary
1. Integer data type
• short, int, long
• char

2. Escape sequence using \

3. Floating Point data type: float, double

4. Format specifiers and modifiers

5. How to output a % sign


3-44

You might also like