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.
ก่อนที่ compiler จะเห็นโค้ดของคุณ
- ก่อนที่ compiler จะเห็นโค้ดของคุณ preprocessor จะจัดการกับทุกบรรทัดที่ขึ้นต้นด้วย
# - มันใส่ headers เข้าไป เปลี่ยน macros และลบ comments — เป็นขั้นตอนข้อความธรรมดาๆ
- คุณเคยใช้มันไปแล้ว:
#include <stdio.h>เป็นคำสั่ง preprocessor
#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>แทรก contents ของ header เข้าไปเพื่อให้คุณสามารถใช้ functions ของมันได้#define PI 3.14159ทำให้PIเป็น macro: preprocessor จะแทนที่ทุกPIด้วยข้อความนั้น- Macros ไม่มี type และไม่มี semicolon —它们是纯文本替换
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 vs #define
const double pi = 3.14159;เป็น real variable ที่ไม่สามารถแก้ไขได้ มันมี type ดังนั้น compiler จึงตรวจสอบมัน#define PI 3.14159เป็นการแทนที่ข้อความธรรมดาๆ ทำก่อนการ compile- Modern C มักจะเลือก
constสำหรับ values แต่#defineยังคงเป็นที่นิยมสำหรับ constants และ flags
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ภายใน function รักษาค่าของมันระหว่างการเรียกใช้ — ถูกตั้งค่าครั้งเดียว ไม่ใช่รีเซ็ตทุกครั้งที่มี function ทำงาน - An
enumให้ชื่อแก่ชุดของintvalues ที่เกี่ยวข้องกัน:enum Suit { HEARTS, DIAMONDS, CLUBS, SPADES };(ในที่นี้HEARTSคือ0) - ทั้งสองทำให้ code ชัดเจนขึ้น:
staticสำหรับ memory ที่คงอยู่,enumสำหรับ choices ที่มีชื่อ
#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ทำการแทนที่ข้อความธรรมดา — ห่อ macro bodies ไว้ในวงเล็บconstไม่สามารถแก้ไขได้; ตัวแปรstatic局部的 keeps its value ระหว่างการเรียกใช้
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ที่กำหนดไว้ใน starters - ตัวแปร
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 } ถูกกำหนดให้ เติม const char *suit_name(enum Suit s) เพื่อให้คืนชื่อ ("Hearts", "Diamonds", "Clubs", "Spades") โดยใช้ switch ห้าม เขียน main
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่
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. · คลิก Run เพื่อดูผลลัพธ์ที่นี่
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. · เติม int next_id(void) โดยใช้ตัวนับท้องถิ่น static, เพื่อให้การเรียกครั้งแรกคืนค่า 1, ครั้งถัดไป 2, แล้ว 3 และ seterusnya ห้าม เขียน main
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่