You are on page 1of 45

CSCI 1110

Introduction to Computing
using C

Lecture 6
Fundamental Data Types

The Importance of Data Types


Variable Declarations:
Reserve an appropriate amount of memory.
Ensure correct operations on variables.

Expressions:
Combinations of constants, variables, and
function calls.
Have value and type.

Fundamental data types


Fundamental data types: Long Form
char

signed char

unsigned char

float

signed short int

unsigned short int

double

signed int

unsigned int

long double

signed long int

unsigned long int

Abbreviations:
signed int
short int
long int
unsigned int

int
short
long
unsigned
3

Fundamental data types


Fundamental data types: Short Form
char

signed char

unsigned char

float

short

unsigned short

double

int

unsigned

long double

long

unsigned long

Fundamental data types grouped by functionality


Integral Types

char
short
unsigned short

signed char
int
unsigned

unsigned char
long
unsigned long

Floating Types

float

double

long double

Arithmetic Types Integral types + Floating types


4

1. The Data Type char


What is char?
One of the fundamental data types in C
Representing a single character
Enclosed inside ' '
E.g.: 'A', 'd', '9', '+', '=',

Internal representation of a char


A char can be thought of as an integer value
(compatible with but not the int data type).
E.g.:
'A' is stored as integer value 65
'+' is stored as integer value 43
5

The Data Type char (con't)


What integer value represents what character?
The ASCII table (shown on the next page) defines
the standard mappings between characters to their
corresponding integer values.

ASCII:
"American Standard Code for Information Interchange."

The 7-bit ASCII Table


0

0 NUL SOH STX


10 NL

VT

NP

ETX

EOT ENQ ACK BEL

CR

SO

SI

6
DLE

BS

HT

DC1 DC2 DC3

20 DC4 NAK SYN ETB

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

How does a byte store a char?


Each character is stored in one byte.
One byte 8 bits 28 possibilities
256 distinct values can be represented.
The full range is 0 255. ASCII uses 0 127.
0 0 0 0 0 0 0 0

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

Some character constants and their integer ASCII


values
ASCII of Lowercase
Letters

'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

Nonprinting or hard-to-print characters


Name of character

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

Escape character (\)


Escape sequence (e.g. \n)
11

Some Examples
printf ("%c", 'a');

printf ("%d", 'a');

97

printf ("%c", 97);

printf ("%d", 'a' * 2 - 7);

187

printf ("H\tello");

printf ("\a");

beep

printf ("\"Hello!\"");

"Hello!"

ello

12

2. The Data Type int


, -3, -2, -1, 0, +1, +2, +3,
Computers can store only a finite portion
of natural numbers. That is, upper and
lower limits exist.
Why?

An int is stored in 2 bytes, 4 bytes, or even


larger.
13

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

Finding out the size of an integer


sizeof(int)
Can be used to find out the number of bytes used for
storing an int.
Example below.
1
2
3
4
5
6
7

#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

The Overflow Problem


1
2
3
4
5
6
7
8
9
10
11
12
13

#include <limits.h>
#include <stdio.h>
int main(void)
{
int a, b = INT_MAX;

For using INT_MAX

/* max value of an int */

printf("Size of int = %d bits\n", sizeof(int) * 8);

Variable a overflows
a = b + 1;
printf("a = %d, b = %d\n", a, b);
return 0;
}

Size of int = 32 bits


a = -2147483648, b = 2147483647
18

int variation short int


May take less number of bytes than int.
May thus store a smaller integer value.
For example,
Suppose your computer uses 4-byte int, ranging from
-2147483648 to +2147483647.

A short int may occupy 2 bytes, ranging from


-32768 to +32767.
Why?

19

int variation long int


May take more number of bytes than int.
May thus store a larger integer value.
For example,
Suppose your computer uses 4-byte int, ranging from
-2147483648 to +2147483647.

A long int may occupy 8 bytes, ranging from -(263)


to +(263 - 1).
Why?

20

int variation unsigned int


Take same number of bytes as int.
But stores non-negative integers only.
Range: 0 2int_length 1

2-byte unsigned int:


0 to 216 - 1
0 to 65535.

4-byte unsigned int:


0 to 232 - 1
0 to 4294967295.
21

int variation unsigned int


Note that combinations of short and long with
unsigned are possible, leading to:
unsigned short int

unsigned long int

Exercise
Find out the range of values that can be
represented for
2-byte unsigned short int
8-byte unsigned long int
22

Note the use of unsigned in declaring


variable a.

1
2
3
4
5
6
7
8
9
10
11
12
13

#include <stdio.h>
#define BIG 2000000000

Note the use of %u in the printf.


Overflow is avoided. Why?

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

Size of int = 32 bits


a=4000000000, b=2000000000, c=2000000000
23

3. The Floating Types


What is a floating point value?
For storing real numbers
0.0, -10.2, 3.1416, .244, -.91, 1.

Floating point constants can also be


specified using exponential notation:
1.234567e5
-3.1e-5
6.02e23

123456.7
-0.000031
?
24

3. The Floating Types


Floating point numbers and integers have
different representations.
E.g., floating number 13.25 10:
13.2510
= 1101.012
= 0.1101012 x 24

Mantissa

Exponent

A computer allocates some bits to store the


mantissa and some bits to store the exponent.
25

Precision and Range


Precision number of significant decimal places.
Range largest and smallest values that can be
stored.
Example, float on a particular machine:
Precision: 6 significant figures
Range:
10-38 to 10+38
0.d1d2d3d4d5d6 10n, -38 n +38

The size of mantissa determines the precision.


The size of exponent determines the range.
26

float variation double


Example, double on a particular machine:
Precision: 15 significant figures
Range:
10-308 to 10+308
0.d1d2d3d4dd14d15 10n, -308 n +308

Example, assuming precision as 15 significant figures


x = 123.45123451234544444;

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

Characteristics of Floating Point Numbers

Floating point arithmetic operations may


not be exact.
Try this:
float a = 111111.0, b = 0.1, c;
c = a + b;
printf("%.10f\n", a);
printf("%.10f\n", b);
printf("%.10f\n", c);
111111.0000000000
0.1000000015
11111.1015625000

As a result,
a + b == 111111.1
false (!!!)
Avoid comparing floating
numbers for equality! 29

4. The sizeof Operator


Used to find the number of bytes needed to
store a piece of data.
General form,
sizeof(data_type_or_name)
data_type_or_name can be a valid data type
(such as int, float) or a variable name that
has already been declared.
e.g.
sizeof( long double )
sizeof( i )
30

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

Here are the sizes of some fundamental types:


char: 1
short: 2
int: 4
long: 4
unsigned: 4
float: 4
double: 8
long double: 16

byte
bytes
bytes
bytes
bytes
bytes
bytes
bytes

Note: Execution results vary with different machines and compilers.

32

5. Mixing Data Types in Expressions


HK$1000 + US$100

=?

3.1 + 2

=?

4.0 == 4

true or false?

double d = 4;

What value will d hold?

int x = 4.1;

What value will x hold?

Some kind of conversions is needed to ensure the type of


both operands are compatible before we can evaluate the
expressions.
33

Implicit Type Conversion


C has a set of conversion rules to resolve
certain mismatched operand types.
As a convenience to programmers,
compilers automatically convert the value
of the operands from one type to another
based on these rules whenever possible.
Sometimes called coercion.
34

Arithmetic Conversions (Simplified)


If either operand is a double, the other is converted to
double. The result type is also double.
E.g.:
3.1 + 2

3.1 + 2.0
5.1

(3.1, by default, is of type double)


(2 is converted to 2.0, of type double)
(Result is also of type double)

If both operands are integral types (char, short, int),


then both operands are converted to int. The result type is
also int.
E.g.:

'A' + 'a'

65 + 97

162

35

Floating Integral Types (Simplified)


Converting integral type to double is typically safe.
No warning is given at compile time
double d = 4; /* 4 converted to 4.0 */

Converting a double to an integral type may


result in loss of data.
If the number is within the range of the integral type, the
fractional part is truncated.
Compiler usually warn at compile time. (No guarantee.)

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;

unsigned long ul;

float f;

double d;

long double ld;

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

Explicit Type Conversion (Casting)


Suppose you want to perform
double d = 4.0;
int x = d;

but you don't want the compiler to warn you about


possible lost of data. What can you do?
You can request explicit conversion using the
"type-casting operators":
x = (int)d;
(Converts the value of d to an "equivalent" value of type
int)
38

Type Casting Operator


Syntax:
(new_type) expr
Converts the value of expr to the equivalent
value of type new_type.

39

Using Type Casting Operator (Example)

Average of N integers
/* Consider the following declaration */
int total, N;
double avg;

/* Suppose we have obtained the value of N


* and calculated the total of N integers */
/* Which of these will correctly calculate
* the average? */
avg = total / N;
/* A */
avg = (double)total / N;
/* B */
avg = total / (double)N;
/* C */
avg = (double)(total / N);
/* D */
avg = (double)total / (double)N;
/* E */
40

How are data converted?


(Apply to both implicit and explicit conversions)

Floating types to integral types


Only retain the integer part (no rounding)
e.g.:
int x = (int)4.9;
int y = (int)-3.99;

/* x gets 4 */
/* y gets -3 */

Larger integer types to smaller integer types


Retain only the least significant bits
e.g.: (32-bit integer (int) to 16-bit integer (short))
00000000000000100000000000000011
= 13107510
0000000000000011
= 310
short x = (short)131075;
/* x becomes 3 */
41

Implicit vs. Explicit Conversion


Implicit Casting
The target type is selected according to some rules.
/* Implicitly convert 'A' to 65 (of type int) */
printf("%d", 'A' / 2);
/* Output 32 */

Compilers may warn if conversion is unsafe.


Explicit Casting
Programmers have control over what the target type is.
/* Explicitly convert 'A' to 65.0 (type double) */
printf("%f", (double)'A' / 2);
/* Output 32.5 */

Compilers won't warn even if loss of data is possible.

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

Assumed type of real number constants in


a C program is double.
e.g. d = 2.0; // the number 2.0 is a double
43

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

Understand how floating type value is converted


to integral type value

Knowing how to evaluate and express


expressions containing values of different types.
45

You might also like