Preprocessor, const, static, and enum · 预处理器、const、static 与 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.
在编译器之前运行的东西
- 在编译器看到你的代码之前,预处理器会先处理每一行以
#开头的内容。 - 它把头文件粘贴进来、替换宏、去掉注释 —— 是一个简单的文本步骤。
- 你已经用过它了:
#include <stdio.h>就是一条预处理命令。
#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 与 #define
#include <stdio.h>把一个头文件的内容粘贴进来,这样你就能用它的函数。#define PI 3.14159把PI变成一个宏:预处理器会把每个PI替换成那段文本。- 宏没有类型,也没有分号 —— 它纯粹是文本替换。
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 与 #define
const double pi = 3.14159;是一个真正的变量,但不能被改变。它有类型,所以编译器会检查它。#define PI 3.14159只是文本替换,在编译之前完成。- 现代 C 通常更偏向用
const表示数值,但#define在常量和标志位上仍然常见。
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 与 enum
- 函数里的
static变量会在多次调用之间保留它的值 —— 它只创建一次,而不是每次函数运行时都重置。 enum给一组相关的int值起名字:enum Suit { HEARTS, DIAMONDS, CLUBS, SPADES };(这里HEARTS是0)。- 两者都让代码更清晰:
static用于需要保留的内存,enum用于有名字的选项。
#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.
常见错误
#define只做纯文本替换——宏体要用括号包起来。const不能改;static局部变量在多次调用间保留值。
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.
现在轮到你了
- 使用起始代码里给出的
#define和enum。 - 函数里的
static局部变量会在多次调用之间记住它的值。不要自己写main—— 检查器会提供。
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. · enum Suit { HEARTS, DIAMONDS, CLUBS, SPADES } 已给出。用 switch 完成 const char *suit_name(enum Suit s),让它返回名字("Hearts"、"Diamonds"、"Clubs"、"Spades")。不要写 main。
Click Run to see the output here. · 点击“运行”查看此处输出。
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. · 已给出宏 #define PI 3.14159265358979。完成 double circle_area(double r),用这个宏返回圆的面积 PI * r * r。不要写 main。
Click Run to see the output here. · 点击“运行”查看此处输出。
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. · 用一个 static 局部计数器完成 int next_id(void),让第一次调用返回 1,下一次 2,再下一次 3,以此类推。不要写 main。
Click Run to see the output here. · 点击“运行”查看此处输出。