Preprocessor, const, static, and enum · Preprocessor, const, static, dan 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.
Apa yang terjadi sebelum kompiler
- Sebelum kompiler melihat kode Anda, preprocessor menangani setiap baris yang dimulai dengan
#. - Ia menyematkan header, menggantikan makro, dan menghapus komentar — langkah teks sederhana.
- Anda telah menggunakannya sebelumnya:
#include <stdio.h>adalah perintah 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 dan #define
#include <stdio.h>menyematkan isi header agar Anda dapat menggunakan fungsinya.#define PI 3.14159menjadikanPIsebagai makro: preprocessor mengganti setiapPIdengan teks itu.- Makro tidak memiliki tipe dan tidak ada titik koma — mereka adalah pengganti teks murni.
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;adalah variabel riil yang tidak dapat diubah. Ia memiliki tipe, sehingga compiler memeriksanya.#define PI 3.14159hanyalah pengganti teks, dilakukan sebelum kompilasi.- C modern biasanya lebih memilih
constuntuk nilai, tetapi#definemasih umum digunakan untuk konstanta dan flag.
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 dan enum
- Variabel
staticdi dalam fungsi menyimpan nilainya antar pemanggilan — ia diatur sekali, tidak disetel ulang setiap kali fungsi dijalankan. - Sebuah
enummemberikan nama pada sekumpulanintyang terkait:enum Suit { HEARTS, DIAMONDS, CLUBS, SPADES };(di siniHEARTSadalah0). - Keduanya membuat kode lebih jelas:
staticuntuk memori yang persisten,enumuntuk pilihan bernama.
#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.
Kesalahan umum
#definemelakukan pengganti teks biasa — bungkus tubuh makro dalam kurung.consttidak dapat berubah; variabel lokalstaticmenyimpan nilainya antar pemanggilan.
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.
Sekarang Anda coba
- Gunakan
#definedanenumyang diberikan dalam starter. - Variabel lokal
staticmengingat nilainya lintas pemanggilan. Jangan tulismain— pemeriksa menyediakan satu.
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 } diberikan. Lengkapi const char *suit_name(enum Suit s) agar mengembalikan nama ("Hearts", "Diamonds", "Clubs", "Spades") menggunakan switch. Jangan tulis sebuah main.
Click Run to see the output here. · Klik Jalankan untuk melihat output di sini.
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. · Macro #define PI 3.14159265358979 diberikan. Lengkapi double circle_area(double r) agar mengembalikan luas lingkaran, PI * r * r, menggunakan macro tersebut. Jangan tulis sebuah main.
Click Run to see the output here. · Klik Jalankan untuk melihat output di sini.
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. · Lengkapi int next_id(void) menggunakan static penanda lokal, sehingga panggilan pertama mengembalikan 1, berikutnya 2, lalu 3, dan seterusnya. Jangan tulis sebuah main.
Click Run to see the output here. · Klik Jalankan untuk melihat output di sini.