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. · اضغط تشغيل لرؤية المخرجات هنا.