02a. Data Types-2
02a. Data Types-2
1
Outline
1. Motivation
2. Integral (Integer) Types
3. Floating Point Numbers
4. Type Conversion
5. Back to the Original Example
2
1. Motivation
• After our previous lecture, you should be able to have intuitive understanding of
the following simple C program
1 #include <stdio.h>
2
3 int main(void)
4 {
5 int area, h, b;
6
7 h = 3;
8 b = 4;
9 printf("Height and base of triangle: %d, %d\n", h, b);
10 area = (1/2) * h * b;
11 printf("Area of triangle: %d\n", area);
12
13 return 0;
14 }
3
1. Motivation
• This C program, however, do not produce the expected result. Why?! All the
formula looks alright to you, right?
1 #include <stdio.h>
2
3 int main(void)
4 {
5 int area, h, b;
6
7 h = 3;
8 b = 4;
9 printf("Height and base of triangle: %d, %d\n", h, b);
10 area = (1/2) * h * b;
11 printf("Area of triangle: %d\n", area);
12
13 return 0;
14 }
4
1. Motivation
• To understand the problem of the above program, you need to
learn more about data types
• In the previous lecture, we have introduced the integer data type:
int x = 0;
double pi = 3.14;
5
2. Integral (Integer) Types
Key concepts
• The smallest/largest integer values of type int
• Integer overflow
6
2.1. Basic Building Block: Bits
• Ultimately, a computer represent data in bits.
• 1 byte == 8 bits
And call them -128, -127, -126, -125, …, -2, -1, 0, 1, 2,3, …, 125, 126, 127
The patterns are used for representing both –ve, 0 and +ve integers.
8
2.1. From Bits to Integers
• With N bits, we can represent 2N distinct values.
– Half for negative integers, and half for non-negative integers:
-(2N-1), -(2N-1 - 1), …, -2, -1, 0, +1, +2, …, +(2N-1 - 1)
9
2.2. Variations of Integral Types
Type Size in bytes Range
[Replit (2021)]
signed char 1 -128 to 127 (a signed byte)
short (or short int) 2 -215 to 215-1 (-32768 to 32767)
int 4 -231 to 231-1 (if 4 bytes)
long (or long int) 8 -263 to 263-1 (if 8 bytes)
unsigned char 1 0 to 255 (an unsigned byte)
unsigned short 2 0 to 216-1 (0 to 65535)
unsigned int
4 0 to 232-1 (if 4 bytes)
(or unsigned)
unsigned long 8 0 to 264-1 (if 8 bytes)
10
2.2. Variations of Integral Types
• What is the appropriate type to represent integers
in this program?
– When the amount of data to be processed is large and
the memory space is scarce, we have to be mean.
11
2.3. Integer Overflow
• Integer overflow occurs when the result of an
arithmetic operation is too large to be represented by
the underlying integer representation.
2147483647 + 1 → –2147483648
12
3. Floating Point Numbers
Key Concepts
• Floating point numbers representation and arithmetic are
not exact.
13
3.1. Floating Point Number Representation
• Floating point numbers and integers have different
representations.
16
3.3. Using floating point with printf()
• You should now realize that integers and floating
point numbers are completely different things in C
language
• You must specify correctly what data type you are
supplying or you will see meaningless results
• What do you think the result will be in the
following example?
e.g. printf("%d", 3.14);
17
3.4. Using floating point with scanf()
1 #include <stdio.h>
2
3 int main(void)
4 { Variables r1 and r2 are of type double
5 double r1, r2;
6
7 printf("Enter two real numbers:\n");
8 scanf("%lf%lf", &r1, &r2);
Concerning double-typed values,
9
10 printf("r1 = %f\n", r1); scanf() uses %lf (‘ell’ f);
11 printf("r2 = %f\n", r2); printf() uses %f.
12 Look similar but slightly different!
13 return 0;
14 }
19
4.1. Expressions with mixed types of data
• HK$1000 + US$100 =?
• 3.1 + 2 =?
20
4.1. Implicit Type Conversion
• C language has a set of conversion rules to resolve
certain mismatched operand types.
21
4.1. Arithmetic Conversions (Simplified Rules)
• 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 is of type double)
= 3.1 + 2.0 (Therefore, 2 is converted to 2.0)
= 5.1 (Result is of type double)
22
4.2. Converting Integral Type to double
• Converting integral type to double is safe.
– No warning is given at compile time
double d;
d = 4;
/* 4 is converted to 4.0,
and then 4.0 is assigned to d */
23
4.2. Converting double to Integral Type
• 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, i.e. discarded.
– Compilers usually warn at compile time (but NOT guaranteed.)
24
4.3. Explicit Type Conversion (Casting)
Syntax: (new_type) operand
25
4.4. Type Conversion (Examples)
1 int x = 5, y = 2;
2 double a, b;
3 a = 2.5 + (x / y); /* R.H.S. is evaluated as
4 2.5 + (5 / 2)
5 => 2.5 + 2
6 => 2.5 + 2.0
7 => 4.5
8 */
9
10 b = 2.5 + ((double)x / y); /* R.H.S. is evaluated as
11 2.5 + (5.0 / 2)
12 => 2.5 + (5.0 / 2.0)
13 => 2.5 + 2.5
14 => 5.0
15 */
26
4.4. Type Conversion (Examples)
1 int x, y;
2 double a = 2.6, b = 2.4;
3 x = (int)(a + 0.5); // x is assigned 3
4 y = (int)(b + 0.5); // y is assigned 2
Example 4.2. Rounding floating point numbers to nearest integer
27
4.4. Using Type Casting Operators (Exercise)
• 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
28
4.5. How are numbers converted?
(Apply to both implicit and explicit conversions)
29
5. Back to the Original Example
• Given your understanding of data types now, what is wrong with our original
example?
1 #include <stdio.h>
2
3 int main(void)
4 {
5 int area, h, b;
6
7 h = 3;
8 b = 4;
9 printf("Height and base of triangle: %d, %d\n", h, b);
10 area = (1/2) * h * b;
11 printf("Area of triangle: %d\n", area);
12
13 return 0;
14 }
30
5. Back to the Original Example
• Given your understanding of data types now, what is wrong with our original
example?
1 #include <stdio.h>
2
3 int main(void)
4 { Are we using the proper
5 int area, h, b; type? Are these variables
6 always integers?
How about this? What are
7 h = 3; we telling printf?
8 b = 4;
9 printf("Height and base of triangle: %d, %d\n", h, b);
10 area = (1/2) * h * b;
11 printf("Area of triangle: %d\n", area);
12 Does 1/2 give integer as result,
13 return 0; or floating point? Should we
14 } use, say, 0.5 instead?
31
Summary
• All number types have an intended purpose,
precision, and range.
– Choose a proper data type to represent data
– Beware of and prevent overflow
33
Reading Assignment
• C: How to Program, 8th ed, Deitel and Deitel
• Appendix C Number Systems
34
Reminder: PreLabs are Ready!
• Every Mon afternoon we will release the PreLabs
– Meant to help you prepare for the lab
– Due Wed 9:30am – Please try it after the lecture and
submit before Wed!
– Don't worry – it's super easy (takes < 30 min) and it's
very easy marks to get! Don't forget!
35