C basics: main, printf, and functions · C cơ bản: main, printf, và hàm
C is a compiled language
- C is one of the oldest and most important languages. Linux, databases, and even Python are built with C.
- Unlike Python, C is compiled: the computer first turns your code into a program, then runs it.
- C is strict: every statement ends with a semicolon
;, and every variable needs a type.
C là ngôn ngữ biên dịch
- C là một trong những ngôn ngữ lâu đời và quan trọng nhất. Linux, cơ sở dữ liệu, và thậm chí Python đều được xây dựng bằng C.
- Khác với Python, C là biên dịch: máy tính trước tiên biến mã của bạn thành một chương trình, sau đó chạy nó.
- C rất khiêm khắt: mọi mệnh lệnh đều kết thúc bằng dấu chấm phẩy
;, và mọi biến đều cần một kiểu.
The C skeleton
- Every C program starts in a function called main. Your code goes inside main.
#include <stdio.h>gives youprintf, which prints text.return 0;at the end ofmainmeans "the program finished without an error".
Khung xương của C
- Mọi chương trình C đều bắt đầu trong một hàm gọi là main. Mã của bạn nằm trong main.
#include <stdio.h>cung cấp cho bạnprintf, dùng để in văn bản.return 0;ở cuốimaincó nghĩa "chương trình đã kết thúc mà không có lỗi".
#include <stdio.h>
int main(void) {
printf("C runs from inside main.\n");
int year = 11;
printf("Grade %d\n", year);
return 0;
}
printf and types
printfprints text. The\ninside the text means "start a new line".- To print a value, use a placeholder:
%dfor anint,%ffor adouble,%cfor one character. printf("Grade %d\n", year);puts the value ofyearwhere the%dis.- Every variable needs a type first:
int year = 11;ordouble pi = 3.14;.
printf và các kiểu dữ liệu
printfin văn bản. Ký tự\nnằm bên trong văn bản có nghĩa là "bắt đầu dòng mới".- Để in một giá trị, hãy sử dụng một điểm đặt chỗ:
%dcho mộtint,%fcho mộtdouble,%ccho một ký tự. printf("Grade %d\n", year);đặt giá trị củayearvào vị trí mà%dđang ở.- Mọi biến cần có một kiểu dữ liệu trước:
int year = 11;hoặcdouble pi = 3.14;.
Common mistakes
- Every statement ends with
;, andmainreturnsint(return 0;). printfneeds the right format (%d,%f) and\nfor a new line.
Lỗi thường gặp
- Mỗi câu lệnh kết thúc bằng
;, vàmaintrả vềint(return 0;). printfcần định dạng đúng (%d,%f) và\nđể xuống dòng mới.
Now you try
- Each task pre-fills the code you need. Write inside
main, or complete the function shown. - Some tasks ask for a function only — do not write your own
main; the checker adds one. - Press Run to compile and run, then Check answer. Your code compiles and runs on the server.
Bây giờ bạn thử
- Mỗi bài tập sẽ tự động điền sẵn phần code bạn cần. Hãy viết bên trong
main, hoặc hoàn thành hàm được hiển thị. - Một số bài yêu cầu chỉ cần hàm — đừng tự viết
main; bộ kiểm tra sẽ thêm vào đó. - Nhấn Run để biên dịch và chạy, sau đó nhấn Check answer. Code của bạn được biên dịch và chạy trên máy chủ.
Compile then run · biên dịch rồi chạy
C source is compiled to a program, which you then run. · Mã nguồn C được biên dịch thành chương trình, sau đó bạn mới chạy nó.
Inside main, use printf(...) to print exactly Hello, C! on its own line. · Bên trong main, dùng printf(...) để in chính xác Hello, C! trên một dòng riêng.
Click Run to see the output here. · Nhấn Chạy để xem kết quả ở đây.
Complete the function int square(int n) so it returns n * n. The checker calls it with several values. Do not write a main — the checker provides one. · Hoàn thiện hàm int square(int n) sao cho trả về n * n. Bộ kiểm tra sẽ gọi hàm với nhiều giá trị. Đừng viết một main — bộ kiểm tra đã cung cấp sẵn.
Click Run to see the output here. · Nhấn Chạy để xem kết quả ở đây.
Inside main, declare int grade = 11;. Then use printf to print two lines: first Hello!, then Grade 11 — print the number with a %d placeholder, not by typing 11. · Bên trong main, khai báo int grade = 11;. Sau đó sử dụng printf để in hai dòng: đầu tiên là Hello!, sau đó là Grade 11 — in số bằng cách sử dụng %d, không phải gõ trực tiếp 11.
Click Run to see the output here. · Nhấn Chạy để xem kết quả ở đây.