Thuta Learning
C
BasicProgrammingအခြေခံ

Constants

Cသင်ခန်းစာ 825 မိနစ်

ဒီခန်းပြီးရင် ဘာတတ်သွားမလဲ

  • Magic number တွေကို const variable၊ #define macro ဒါမှမဟုတ် enum constant နဲ့ နာမည်ပေးနိုင်ဖို့
  • const int ကို C မှာ case label နဲ့ file-scope array size အဖြစ် ဘာကြောင့် သုံးမရလဲ ရှင်းပြနိုင်ဖို့
  • Macro ကို parentheses နဲ့ ရေးပြီး semicolon မထည့်ရတဲ့ အကြောင်းရင်းကို နားလည်ဖို့
  • Situation တစ်ခုအတွက် const, #define, enum ထဲက သင့်တော်တာကို ရွေးနိုင်ဖို့

ရိုးရိုးလေး ရှင်းပြမယ်

ရိုးရိုးလေး ရှင်းရရင်

Constant ဆိုတာ program run နေစဉ် ဘယ်တော့မှ မပြောင်းသင့်တဲ့ value တစ်ခုကို နာမည်ပေးထားတာပါ — တစ်နာရီမှာ မိနစ် 60၊ PI၊ အခန်းထဲက ထိုင်ခုံ အများဆုံး အရေအတွက်၊ tax rate စတာတွေပါ။ Code ထဲမှာ 60 လို့ ရေးမယ့်အစား MINUTES_PER_HOUR လို့ ရေးလိုက်ရင် ဖတ်တဲ့သူက ဂဏန်းရဲ့ အဓိပ္ပာယ်ကို ချက်ချင်း သိပါတယ်။

အကျိုးကျေးဇူး နောက်တစ်ခုက value ကို နေရာတစ်ခုတည်းမှာပဲ ပြောင်းရတာပါ။ Tax rate တက်သွားရင် constant definition တစ်ကြောင်းကို ပြင်လိုက်ရုံနဲ့ သုံးထားတဲ့ နေရာတိုင်း အလိုလို update ဖြစ်ပါတယ်။ C မှာ constant ရေးနည်း သုံးမျိုး ရှိပြီး တစ်မျိုးချင်းစီမှာ ကိုယ်ပိုင် အားသာချက်တွေ ရှိပါတယ်။

Problem → Why → Solution

ပြဿနာ: နေရာသုံးခုမှာ ကွဲသွားတဲ့ tax rate

Invoice program တစ်ခုမှာ tax rate 0.07 ကို နေရာသုံးခုမှာ တိုက်ရိုက် ရေးထားတယ် ဆိုပါစို့။ Rate က 0.08 ဖြစ်သွားတော့ developer တစ်ယောက်က refund code ထဲမှာပဲ ပြင်ခဲ့ပါတယ်။ Program က warning တစ်ခုမှ မပြဘဲ compile ဖြစ်ပြီး ဖောက်သည်တွေက invoice ပေါ်မှာ rate တစ်မျိုး၊ refund ပေါ်မှာ နောက်တစ်မျိုး ရသွားပါတယ်။ ပိုဆိုးတာက နောက်ထပ် ဖတ်တဲ့သူက 0.07 ဟာ tax လား၊ discount လား၊ commission လား ခွဲမသိနိုင်တာပါ။

invoice.c
double subtotal = 120.0;
double tax = subtotal * 0.07;               /* what is 0.07? */
printf("Tax: %.2f\n", tax);

/* ... 200 lines later ... */
double total = subtotal + subtotal * 0.07;  /* same number, copied */

/* ... in the refund code, someone already changed it ... */
double refund_tax = subtotal * 0.08;        /* now the copies disagree */

ဘာကြောင့် compiler က မကူညီနိုင်တာလဲ

Compiler အမြင်မှာ 0.07 နဲ့ 0.08 နှစ်ခုလုံးက မှန်ကန်တဲ့ double literal တွေပဲ ဖြစ်ပြီး အဲဒါတွေက အတူတူ ဖြစ်ရမယ်ဆိုတာ မသိပါဘူး။ Value ကို TAX_RATE လို နာမည်တစ်ခုတည်းအောက်မှာ စုထားမှ ပြောင်းစရာ နေရာ တစ်ခုပဲ ကျန်ပြီး ကွဲပြားနိုင်ခြေ ပျောက်သွားပါတယ်။ နာမည်က code ကိုယ်တိုင်ကို documentation ဖြစ်စေပါတယ်။

Core concept: const, #define နဲ့ enum

const က variable တစ်ခုကို read-only ဖြစ်စေပါတယ်။ const int MINUTES_PER_HOUR = 60; မှာ type (int) ရှိတယ်၊ ကြေညာထားတဲ့ block ထဲမှာပဲ မြင်ရတဲ့ scope ရှိတယ်၊ debugger ထဲမှာလည်း နာမည်နဲ့ မြင်ရပါတယ်။ အဲဒါကို ပြန် assign လုပ်ဖို့ ကြိုးစားရင် GCC က error: assignment of read-only variable 'MINUTES_PER_HOUR' လို့ ငြင်းပါတယ်။ ဒါပေမယ့် C မှာ (C++ နဲ့ မတူဘဲ) const variable က "integer constant expression" မဟုတ်ပါဘူး — ဒါကြောင့် switch ရဲ့ case label အဖြစ်၊ file scope မှာ array size အဖြစ် သုံးလို့ မရပါဘူး။

#define MAX_SEATS 40 ကတော့ variable မဟုတ်ပါဘူး။ Compile မလုပ်ခင် preprocessor က MAX_SEATS ဆိုတဲ့ စာသားတိုင်းကို 40 နဲ့ အစားထိုးပါတယ် — ရိုးရိုး find-and-replace ပါ။ Type မရှိ၊ scope မရှိ (#define ကနေ file အဆုံးထိ သက်ရောက်) ဖြစ်ပေမယ့် literal 40 ရေးလို့ရတဲ့ နေရာတိုင်းမှာ သုံးလို့ ရပါတယ်။ စာသားကို အတိအကျ ကူးထည့်တာမို့ expression ပါတဲ့ macro ကို parentheses နဲ့ ပတ်ရပြီး အဆုံးမှာ semicolon မထည့်ရပါဘူး။

enum { DAYS_PER_WEEK = 7 }; က နာမည်ပေးထားတဲ့ int constant တစ်ခုကို ဖန်တီးပါတယ်။ သူက တကယ့် integer constant expression ဖြစ်လို့ case label နဲ့ array size မှာ သုံးလို့ရပြီး macro နဲ့ မတူဘဲ ရိုးရိုး identifier တွေလို scope စည်းမျဉ်းကို လိုက်နာပါတယ်။ ကန့်သတ်ချက်ကတော့ integer value တွေပဲ ရတာပါ — 3.14159 လို double ကို enum ထဲ မထည့်နိုင်ပါဘူး။

လက်တွေ့ စည်းမျဉ်းလေးကတော့ — double လို integer မဟုတ်တဲ့ value တွေနဲ့ type စစ်ဆေးမှု လိုတဲ့ value တွေအတွက် const၊ ဆက်စပ်နေတဲ့ integer code တွေနဲ့ size တွေအတွက် enum၊ conditional compilation လို preprocessor အလုပ်တွေ ဒါမှမဟုတ် header file တွေကနေ project တစ်ခုလုံး မျှသုံးရမယ့် value တွေအတွက် #define ပါ။ Constant နာမည်ကို UPPER_SNAKE_CASE နဲ့ ရေးတာက variable တွေကြားမှာ ခွဲမြင်ရလွယ်စေတဲ့ ကျယ်ကျယ်ပြန့်ပြန့် သုံးတဲ့ convention ပါ။ C23 မှာ object တွေအတွက် constexpr ထပ်ပါလာပေမယ့် compiler support ကို အရင် စစ်ပါ။

ပုံစံType နဲ့ scopecase label / file-scope array size
const int LIMIT = 40;int type ရှိ၊ block scopeC မှာ မရ
#define LIMIT 40type မရှိ၊ file အဆုံးထိရ
enum { LIMIT = 40 };int type ရှိ၊ scope ရှိရ
Mental model: value ကို ဘယ်သူက အစားထိုးလဲ
  1. #define: preprocessor က စာသား အစားထိုး
  2. enum: compiler က int constant အဖြစ် သိ
  3. const: memory ထဲက read-only variable
  4. Compiler က const ကို ပြင်တာကို တားဆီး

Compile time မှာ value သိဖို့ လိုရင် (case label, array size) enum ဒါမှမဟုတ် #define ကို ရွေးပါ၊ type ပါတဲ့ read-only value လိုရင် const ကို ရွေးပါ။

အထွေထွေ ပုံစံ

c
#define NAME value                /* no type, no = and no ; */
#define NAME_EXPR (a + b)          /* expressions in parentheses */

enum { CONSTANT_A = 1, CONSTANT_B = 2 };   /* named int constants */

int main(void) {
  const type NAME = value;         /* typed, read-only, scoped */
  return 0;
}

#define မှာ = လည်း မပါ semicolon လည်း မပါပါဘူး၊ expression ဆိုရင် parentheses နဲ့ ပတ်ပါ။ enum က comma ခြားပြီး int constant အများကြီးကို တစ်ခါတည်း ကြေညာနိုင်ပါတယ်။ const ကတော့ ရိုးရိုး declaration ရှေ့မှာ const ထည့်ပြီး ကြေညာတဲ့နေရာမှာပဲ value ပေးရပါတယ်။

Example: constant သုံးမျိုးလုံး အလုပ်လုပ်ပုံ

ဒီ program က booking system သေးသေးလေး တစ်ခုရဲ့ ဂဏန်းတွေကို တွက်ပါတယ်။ ထိုင်ခုံ limit က #define၊ တစ်ပတ်မှာ ရက်အရေအတွက်က enum၊ အချိန်နဲ့ PI က const ဖြစ်ပါတယ်။ Calculation ထဲမှာ magic number တစ်ခုမှ မပါဘဲ ဂဏန်းတိုင်းမှာ နာမည်ရှိနေတာကို သတိထားကြည့်ပါ — ကျန်နေတဲ့ 3 နဲ့ 2 က ဒီ calculation အတွက်ပဲ ဖြစ်တဲ့ input value တွေပါ။

constants.c
#include <stdio.h>

#define MAX_SEATS 40
enum { DAYS_PER_WEEK = 7 };

int main(void) {
  const int MINUTES_PER_HOUR = 60;
  const double PI = 3.14159;
  int booked_today = 33;

  printf("Minutes in 3 hours: %d\n", 3 * MINUTES_PER_HOUR);
  printf("PI: %.2f\n", PI);
  printf("Circle area (r = 2): %.2f\n", PI * 2 * 2);
  printf("Seats left: %d of %d\n", MAX_SEATS - booked_today, MAX_SEATS);
  printf("Seats per week: %d\n", MAX_SEATS * DAYS_PER_WEEK);
  return 0;
}

Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်

  1. Line 3#define MAX_SEATS 40

    Compile မလုပ်ခင် MAX_SEATS ဆိုတဲ့ စာသားတိုင်းကို 40 နဲ့ အစားထိုးပါတယ်။ = မပါ၊ semicolon မပါတာကို သတိပြုပါ။

  2. Line 4enum { DAYS_PER_WEEK = 7 };

    နာမည်မပါတဲ့ enum ထဲမှာ int constant တစ်ခု ကြေညာတာပါ။ 7 ကို နာမည်တစ်ခုနဲ့ ချိတ်ထားပြီး case label နဲ့ array size မှာလည်း သုံးလို့ ရပါတယ်။

  3. Lines 7-8const double PI = 3.14159;

    const variable တွေမှာ type ရှိပြီး main ထဲမှာပဲ မြင်ရပါတယ်။ PI = 3; လို ရေးရင် compile error ဖြစ်ပါတယ်။ PI က double ဖြစ်လို့ enum နဲ့ မရဘဲ const ကို သုံးရပါတယ်။

  4. Line 13PI * 2 * 2

    အချင်းဝက် 2 ရှိတဲ့ စက်ဝိုင်းရဲ့ ဧရိယာ (π r²) ပါ။ 3.14159 × 4 = 12.56636 ကို %.2f က 12.57 အဖြစ် round လုပ်ပါတယ်။

  5. Line 15MAX_SEATS * DAYS_PER_WEEK

    Preprocessor ပြီးသွားရင် ဒါက 40 * DAYS_PER_WEEK ဖြစ်သွားပြီး compiler က 280 ကို တွက်ပါတယ်။ ထိုင်ခုံ limit ပြောင်းရင် line 3 တစ်ကြောင်းကိုပဲ ပြင်ရပါမယ်။

You should see
Minutes in 3 hours: 180
PI: 3.14
Circle area (r = 2): 12.57
Seats left: 7 of 40
Seats per week: 280

Macro က စာသား ကူးထည့်တာသာ ဖြစ်တယ်

Preprocessor က math မသိပါဘူး၊ စာသားကိုပဲ ကူးထည့်ပါတယ်။ #define BAD_TOTAL 2 + 3 ပြီးရင် BAD_TOTAL * 10 က 2 + 3 * 10 ဖြစ်သွားပြီး * က + ထက် အရင်လုပ်တာမို့ 50 မဟုတ်ဘဲ 32 ရပါတယ်။ Parentheses နဲ့ ပတ်ထားတဲ့ GOOD_TOTAL ကတော့ (2 + 3) * 10 ဖြစ်လို့ မှန်ကန်တဲ့ 50 ကို ရပါတယ်။ Run ကြည့်ပြီး ကိုယ့်မျက်စိနဲ့ စစ်ကြည့်ပါ။

macro_parens.c
#include <stdio.h>

#define BAD_TOTAL 2 + 3
#define GOOD_TOTAL (2 + 3)

int main(void) {
  printf("BAD_TOTAL * 10 = %d\n", BAD_TOTAL * 10);
  printf("GOOD_TOTAL * 10 = %d\n", GOOD_TOTAL * 10);
  return 0;
}

Semicolon ကလည်း အလားတူ ကူးထည့်ခံရပါတယ်။ #define MAX_SEATS 40; ဆိုရင် int left = MAX_SEATS - 7; က int left = 40; - 7; ဖြစ်သွားပါတယ်။ ဒါက compile တောင် ဖြစ်ပါသေးတယ် — GCC က warning: statement with no effect လို့ပဲ ပြောပြီး left က 33 မဟုတ်ဘဲ 40 ဖြစ်နေပါတယ်။ Macro definition ရဲ့ အဆုံးမှာ semicolon ဘယ်တော့မှ မထည့်ပါနဲ့။

လက်တွေ့ project တွေမှာ

Real project တွေမှာ ဘယ်လို သုံးလဲ

C project ကြီးတွေမှာ configuration limit တွေကို header file ထဲက #define တွေနဲ့ စုထားလေ့ ရှိပါတယ် — #define BUFFER_SIZE 256 ဒါမှမဟုတ် #define MAX_CLIENTS 64 လိုမျိုးပါ။ Header ကို include လုပ်တဲ့ .c file တိုင်းက value တစ်ခုတည်းကို မြင်ရပါတယ်။ Header က definition ကို #ifndef MAX_CLIENTS နဲ့ ပတ်ထားရင် build system က gcc -DMAX_CLIENTS=128 လို flag နဲ့ ပြောင်းပေးနိုင်ပါသေးတယ်။ ဆက်စပ်နေတဲ့ code တွေ — HTTP status code၊ error code၊ menu option — ကိုတော့ enum နဲ့ အုပ်စုဖွဲ့ပြီး switch ထဲမှာ case label အဖြစ် သုံးပါတယ်။

Scientific နဲ့ engineering code မှာတော့ physical constant တွေကို const double နဲ့ ရေးလေ့ ရှိပါတယ်၊ type ပါပြီး debugger ထဲမှာ မြင်ရလို့ပါ။ သတိထားစရာ တစ်ခုက M_PI ပါ — အဲဒါက ISO C standard ရဲ့ အပိုင်း မဟုတ်ဘဲ POSIX extension ဖြစ်လို့ gcc -std=c17 နဲ့ compile လုပ်ရင် error: 'M_PI' undeclared ဖြစ်နိုင်ပါတယ်။ ဒါကြောင့် project အတော်များများက ကိုယ်ပိုင် PI constant ကို တစ်ခါ define လုပ်ပြီး နေရာတိုင်းမှာ ပြန်သုံးပါတယ်။

အမှားများ

အဖြစ်များတဲ့ အမှားတွေ

1. Macro ရဲ့ အဆုံးမှာ semicolon ထည့်တာ

#define MAX_SEATS 40; က MAX_SEATS ကို သုံးတဲ့ နေရာတိုင်းမှာ ; ကိုပါ ကူးထည့်ပါတယ်။ တချို့နေရာမှာ syntax error ဖြစ်ပြီး int left = MAX_SEATS - 7; လို နေရာမျိုးမှာတော့ warning တစ်ခုနဲ့ပဲ compile ဖြစ်ပြီး value မှားသွားပါတယ်။

2. Expression macro ကို parentheses မပတ်တာ

#define TOTAL 2 + 3 ပြီးရင် TOTAL * 10 က 50 မဟုတ်ဘဲ 32 ပါ။ Expression ပါတဲ့ macro တိုင်းကို (2 + 3) လို အပြင်ဘက်ကနေ parentheses နဲ့ ပတ်ပါ၊ ဒါမှမဟုတ် const variable ကို သုံးပါ။

3. const int ကို compile-time constant လို သုံးတာ

const int CODE = 2; ပြီးမှ case CODE: လို့ ရေးရင် GCC က error: case label does not reduce to an integer constant လို့ ပြောပါတယ်။ File scope မှာ int table[SIZE]; ရေးရင်လည်း error: variably modified 'table' at file scope ရပါတယ်။ ဒီနေရာတွေမှာ enum ဒါမှမဟုတ် #define ကို သုံးပါ။

Best Practices

အကောင်းဆုံး အလေ့အကျင့်များ

  • ဂဏန်းတစ်ခုမှာ အဓိပ္ပာယ်ရှိရင် (rate, limit, unit) နာမည်ပေးပါ၊ 0 နဲ့ 1 လို ကိုယ်တိုင် ရှင်းနေတဲ့ value တွေကိုတော့ ချန်ထားလို့ ရပါတယ်။
  • Double value တွေအတွက် const၊ ဆက်စပ် integer code တွေအတွက် enum ကို အရင် စဉ်းစားပါ။
  • #define ကို သုံးရင် expression တိုင်းကို parentheses နဲ့ ပတ်ပြီး semicolon မထည့်ပါနဲ့။
  • Constant နာမည်တွေကို UPPER_SNAKE_CASE နဲ့ ရေးပြီး unit ကို နာမည်ထဲ ထည့်ပါ (TIMEOUT_MS, MAX_WEIGHT_KG)။

Mini challenge

Mini challenge: parking fee တွက်စက်

Car park တစ်ခုက ပထမ 15 မိနစ်ကို အခမဲ့ ပေးပြီး ကျန်တဲ့ အချိန်ကို တစ်နာရီ $1.50 နှုန်းနဲ့ ကောက်ပါတယ်။ 135 မိနစ် ရပ်ထားတဲ့ ကားအတွက် fee ကို calculation ထဲမှာ magic number မပါဘဲ တွက်ပါ။

  • အခမဲ့ မိနစ် (15) ကို #define နဲ့ ရေးရမယ်
  • တစ်နာရီမှာ မိနစ် (60) ကို enum constant နဲ့ ရေးရမယ်
  • တစ်နာရီ နှုန်း (1.50) ကို const double နဲ့ ရေးရမယ်
  • Billable hours နဲ့ fee ကို ဒသမ ၂ နေရာနဲ့ print ထုတ်ရမယ်
parking.c
#include <stdio.h>

/* TODO: #define for the free minutes */
/* TODO: enum constant for minutes per hour */

int main(void) {
  /* TODO: const double for the hourly rate */
  int parked_minutes = 135;

  /* TODO: billable minutes, billable hours (keep the fraction), fee */

  /* TODO: print parked minutes, billable hours and the fee */
  return 0;
}

ရလဒ်: 135 မိနစ်အတွက် ဒီ output ရပါမယ်: Parked: 135 min (15 free) Billable hours: 2.00 Fee: $3.00

အရိပ်အမြွက် ကြည့်ရန်

Billable minutes ကို hours အဖြစ် ပြောင်းတဲ့အခါ int / enum ဖြစ်နေလို့ fraction ပျောက်နိုင်ပါတယ်။ (double) cast တစ်ခု ထည့်ပါ။

အဖြေ ကြည့်ရန်
parking.c
#include <stdio.h>

#define FREE_MINUTES 15
enum { MINUTES_PER_HOUR = 60 };

int main(void) {
  const double RATE_PER_HOUR = 1.50;
  int parked_minutes = 135;

  int billable_minutes = parked_minutes - FREE_MINUTES;
  double billable_hours = (double)billable_minutes / MINUTES_PER_HOUR;
  double fee = billable_hours * RATE_PER_HOUR;

  printf("Parked: %d min (%d free)\n", parked_minutes, FREE_MINUTES);
  printf("Billable hours: %.2f\n", billable_hours);
  printf("Fee: $%.2f\n", fee);
  return 0;
}

Quick Quiz 1 / 3

#define SIZE 4 + 1 ပြီးရင် printf("%d\n", SIZE * 2); က ဘာ print ထုတ်မလဲ။

Quick Quiz 2 / 3

const int LIMIT = 10; ကို file scope မှာ ကြေညာပြီးနောက် ဘယ်ဟာက C17 မှာ compile error ဖြစ်မလဲ။

Quick Quiz 3 / 3

Order status code လေးခု (PENDING, PAID, SHIPPED, CANCELLED) ကို switch ထဲမှာ case label အဖြစ် သုံးမယ်ဆိုရင် ဘယ်ဟာ အသင့်တော်ဆုံးလဲ။

မှတ်ထားရမယ့် အချက်များ

  • Magic number တွေကို နာမည်ပေးထားရင် အဓိပ္ပာယ် ရှင်းပြီး ပြောင်းစရာ နေရာ တစ်ခုတည်းပဲ ကျန်ပါတယ်။
  • const က type နဲ့ scope ရှိတဲ့ read-only variable ဖြစ်ပေမယ့် C မှာ case label နဲ့ file-scope array size အဖြစ် မရပါဘူး။
  • #define က preprocessor ရဲ့ စာသား အစားထိုးမှုဖြစ်လို့ parentheses ပတ်ပြီး semicolon မထည့်ရပါဘူး။
  • enum က integer constant expression ဖြစ်တဲ့ int constant တွေကို ပေးလို့ code တွေ၊ size တွေအတွက် အကောင်းဆုံးပါ။

GNU C compiler diagnostics — GNU Project

နောက်ဆုံးပြင်ဆင်ရက်

Progress ကို ဤ browser (ဤစက်) တွင်သာ သိမ်းထားပါသည်။ အခြားစက်များနှင့် sync မလုပ်ပါ။

ဤသင်ခန်းစာတွင် အမှားတွေ့ပါက အကြောင်းကြားရန်သင့် email app ကို ဤသင်ခန်းစာလိပ်စာ ဖြည့်ပြီးသားဖြင့် ဖွင့်ပေးပါမည် — သင်ကိုယ်တိုင် ပို့မှသာ ပို့မည်ဖြစ်သည်။

Constants | Thuta Learning