Variables, types, and arithmetic · Biến, kiểu dữ liệu và toán học
Every value has a type
- In C, every variable has a type that says what kind of value it holds.
- The three you use most:
int(a whole number),double(a number with a decimal point), andchar(a single character). - You must give the type when you create the variable:
int n = 5;ordouble pi = 3.14;.
Mọi giá trị đều có kiểu dữ liệu
- Trong C, mỗi biến đều có một kiểu xác định loại giá trị mà nó chứa.
- Ba kiểu bạn hay dùng nhất:
int(số nguyên),double(số có dấu thập phân), vàchar(một ký tự đơn lẻ). - Bạn phải chỉ định kiểu khi tạo biến:
int n = 5;hoặcdouble pi = 3.14;.
Doing arithmetic
- C does math with
+,-,*, and/, just like a calculator. *and/happen before+and-. Use brackets( )to change the order.- The
%operator gives the remainder of a division:17 % 5is2.
Thực hiện phép toán
- C thực hiện toán học với
+,-,*, và/, giống như một chiếc máy tính. *và/diễn ra trước+và-. Sử dụng ngoặc( )để thay đổi thứ tự ưu tiên.- Toán tử
%trả về phần dư của phép chia:17 % 5là2.
Integers divide differently
- When you divide two
ints, C throws away the fraction:7 / 2is3, not3.5. - To keep the fraction, at least one side must be a
double.7 / 2.0is3.5. - You can cast an
intto adoublewith(double):(double)sum / count.
Chia số nguyên khác biệt
- Khi chia hai
int, C loại bỏ phần thập phân:7 / 2là3, không phải3.5. - Để giữ lại phần thập phân, ít nhất một vế phải là
double.7 / 2.0là3.5. - Bạn có thể ép kiểu một
intthành mộtdoublebằng cách dùng(double):(double)sum / count.
Reading input
scanfreads typed input into a variable.scanf("%d", &n);reads oneintinton.- Note the
&beforen:scanfneeds the address of the variable so it can fill it in. - In this playground, Run first asks you for the input, one value per line; Check answer feeds the example input for you.
Đọc dữ liệu đầu vào
scanfđọc dữ liệu nhập từ bàn phím vào một biến.scanf("%d", &n);đọc mộtintvàon.- Lưu ý dấu
&trướcn:scanfcần địa chỉ của biến để có thể gán giá trị vào đó. - Trong môi trường này, Run trước hết sẽ yêu cầu bạn nhập dữ liệu, mỗi giá trị trên một dòng; Check answer sẽ cung cấp sẵn dữ liệu mẫu cho bạn.
#include <stdio.h>
int main(void) {
int a = 7, b = 2;
printf("%d\n", a / b); // 3 (integer division)
printf("%.1f\n", a / 2.0); // 3.5 (one side is a double)
printf("%d\n", a % b); // 1 (remainder)
return 0;
}
Common mistakes
- Declare a variable with its type:
int n;. - Integer division truncates:
7 / 2is3, not3.5.
Lỗi thường gặp
- Khai báo một biến với kiểu dữ liệu của nó:
int n;. - Phép chia số nguyên bị cắt ngắn:
7 / 2là3, không phải3.5.
Now you try
- Use
/and%for whole-number math, and adouble(or a cast) when you need a fraction. - For the function tasks, do not write a
main— the checker provides one. - Press Run to compile and run, then Check answer.
Bây giờ bạn thử
- Sử dụng
/và%cho phép toán số nguyên, và mộtdouble(hoặc ép kiểu) khi bạn cần kết quả có phần phân số. - Đối với các nhiệm vụ hàm, không viết một
main— trình kiểm tra sẽ cung cấp cho bạn một cái. - Nhấn Chạy để biên dịch và chạy, sau đó Kiểm tra câu trả lời.
Variables hold a value · Biến lưu giữ một giá trị
A variable is a named box of a fixed type; assigning replaces it. · Một biến là một hộp có tên của kiểu cố định; việc gán sẽ thay thế nội dung cũ.
Read one integer n with scanf. A box holds 6 items. Print two lines exactly: Full boxes: then the value of n / 6, and Left over: then the value of n % 6. · Đọc một số nguyên n bằng scanf. Một hộp chứa 6 mục. In hai dòng chính xác: Full boxes: rồi giá trị của n / 6, và Left over: rồi giá trị của n % 6.
Click Run to see the output here. · Nhấn Chạy để xem kết quả ở đây.
Complete double average3(int a, int b, int c) so it returns the average of the three as a double. Watch out for integer division — divide by 3.0, not 3. Do not write a main. · Hoàn thành double average3(int a, int b, int c) sao cho nó trả về trung bình cộng của ba giá trị đó dưới dạng double. Cẩn thận với phép chia số nguyên — hãy chia cho 3.0, không phải 3. Không viết một main.
Click Run to see the output here. · Nhấn Chạy để xem kết quả ở đây.
Write double hyp(double a, double b) that returns the hypotenuse length: the square root of a*a + b*b. Use sqrt from <math.h>. Do not write a main. · Viết double hyp(double a, double b) trả về độ dài cạnh huyền: căn bậc hai của a*a + b*b. Dùng sqrt từ <math.h>. Đừng viết một main.
Click Run to see the output here. · Nhấn Chạy để xem kết quả ở đây.