ရိုးရိုးလေး ရှင်းပြမယ်
ရိုးရိုးလေး ရှင်းရရင်
Operator တွေက expression တွေရဲ့ ကြိယာ (verb) တွေပါ — value တွေကို ယူပြီး တစ်ခုခု လုပ်ပေးပါတယ်။ Arithmetic operator (+ - * / %) တွေက တွက်ချက်ပေးပြီး comparison operator (== != < > <= >=) တွေက မေးခွန်းမေးကာ 1 (မှန်) ဒါမှမဟုတ် 0 (မှား) ပြန်ပေးပါတယ်။ Logical operator (&& || !) တွေက အဲဒီ အဖြေတွေကို ပေါင်းစပ်ပေးပါတယ်။
Assignment operator (= += -= ...) တွေကတော့ ရလဒ်ကို variable ထဲ ပြန်သိမ်းပေးပါတယ်။ အများစုက သင်္ချာ အတန်းက သိပြီးသားလိုပဲ အလုပ်လုပ်ပေမယ့် C မှာ အံ့ဩစရာ နှစ်ခုသုံးခု ရှိပါတယ် — int နှစ်ခုကို စားရင် အကြွင်းကို ဖြတ်ပစ်တာ၊ = နဲ့ == က လုံးဝ မတူတာ စတာတွေပါ။ ဒီ lesson က အဲဒါတွေကို ကြိုသိအောင် ပြင်ဆင်ပေးပါမယ်။
Problem → Why → Solution
ပြဿနာ: 90% အောင်ထားတာကို 0% လို့ ပြတာ
Test 50 ခုထဲက 45 ခု အောင်တဲ့ ရာခိုင်နှုန်းကို dashboard မှာ ပြချင်တယ် ဆိုပါစို့။ သင်္ချာအရ 45 / 50 × 100 = 90 ဖြစ်လို့ ဒီ formula ကို code ထဲ တိုက်ရိုက် ကူးရေးလိုက်ပါတယ်။ Compiler က warning တစ်ခုမှ မပြဘဲ program က Passed: 0% လို့ ထုတ်ပါတယ်။ Formula က မှန်ပေမယ့် C က အဲဒါကို သင်္ချာ ဆရာလို မဟုတ်ဘဲ ကိုယ်ပိုင် စည်းမျဉ်းတွေနဲ့ တွက်လို့ပါ။
int passed = 45;
int total = 50;
int percent = passed / total * 100; /* 45 / 50 is 0, then 0 * 100 */
printf("Passed: %d%%\n", percent); /* prints Passed: 0% */ဘာကြောင့် 0 ဖြစ်သွားတာလဲ
/ နဲ့ * က precedence တူပြီး ဘယ်ကနေ ညာကို တွက်လို့ passed / total ကို အရင် တွက်ပါတယ်။ Operand နှစ်ခုလုံး int ဖြစ်လို့ 45 / 50 က integer division ဖြစ်ပြီး 0.9 ရဲ့ fraction ကို ဖြတ်ပစ်တော့ 0 ရပါတယ်။ အဲဒီနောက် 0 * 100 က 0 ပါ။ အစဉ်ကို ပြောင်းပြီး passed * 100 / total (= 90) လို့ ရေးတာ၊ ဒါမှမဟုတ် 100.0 * passed / total လို့ double နဲ့ တွက်တာက ပြဿနာကို ဖြေရှင်းပါတယ်။
Core concept: operator တွေ ဘယ်လို တွက်လဲ
Integer နှစ်ခုကို / နဲ့ စားရင် ရလဒ်ကို သုည ဘက်ကို ဖြတ်ပါတယ် (C99 ကစပြီး standard က ဒီလို သတ်မှတ်ထားပါတယ်) — 7 / 2 က 3၊ -7 / 2 က -4 မဟုတ်ဘဲ -3 ပါ။ % က အကြွင်းကို ပေးပြီး သူ့ sign က ဘယ်ဘက် operand နဲ့ တူပါတယ် — -7 % 2 က -1 ပါ။ ဒီနှစ်ခုက (a / b) * b + a % b == a ဆိုတဲ့ စည်းမျဉ်းကို အမြဲ လိုက်နာပါတယ်။ % က integer တွေနဲ့ပဲ အလုပ်လုပ်ပြီး (double အတွက် <math.h> ရဲ့ fmod ကို သုံးပါ) integer ကို သုညနဲ့ စားတာ ဒါမှမဟုတ် % 0 လုပ်တာက undefined behaviour ပါ။
Expression တစ်ခုမှာ operator အများကြီး ပါရင် precedence က ဘယ်ဟာ အရင်လုပ်မလဲ ဆုံးဖြတ်ပြီး precedence တူရင် arithmetic operator တွေက ဘယ်ကနေ ညာကို အုပ်စုဖွဲ့ပါတယ်။ အောက်က table မှာ အမြင့်ဆုံးကနေ အနိမ့်ဆုံးကို စီထားပါတယ်။ ထောင်ချောက်တစ်ခုက bitwise & ပါ — သူက == ထက် precedence နိမ့်လို့ flags & 4 == 0 က flags & (4 == 0) အဖြစ် parse ဖြစ်ပြီး GCC -Wall က suggest parentheses around comparison in operand of '&' လို့ သတိပေးပါတယ်။ မသေချာရင် parentheses ထည့်ပါ။
Comparison နဲ့ logical operator တွေက int 1 ဒါမှမဟုတ် 0 ကို ပြန်ပေးပါတယ်။ && နဲ့ || က short-circuit လုပ်ပါတယ် — ဘယ်ဘက်ခြမ်းက ရလဒ်ကို ဆုံးဖြတ်ပြီးသား ဖြစ်ရင် ညာဘက်ခြမ်းကို လုံးဝ မတွက်ပါဘူး။ ဒါကြောင့် count != 0 && total / count > 50 က count သုည ဖြစ်တဲ့အခါ စားတာကို ဘယ်တော့မှ မလုပ်လို့ ဘေးကင်းပါတယ်။
Type ရောနေရင် C က မတွက်ခင် operand တွေကို ပြောင်းပါတယ်။ char နဲ့ short တွေက int အဖြစ် အရင် promote ဖြစ်ပြီး (integer promotion) operand တစ်ခုက double ဖြစ်ရင် ကျန်တစ်ခုကိုလည်း double ပြောင်းပါတယ်။ int နဲ့ unsigned int ရောရင်တော့ int က unsigned ဖြစ်သွားလို့ -1 < 10u ဆိုတာ မှား (0) ဖြစ်ပါတယ်၊ -1 က 4294967295 ဖြစ်သွားလို့ပါ။ နောက်ဆုံးအနေနဲ့ x += 5 က x = x + 5 ရဲ့ အတိုကောက်ဖြစ်ပြီး ++ နဲ့ -- က 1 တိုး/လျှော့ပါတယ် — ဒါပေမယ့် expression တစ်ခုတည်းထဲမှာ variable တစ်ခုကို နှစ်ခါ မပြင်ပါနဲ့။
| အုပ်စု (မြင့် ကနေ နိမ့်) | Operator များ |
|---|---|
| Unary | ! - ++ -- (type) |
| မြှောက်/စား | * / % |
| ပေါင်း/နုတ် | + - |
| နှိုင်းယှဉ် | < <= > >= |
| တူ/မတူ | == != |
| Logical | && ပြီးမှ || |
| Assignment (အနိမ့်ဆုံး) | = += -= *= /= %= |
- Operand type တွေကို ညှိ (promote/convert)
- Precedence အမြင့်ဆုံး operator ကို အရင်တွက်
- Precedence တူရင် ဘယ်ကနေ ညာ
- && / || မှာ အဖြေသိရင် ရပ်
int / int ကို မြင်တိုင်း "fraction ပျောက်မယ်" လို့ အလိုလို စဉ်းစားတတ်အောင် လေ့ကျင့်ပါ။
အထွေထွေ ပုံစံ
result = a + b; /* also - * / % */
flag = a < b; /* also <= > >= == != : gives 1 or 0 */
both = cond1 && cond2;
either = cond1 || cond2;
opposite = !cond;
total += amount; /* total = total + amount */
counter++; /* counter = counter + 1 */Arithmetic operator တွေက value အသစ် တစ်ခု ထုတ်ပေးပြီး comparison နဲ့ logical operator တွေက 1 ဒါမှမဟုတ် 0 ထုတ်ပေးပါတယ်။ Compound assignment (+= စတာ) နဲ့ ++ တို့ကတော့ variable ကို နေရာမှာတင် ပြင်ပါတယ်။
Example: operator အုပ်စုတိုင်းကို စမ်းကြည့်ခြင်း
x = 10 နဲ့ y = 3 ကို သုံးပြီး ဒီ program က arithmetic, comparison, logical, compound assignment operator တွေကို တစ်ခုချင်း ပြပါတယ်။ Run မလုပ်ခင် line တစ်ကြောင်းချင်းရဲ့ output ကို စာရွက်ပေါ်မှာ ကြိုခန့်မှန်းကြည့်ပါ — အထူးသဖြင့် Divide နဲ့ -7 ပါတဲ့ line ကိုပါ။
#include <stdio.h>
int main(void) {
int x = 10;
int y = 3;
printf("Add: %d\n", x + y);
printf("Divide: %d\n", x / y);
printf("Remainder: %d\n", x % y);
printf("Real divide: %.3f\n", (double)x / y);
printf("-7 / 2 = %d, -7 %% 2 = %d\n", -7 / 2, -7 % 2);
printf("Is x greater than y? %d\n", x > y);
printf("x == 10 && y > 5: %d\n", x == 10 && y > 5);
x += 5;
printf("After x += 5: %d\n", x);
return 0;
}Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်
- Lines 8-9
x / y10 / 3 က integer division ဖြစ်လို့ 3 ရပြီး 10 % 3 က အကြွင်း 1 ပါ။ 3 * 3 + 1 = 10 ဆိုတော့ (a / b) * b + a % b == a စည်းမျဉ်းနဲ့ ကိုက်ပါတယ်။
- Line 10
(double)x / yCast က division မတိုင်ခင် x ကို double ပြောင်းလို့ y ကလည်း double ဖြစ်သွားပြီး 3.333... ရပါတယ်။ (double)(x / y) လို့ ရေးရင်တော့ 3 ကို ပြောင်းတာမို့ 3.000 ပဲ ရပါမယ်။
- Line 11
printf("-7 / 2 = %d, -7 %% 2 = %d\n", -7 / 2, -7 % 2);-7 / 2 က သုည ဘက်ကို ဖြတ်လို့ -3၊ -7 % 2 က ဘယ်ဘက် operand ရဲ့ sign ကို ယူလို့ -1 ပါ။ Format string ထဲမှာ % စာလုံး တစ်လုံးကို print ထုတ်ဖို့ %% လို့ နှစ်ခါ ရေးရပါတယ်။
- Lines 12-13
x == 10 && y > 5x > y က မှန်လို့ 1 ပါ။ x == 10 က မှန်ပေမယ့် y > 5 က မှားလို့ && ရဲ့ ရလဒ် 0 ပါ။ == က နှိုင်းယှဉ်တာ ဖြစ်ပြီး x ကို မပြောင်းပါဘူး။
- Line 14
x += 5;x = x + 5 ရဲ့ အတိုကောက်ပါ။ x က 10 ကနေ 15 ဖြစ်သွားလို့ နောက်ဆုံး line က After x += 5: 15 ကို ပြပါတယ်။
Add: 13
Divide: 3
Remainder: 1
Real divide: 3.333
-7 / 2 = -3, -7 % 2 = -1
Is x greater than y? 1
x == 10 && y > 5: 0
After x += 5: 15Short-circuit, wraparound နဲ့ promotion ကို လက်တွေ့ ကြည့်ခြင်း
အောက်က program မှာ count က 0 ဖြစ်ပေမယ့် program က crash မဖြစ်ပါဘူး — && ရဲ့ ဘယ်ဘက်ခြမ်း count != 0 က မှားတာနဲ့ ညာဘက်က division ကို ကျော်သွားလို့ပါ။ Unsigned stock ကနေ 1 နုတ်လိုက်ရင် အနုတ်မဖြစ်ဘဲ အကြီးဆုံး unsigned value ကို ပတ်လည်သွားပါတယ် (unsigned int က 32 bit ဖြစ်တဲ့ mainstream platform တွေမှာ 4294967295)။ ဒါက standard က သတ်မှတ်ထားတဲ့ behaviour ဖြစ်ပြီး signed int overflow နဲ့ မတူပါဘူး။ char နှစ်ခု ပေါင်းတာကတော့ int အဖြစ် promote ဖြစ်ပြီးမှ တွက်လို့ 200 ကို မှန်မှန်ကန်ကန် ရပါတယ်။
#include <stdio.h>
int main(void) {
int count = 0;
int total = 120;
/* && stops at the first false operand, so total / count never runs */
printf("Safe check: %d\n", count != 0 && total / count > 50);
unsigned int stock = 0;
stock = stock - 1; /* unsigned arithmetic wraps around */
printf("0 - 1 as unsigned: %u\n", stock);
printf("-1 converted to unsigned: %u\n", (unsigned int)-1);
char a = 100, b = 100;
int sum = a + b; /* both chars become int first */
printf("char 100 + char 100 = %d\n", sum);
return 0;
}လက်တွေ့ project တွေမှာ
Real project တွေမှာ ဘယ်လို သုံးလဲ
/ နဲ့ % ကို အချိန်နဲ့ unit ပြောင်းတဲ့ နေရာတိုင်းမှာ တွေ့ရပါတယ် — စက္ကန့်ကို နာရီ၊ မိနစ်၊ စက္ကန့် ခွဲတာ၊ cents ကို dollar နဲ့ cent ခွဲတာ စတာတွေပါ။ % ကို ring buffer ထဲမှာ index = (index + 1) % SIZE လို index ကို အစကို ပြန်ပတ်ဖို့၊ n % 2 == 0 နဲ့ စုံ/မ စစ်ဖို့ သုံးပါတယ်။ Pagination မှာ page အရေအတွက်ကို (items + per_page - 1) / per_page လို့ တွက်တာက integer division ကို အပေါ်ဘက် round လုပ်တဲ့ နည်းလမ်း တစ်ခုပါ။
Short-circuit evaluation က guard တွေရဲ့ အခြေခံပါ။ Production code မှာ "အရင် စစ်၊ ပြီးမှ သုံး" ပုံစံကို နေရာတိုင်းမှာ တွေ့ရပါတယ် — သုညနဲ့ မစားခင် count != 0 &&၊ နောက်ပိုင်း lesson တွေမှာ တွေ့ရမယ့် pointer တစ်ခုကို မသုံးခင် ptr != NULL && လိုမျိုးပါ။ Signed နဲ့ unsigned ရောတဲ့ bug ကလည်း အမှန်တကယ် ဖြစ်ပါတယ် — size_t (unsigned) ဖြစ်တဲ့ length တစ်ခုကနေ နုတ်လိုက်ရင် အနုတ် မဖြစ်ဘဲ ဂဏန်းကြီး ဖြစ်သွားတတ်လို့ -Wextra ရဲ့ sign-compare warning တွေကို လျစ်လျူ မရှုပါနဲ့။
အမှားများ
အဖြစ်များတဲ့ အမှားတွေ
1. == နေရာမှာ = ရေးတာ
if (x = 5) က x ကို 5 နဲ့ assign လုပ်ပြီး assignment ရဲ့ value 5 က သုည မဟုတ်လို့ condition က အမြဲ မှန်ပါတယ်။ GCC -Wall က suggest parentheses around assignment used as truth value လို့ သတိပေးပါတယ်။ နှိုင်းယှဉ်ချင်ရင် == ကို သုံးပါ။
2. Signed နဲ့ unsigned ကို နှိုင်းယှဉ်တာ
int n = -1; unsigned int limit = 10; ဆိုရင် n < limit က 0 (မှား) ပါ၊ n က unsigned ဖြစ်သွားပြီး 4294967295 ဖြစ်လို့ပါ။ -Wextra က comparison of integer expressions of different signedness: 'int' and 'unsigned int' လို့ ပြောပါတယ်။ Type နှစ်ခုကို တူအောင် ထားပါ။
3. Variable တစ်ခုကို expression တစ်ခုထဲ နှစ်ခါ ပြင်တာ
i = i++ + 1; လို code က undefined behaviour ပါ — i ကို ++ နဲ့ရော = နဲ့ပါ ပြင်ပြီး ဘယ်ဟာ အရင်ဖြစ်မလဲ standard က မသတ်မှတ်ပါဘူး။ GCC က operation on 'i' may be undefined လို့ သတိပေးပါတယ်။ Step တစ်ခုချင်းကို statement သီးသီးခြားခြား ခွဲရေးပါ။
Best Practices
အကောင်းဆုံး အလေ့အကျင့်များ
- ရာခိုင်နှုန်းလို ratio တွေကို တွက်တဲ့အခါ မစားခင် အရင်မြှောက်ပါ (passed * 100 / total)၊ ဒါမှမဟုတ် double နဲ့ တွက်ပါ။
- Precedence ကို သံသယ ဖြစ်တာနဲ့ parentheses ထည့်ပါ — ဖတ်တဲ့သူ အတွက်ပါ ပိုရှင်းပါတယ်။
- Division တိုင်းရဲ့ ရှေ့မှာ divisor သုည ဖြစ်နိုင်လား မေးပြီး && guard ထည့်ပါ။
- ++ နဲ့ -- ကို သူ့ statement သူ ထားပါ၊ ရှုပ်ထွေးတဲ့ expression ထဲ မထည့်ပါနဲ့။
Mini challenge
Mini challenge: စက္ကန့်ခွဲစက်နဲ့ အမှတ်စာရင်း
စုစုပေါင်း စက္ကန့် 7384 ကို နာရီ၊ မိနစ်၊ စက္ကန့် ခွဲပြီး test 50 ခုထဲက 45 ခု အောင်တဲ့ ရာခိုင်နှုန်းကို မှန်မှန်ကန်ကန် ပြတဲ့ program ရေးပါ။ Logical operator တွေကိုလည်း သုံးပါ။
- တစ်နာရီ စက္ကန့် (3600) နဲ့ တစ်မိနစ် စက္ကန့် (60) ကို enum constant အဖြစ် ရေးရမယ်
- နာရီ၊ မိနစ်၊ စက္ကန့် ကို / နဲ့ % ကိုပဲ သုံးပြီး တွက်ရမယ်
- ရာခိုင်နှုန်းကို int နဲ့ 90% လို့ မှန်မှန် ပြရမယ် (%% ကို သုံးပါ)
- စက္ကန့် စုံကိန်း ဟုတ်မဟုတ်နဲ့ "အနည်းဆုံး 40 ခု အောင်ပြီး 3 နာရီ အောက်လား" ကို 1/0 နဲ့ ပြရမယ်
#include <stdio.h>
/* TODO: enum constants for seconds per hour and seconds per minute */
int main(void) {
int total_seconds = 7384;
/* TODO: hours, minutes, seconds using / and % */
int passed = 45;
int total = 50;
/* TODO: print "7384 s = ? h ? min ? s" */
/* TODO: print the pass rate (multiply before dividing) */
/* TODO: print the even check and the && check as 1 or 0 */
return 0;
}ရလဒ်: ဒီ output လေးကြောင်း ထွက်ရပါမယ်: 7384 s = 2 h 3 min 4 s Score: 90% Even number of seconds? 1 Passed and under 3 h? 1
အရိပ်အမြွက် ကြည့်ရန်
မိနစ်အတွက် အရင်ဆုံး နာရီ ပြည့်ပြီး ကျန်တဲ့ စက္ကန့်ကို % နဲ့ ယူပြီးမှ 60 နဲ့ စားပါ။ ရာခိုင်နှုန်းမှာ passed / total ကို အရင် မတွက်မိအောင် သတိထားပါ။
အဖြေ ကြည့်ရန်
#include <stdio.h>
enum { SECONDS_PER_HOUR = 3600, SECONDS_PER_MINUTE = 60 };
int main(void) {
int total_seconds = 7384;
int hours = total_seconds / SECONDS_PER_HOUR;
int minutes = total_seconds % SECONDS_PER_HOUR / SECONDS_PER_MINUTE;
int seconds = total_seconds % SECONDS_PER_MINUTE;
int passed = 45;
int total = 50;
printf("%d s = %d h %d min %d s\n", total_seconds, hours, minutes, seconds);
printf("Score: %d%%\n", passed * 100 / total);
printf("Even number of seconds? %d\n", total_seconds % 2 == 0);
printf("Passed and under 3 h? %d\n", passed >= 40 && hours < 3);
return 0;
}Quick Quiz 1 / 3
Quick Quiz 2 / 3
Quick Quiz 3 / 3
မှတ်ထားရမယ့် အချက်များ
- int / int က သုည ဘက်ကို ဖြတ်ပြီး % ရဲ့ sign က ဘယ်ဘက် operand နဲ့ တူပါတယ် (-7 / 2 = -3, -7 % 2 = -1)။
- Precedence တူတဲ့ * / % တွေက ဘယ်ကနေ ညာကို တွက်လို့ passed / total * 100 က 0 ဖြစ်တတ်ပါတယ်၊ အရင် မြှောက်ပါ။
- Comparison နဲ့ logical operator တွေက 1/0 ပြန်ပေးပြီး && နဲ့ || က short-circuit လုပ်လို့ guard အဖြစ် သုံးနိုင်ပါတယ်။
- Unsigned arithmetic က ပတ်လည်ပြီး signed overflow က undefined ဖြစ်ကာ signed/unsigned ရောတဲ့ comparison က ထောင်ချောက်ပါ။
GNU C compiler diagnostics — GNU Project