Bài giảng Introduction to Computer Programming (C language) - Chapter 3: Variables and basic data types - Võ Thị Ngọc Châu
Ho Chi Minh City University of Technology
Faculty of Computer Science and Engineering
Chapter 3: Variables and
Basic Data Types
Introduction to Computer Programming
(C language)
TS. Võ Thị Ngọc Châu
2017 – 2018, Semester 2
Course Content
C.1. Introduction to Computers and
Programming
C.2. C Program Structure and its
Components
C.3. Variables and Basic Data Types
C.4. Selection Statements
C.5. Repetition Statements
C.6. Functions
C.7. Arrays
C.8. Pointers
C.9. File Processing
2
References
[1] “C: How to Program”, 7th Ed. – Paul
Deitel and Harvey Deitel, Prentice Hall, 2012.
[2] “The C Programming Language”, 2nd Ed.
– Brian W. Kernighan and Dennis M. Ritchie,
Prentice Hall, 1988
and others, especially those on the Internet
3
Content
Introduction
Data and Data Types
enum Data Type
struct Data Type
Variables and Variable Declaration
Constant Definition
Expressions
Operators
Summary
4
Introduction
Algorithm findMinNumber
-
-
-
-
Input: positiveNumber[n] which is an array of n positive double values
Output: minNumber which is the smallest one whose type is double
Purpose: find the smallest number in a collection
Given a
set of n
positive
numbers,
find the
smallest
one.
Precondition: n data inputs are positive.
Begin Algorithm
Check positiveNumber[n] contains only positive values
minNumber = positiveNumber[1]
iteration = 2
While (iteration <= n)
Begin While
If (minNumber <= positiveNumber[iteration]) Then
iteration = iteration + 1
Else
Begin
(Chapter 1 –
minNumber = positiveNumber[iteration]
iteration = iteration + 1
Pseudo code)
End
End While
5
Introduction
void main() {
Given a
double positiveNumber[10] = {2, 1, 3, 10, 8, 3, 4, 5, 9, 12};
set of n
positive
numbers,
find the
smallest
one.
int n = 10;
double minNumber = positiveNumber[0];
int iteration = 1;
while (iteration < n) {
if (minNumber <= positiveNumber[iteration])
iteration = iteration + 1;
else {
minNumber = positiveNumber[iteration];
iteration = iteration + 1;
(Chapter 1 –
}
}
Real code in C)
}
6
Introduction
void main() {
Given a
double positiveNumber[10] = {2, 1, 3, 10, 8, 3, 4, 5, 9, 12};
set of n
positive
numbers,
find the
smallest
one.
int n = 10;
double minNumber = positiveNumber[0];
int iteration = 1;
/* Variable declarations */
while (iteration < n) {
if (minNumber <= positiveNumber[iteration])
iteration = iteration + 1;
else {
minNumber = positiveNumber[iteration];
iteration = iteration + 1;
(Chapter 1 –
}
}
Real code in C)
}
7
Introduction
void main() {
Given a
INPUT
set of n
positive
numbers,
find the
smallest
one.
OUTPUT
SUPPORTING ONES
/* Variable declarations */
while (iteration < n) {
if (minNumber <= positiveNumber[iteration])
iteration = iteration + 1;
else {
minNumber = positiveNumber[iteration];
iteration = iteration + 1;
}
(Chapter 1 –
}
Real code in C)
}
8
Introduction
Handle data in a C program
Input
What?
Output
Supporting ones
Declare, Store, Process (Manipulate)?
Who declares?
Where?
Who stores?
Who processes (manipulates)?
9
Introduction
Handle data in a C program
Input
What?
Output
Values + Types
(Our real world)
Supporting ones
Declare, Store, Process (Manipulate)?
Who declares? - We
Where?
Memory
(Computer’s world)
Who stores? - Computer
Who processes (manipulates)? - Computer
10
Data and Data Types
Data
“information, especially facts or numbers, collected
for examination and consideration and used to
help decision-making, or information in an
electronic form that can be stored and processed
by a computer” – Cambridge Dictionary
Examples:
9.5
“Introduction to Computer Programming”
„A‟
(“59800172”, “Chau”, “IT”, 1998)
(2, 5, 10, 3, 8, 9, 4, 1, 7, 6)
1
1, 2, 3, …
11
Data and Data Types
Data
“information, especially facts or numbers, collected
for examination and consideration and used to
help decision-making, or information in an
electronic form that can be stored and processed
by a computer” – Cambridge Dictionary
Examples:
9.5
Value
Meaning
Type
“Introduction to Computer Programming”
„A‟
(“59800172”, “Chau”, “IT”, 1998)
(2, 5, 10, 3, 8, 9, 4, 1, 7, 6)
Purpose
Role
1
1, 2, 3, …
12
Data and Data Types
Data
“information, especially facts or numbers, collected
for examination and consideration and used to
help decision-making, or information in an
electronic form that can be stored and processed
by a computer” – Cambridge Dictionary
Examples:
Grade
9.5
Value
Name
Grade
“Introduction to Computer Programming”
„A‟
(“59800172”, “Chau”, “IT”, 1998)
(2, 5, 10, 3, 8, 9, 4, 1, 7, 6)
Meaning
Type
Student‟s info
Stock levels
Purpose
Role
The lowest stock level
Iterations for problem solving
and so on
1
1, 2, 3, …
Data and Data Types
Data
“information, especially facts or numbers, collected
for examination and consideration and used to
help decision-making, or information in an
electronic form that can be stored and processed
by a computer” – Cambridge Dictionary
Types (atomic, non-atomic):
- Real numbers
Grade
Name
Grade
Value
- Sequences of characters
puter Programming”
- Characters
Meaning
Type
- Records
- Sets of elements
Student‟s info
, “IT”, 1998)
- Integer numbers
1, 7, 6)
Stock levels
- Natural numbers
Purpose
Role
The lowest stock level
- …
Iterations for problem solving
and so on
Value sets + data manipulations
14
Storage
Data and Data Types
Data
“information, especially facts or numbers, collected
for examination and consideration and used to help
decision-making, or information in an electronic
form that can be stored and processed by a
computer” – Cambridge Dictionary
Data Types
Represent data processed in a computer-based
program
Specify what kind of data is to be stored in memory
How much memory should be allocated
How to interpret the bits stored in the memory
Specify what operations are allowed for data
15
manipulation
Data Types in C
Built-in data types (primitive/fundamental)
char (signed char), unsigned char
short int, unsigned short, int, unsigned int, long int,
unsigned long int, long long int, unsigned long long
float, double, long double
void
enum (enumerated data associated with integers)
Derived data types
arrays [] of objects of a given type
pointers * to objects of a given type
structures struct containing objects of other types
union containing any one of several objects of
16
various types
Built-in Data Types in C
Type
Keyword
char
Size
Range
Characters
1 byte
1 byte
2 bytes
-128 to 127
0 to 255
Unsigned characters
Short integer numbers
unsigned char
short
-32768 to 32767
Unsigned short integer numbers
Integer numbers
unsigned short 2 bytes
0 to 65535
int
4 bytes
-2,147,483,648 to 2,147,483,647
(2 bytes: -32768 to 32767)
Unsigned integer numbers
Long integer numbers
unsigned int
long
4 bytes
4 bytes
4 bytes
4 bytes
0 to 4,294,967,295
-2,147,483,648 to 2,147,483,647
0 to 4,294,967,295
Unsigned long integer numbers
unsigned long
float
Single-precision floating-point
numbers (6 digits of precision)
-3.4e+38 to 3.4e+38
Double-precision floating-point
numbers (15 digits of precision)
double
8 bytes
-1.797693e+308 to
1.797693e+308
?
?
long long
8 bytes
8 bytes
?
Size varies from system
to system!
unsigned long
long
?
?
long double
void
12 bytes
1 byte
?
sizeof (type_name)
Typeless
N/A
?
should be used.
Enumerated data
enum
4 bytes
Built-in Data Types in C
Fig. 5.5. Promotion hierarchy for data types ([1], p. 150)
18
Built-in Data Types in C
Representation – Manipulation
char, unsigned/signed
short,
int, unsigned/signed
long
float
double
long double
…
char < short < int < long < float < double < long double
19
Built-in Data Types in C
char
Type name: char
Memory allocation: sizeof(char) = 1 byte
The smallest addressable unit in memory
Enough to store a single ASCII character
Signed range: -128..127
„A‟
+
/-
1
1
0
1
0
0
0
0
0
0
0
0
1
1 bit for sign (0 = +; 1 = -)
Unsigned range: 0..255
„a‟
1
0
8 bits for value
ASCII = American Standard Code for Information Interchange
20
Tải về để xem bản đầy đủ
Bạn đang xem 20 trang mẫu của tài liệu "Bài giảng Introduction to Computer Programming (C language) - Chapter 3: Variables and basic data types - Võ Thị Ngọc Châu", để tải tài liệu gốc về máy hãy click vào nút Download ở trên
File đính kèm:
bai_giang_introduction_to_computer_programming_c_language_ch.pdf

