NHẬP MÔN LẬP TRÌNH
TÍNH TOÁN CƠ BẢN
Variables Expressions
NHẬP MÔN LẬP TRÌNH
MỤC TIÊU
• Hiểu kiểu dữ liệu (data type) là gì
• Khai báo hằng (constants) và biến (variables)
của chương trình
Basic Computations
2
NHẬP MÔN LẬP TRÌNH
NỘI DUNG
• Biến và kiểu dữ liệu
– Data Types – Integral Types – Floating-Point Types – Declarations
• Biểu thức – Số học – Quan hệ – Logical – Phép gán – Mixing Data Types – Casting
Basic Computations
3
NHẬP MÔN LẬP TRÌNH
Review
• Computer program: A set of instructions that computer hardware will
•
execute. Issues for a program/software: Usability, Correctness, Maintainability, Portability
• Computer software: A set of related programs • Steps to develop a software: Requirement collecting, Analysis, Design,
Implementing, Testing, Deploying, Maintaining
Information: Mean of data
• Data: Specific values that describe something
•
• Fundamental Data Units: Bit, Nibble, Byte, KB, MB, GB, TB
• Data Representation: Number systems: 2, 10, 8, 16
• Program Instructions:
languages
Basic Computations
4
NHẬP MÔN LẬP TRÌNH
1- Variables and Data Types
Biến là một tên tham chiếu đến một vị trí trong bộ nhớ (address)
0000 1001 1100 0011
c
b
Chứa dữ liệu dạng nhị phân Khi chương trình biên dịch, trình biên dịch sẽ xác định vị trí mà biến được phân bổ.
a
Questions: (1) Nó ở đâu? It’s Address (2) Nó chiếm bao nhiêu byte nhớ?
Data type
Basic Computations
5
NHẬP MÔN LẬP TRÌNH
Variables and Data Types…
C has 4 primitive data types:
Type
Length
Range
int
-32,768 to 32,767 (16 bit) -2,147,483,648 to 2,147,483,647 (32 bit)
Word (length of CPU register)
char
byte
-128 to 127
float
4 bytes
3,4 * 10-38 to 3,4 * 1038
double
8 bytes
1,7 * 10-308 to 1,7 * 10308
Basic Computations
6
NHẬP MÔN LẬP TRÌNH
‘A’
Variables and Data Types… c:2293623
1
i:2293616
Các biến được lưu trữ ở đâu và chiếm bao nhiêu?
1000 l:2293612
0.5 f:2293608
12.809
d:2293600
7
The operator & will get the address of a variable or code. The operator sizeof(var/type) return the size (number of byte) occupied by a variable/type Basic Computations
NHẬP MÔN LẬP TRÌNH
Variables and Data Types…
The ASCII table for characters
Basic Computations
8
NHẬP MÔN LẬP TRÌNH
Variables and Data Types…
Exercises: • What is the ASCII encoding for
• What is the EBCDIC encoding for
'0' ___________________________________________ 'a' ___________________________________________ 'A' ___________________________________________
•
Convert the following binary notation to an ASCII character: 0110 1101 _____________________________________ 0100 1101 _____________________________________
'0' ___________________________________________ 'a' ___________________________________________ 'A' ___________________________________________
•
Basic Computations
9
Convert the following decimal notation to an EBCDIC character: 199 ______________________ 35 __________________________
NHẬP MÔN LẬP TRÌNH
Variables and Data Types…
Khai báo biến trong C: data_type identifier [= initial value]; • Ví dụ:
char section; int numberOfClasses; double cashFare = 2.25;
Quy định đặt tên: Tên chỉ 01 từ
– Không phải là từ dành riêng cho C – Tên không dài hơn 31 ký tự
Basic Computations
10
Letters/digits/ ‘_’ Letter or ‘_’
NHẬP MÔN LẬP TRÌNH
Variables and Data Types…
camel_back
giraffe's
Exercises: Which of the following is an invalid identifier? 4me2 register
whale _how_do_you_do
senecac.on.ca digt3
• Select a descriptive identifier for and write a
complete declaration for: – A shelf of books__________________________ – A cash register___________________________ – A part_time student_______________________
– A group of programs______________________
Basic Computations
11
NHẬP MÔN LẬP TRÌNH
Variables and Data Types…
Một số thao tác trên biến – Gán 1 giá trị cho biến – Gán giá trị của một biến khác cho biến, – Xuất giá trị của biến – Nhập giá trị cho một biến vào vị trị trong bộ nhớ.
constant
AnotherVar
anotherVar
Monitor
File
Keyboard variable
File Netwwork
Basic Computations
12
Network
NHẬP MÔN LẬP TRÌNH
Questions as Summary
• What is a variable? • What is a data type? • The size of the int data type is …. Bytes. • Chọn khai báo sai:
int n=10; char c1, c2=‘A’; int m=19; k=2; char c3; int t; float f1; f2=5.1;
Basic Computations
13
NHẬP MÔN LẬP TRÌNH
2- Literals
• Constant values are
specified directly in the source code. • They can be
– Character literals
(constant characters) – String literals(constant
strings)
– Number literals
(constant numbers)
Basic Computations
14
NHẬP MÔN LẬP TRÌNH
Literals: ký tự, chuỗi ký tự
4 cách biểu diễn cho chữ cái: • 04 cách:
– Sử dụng dấu nháy đơn- ví dụ 'A', – Mã thập phân ASCII cho ký tự: 65 cho 'A‘ – Mã bát phân ASCII cho ký tự: 0101 cho'A', – Mã thập lục phân ASCII cho ký tự: 0x41 cho'A',
Basic Computations
15
Assign value to a variable: The operator =
NHẬP MÔN LẬP TRÌNH
Literals: Escape Sequences
• Pre-defined literals for special actions:
Basic Computations
16
NHẬP MÔN LẬP TRÌNH
Literals: Escape Sequences…
Error! Why?
Modify then run it
Basic Computations
17
Change \ to \\ then run it
NHẬP MÔN LẬP TRÌNH
3- Named Constants
• Use the pre-processor (pre-compiled directive) #define or
the keyword const
Basic Computations
18
Compiler will allocate memory location for constants that are declared using the keyword const
NHẬP MÔN LẬP TRÌNH
Input/Output Variables…
Chuyển đổi
Basic Computations
19
NHẬP MÔN LẬP TRÌNH
Input/Output Variables…
4210784
n
main
4199056
2293620
m
Format string
Basic Computations
20
scanf( “%d%d”, &n, &m) scanf( “%d%d”, 4210784, 2293620) means that get keys pressed then change them to decimal integers and store them to memory locations 4210784, 2293620.
NHẬP MÔN LẬP TRÌNH
Input/Output Variables…
Nhập giá trị vào một biến: scanf (“input format”, &var1, &var2,…) Xuất giá trị của biến ra màn hình: printf (“output format”, var1, var2,…)
The function scanf receive the BLANK or ENTER KEYS as separators.
Format string
Basic Computations
21
Data holders
NHẬP MÔN LẬP TRÌNH
Questions
• Explain means of parameters of the scanf(…) and
the printf(…) functions.
• Use words “left” and “right”. The assignment x=y;
will copy the value in the ….. side to the ….. Side.
Basic Computations
22
NHẬP MÔN LẬP TRÌNH
Exercises
1- Develop a C program in which 2 integers, 2 float numbers and 2 double numbers are declared. Ask user for values of them then print out values of them.
2- Run the following program:
Why user do not have a chance to stroke the ENTER key before the program terminate?
Basic Computations
23
Modify and re-run: getchar(); getchar();
NHẬP MÔN LẬP TRÌNH
5- Biểu thức - Expressions
• Expression là một kết hợp hợp lệ các hằng số, các biến, toán tử và các hàm và trả về một kết quả.
• Ví dụ:
32-x+y/6 45 > 5*x
16.5 + 4/sqrt(15) * 17 – 8 y = 17 + 6*5/9 –z*z
Basic Computations
24
NHẬP MÔN LẬP TRÌNH
Expressions: Arithmetic Operators
+
+x
y = +x ; y = x;
leaves the variable, constant or expression unchanged
Op. Syntax Description Example
+ -
x+y x-y
z= x+y; t = x-y;
Add/substract values of two operands
- -x reverses the sign of the variable y= -x;
* / x*y x/y
Multiplies values of two operands Get the quotient of a division
z= x-y; z = 10/3; 3 z = 10.0/3; 3.3333333
% x%y Get remainder of a integral division
Demo in the next slide.
++ --
++x --x x++ x--
Increase/decrease the value of a variable (prefix/postfix operators)
Basic Computations
25
17%3 2 15.0 % 3 ERROR
NHẬP MÔN LẬP TRÌNH
Expressions: Arith. Operators…
Basic Computations
26
Explain yourself the output
NHẬP MÔN LẬP TRÌNH
Expressions: Arith. Operators…
?
Basic Computations
27
Explain yourself the output
NHẬP MÔN LẬP TRÌNH
Expressions: Relational Operators
• For comparisional operators. • < <= == >= > != • Return 1: true/ 0: false
Basic Computations
28
NHẬP MÔN LẬP TRÌNH
Expressions: Logical Operators
• Operator for association of conditions • && (and), || (or) , ! (not) • Return 1: true, 0: false
Basic Computations
29
NHẬP MÔN LẬP TRÌNH
Expressions: Assignments Operators
• Variable = expression • Shorthand assignments:
Basic Computations
30
NHẬP MÔN LẬP TRÌNH
Expressions: Mixing Data Types
• Explicit Casting Chúng ta có thể thay tạm thời đổi kiểu dữ liệu của bất kỳ toán hạng nào trong bất kỳ biểu thức nào để có được kết quả của một liệu dữ kiểu nhất định.
Basic Computations
31
NHẬP MÔN LẬP TRÌNH
Thank You
Basic Computations
32

