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 } נתון. שלם const char *suit_name(enum Suit s) כך שיחזיר את השם ("Hearts", "Diamonds", "Clubs", "Spades") באמצעות switch. אל תכתוב 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. · שלם int next_id(void) באמצעות מונה מקומי static, כך שהקריאה הראשונה תחזיר 1, הבאה תחזיר 2, ואז 3, ועוד. אל תכתוב main.
Click Run to see the output here. · לחץ על הרץ כדי לראות את התוצא כאן.