Preprocessor, const, static, and enum · Tiền xử lý, const, static và enum
What runs before the compiler
- Before the compiler sees your code, the preprocessor handles every line that starts with
#. - It pastes in headers, replaces macros, and removes comments — a simple text step.
- You have used it already:
#include <stdio.h>is a preprocessor command.
Những gì diễn ra trước trình biên dịch
- Trước khi trình biên dịch thấy mã của bạn, trình tiền xử lý xử lý mọi dòng bắt đầu với
#. - Nó dán vào header, thay thế macro, và xóa comment — một bước văn bản đơn giản.
- Bạn đã sử dụng nó rồi:
#include <stdio.h>là một lệnh tiền xử lý.
#include and #define
#include <stdio.h>pastes the contents of a header so you can use its functions.#define PI 3.14159makesPIa macro: the preprocessor replaces everyPIwith that text.- Macros have no type and no semicolon — they are pure text replacement.
#include và #define
#include <stdio.h>dán nội dung của một header để bạn có thể sử dụng các hàm của nó.#define PI 3.14159biếnPIthành một macro: trình tiền xử lý thay thế mọiPIbằng văn bản đó.- Macros không có kiểu và không có dấu chấm phẩy — chúng là thay thế văn bản thuần túy.
const vs #define
const double pi = 3.14159;is a real variable that cannot be changed. It has a type, so the compiler checks it.#define PI 3.14159is just text replacement, done before compiling.- Modern C usually prefers
constfor values, but#defineis still common for constants and flags.
const so với #define
const double pi = 3.14159;là một biến thật không thể thay đổi. Nó có kiểu, nên trình biên dịch kiểm tra nó.#define PI 3.14159chỉ là thay thế văn bản, được thực hiện trước khi biên dịch.- C hiện đại thường ưu tiên
constcho giá trị, nhưng#definevẫn phổ biến cho hằng số và cờ.
static and enum
- A
staticvariable inside a function keeps its value between calls — it is set up once, not reset every time the function runs. - An
enumgives names to a set of relatedintvalues:enum Suit { HEARTS, DIAMONDS, CLUBS, SPADES };(hereHEARTSis0). - Both make code clearer:
staticfor memory that persists,enumfor named choices.
static và enum
- Một biến
staticbên trong hàm giữ giá trị giữa các lần gọi — nó được thiết lập một lần, không được đặt lại mỗi lần hàm chạy. - Một
enumgán tên cho một tập hợp các giá trịintliên quan:enum Suit { HEARTS, DIAMONDS, CLUBS, SPADES };(ở đâyHEARTSlà0). - Cả hai làm mã rõ ràng hơn:
staticcho bộ nhớ tồn tại,enumcho các lựa chọn có tên.
#include <stdio.h>
#define GREETING "Hello"
enum Color { RED, GREEN, BLUE };
int main(void) {
printf("%s\n", GREETING); // Hello
enum Color c = GREEN;
printf("%d\n", c); // 1
return 0;
}
Common mistakes
#definedoes plain text replacement — wrap macro bodies in parentheses.constcannot change; astaticlocal keeps its value between calls.
Lỗi thường gặp
#definethực hiện thay thế văn bản thuần túy — bọc thân macro trong ngoặc đơn.constkhông thể thay đổi; một biếnstaticcục bộ giữ giá trị giữa các lần gọi.
Now you try
- Use the given
#defineandenumin the starters. - A
staticlocal variable remembers its value across calls. Do not write amain— the checker provides one.
Bây giờ bạn thử
- Sử dụng
#definevàenumđã cho trong file khởi tạo. - Một biến cục bộ
staticnhớ giá trị của nó qua các lần gọi. Đừng viết mộtmain— bộ kiểm tra sẽ cung cấp một.
The enum Suit { HEARTS, DIAMONDS, CLUBS, SPADES } is given. Complete const char *suit_name(enum Suit s) so it returns the name ("Hearts", "Diamonds", "Clubs", "Spades") using a switch. Do not write a main. · Đã cho enum Suit { HEARTS, DIAMONDS, CLUBS, SPADES }. Hoàn thành const char *suit_name(enum Suit s) để trả về tên ("Hearts", "Diamonds", "Clubs", "Spades") bằng cách sử dụng một switch. Không viết một main.
Click Run to see the output here. · Nhấn Chạy để xem kết quả ở đây.
A macro #define PI 3.14159265358979 is given. Complete double circle_area(double r) so it returns the area of a circle, PI * r * r, using the macro. Do not write a main. · Đã cho macro #define PI 3.14159265358979. Hoàn thành double circle_area(double r) để trả về diện tích hình tròn, PI * r * r, sử dụng macro. Không viết một main.
Click Run to see the output here. · Nhấn Chạy để xem kết quả ở đây.
Complete int next_id(void) using a static local counter, so the first call returns 1, the next 2, then 3, and so on. Do not write a main. · Hoàn thành int next_id(void) sử dụng biến đếm cục bộ static, sao cho lần gọi đầu tiên trả về 1, lần tiếp theo 2, sau đó 3, và cứ thế. Không viết một main.
Click Run to see the output here. · Nhấn Chạy để xem kết quả ở đây.