ရိုးရိုးလေး ရှင်းပြမယ်
ရိုးရိုးလေး ရှင်းရရင်
Language တိုင်းမှာ grammar ရှိပြီး C ရဲ့ grammar က တင်းကျပ်ပါတယ်။ C program တစ်ခုကို statement တွေနဲ့ ဖွဲ့စည်းထားပြီး statement ဆိုတာ language ရဲ့ ဝါကျတွေပါ။ မြန်မာဝါကျတစ်ကြောင်း ပုဒ်မ (။) နဲ့ ဆုံးသလို statement တစ်ခုချင်းက semicolon နဲ့ ဆုံးပါတယ်။ ဆက်စပ်နေတဲ့ statement တွေကို curly brace { } ထဲမှာ block အဖြစ် စုထားပြီး main ပိုင်တဲ့ block က program စတင်တဲ့ နေရာပါ။
Compiler က ကိုယ့် file ကို စာလုံးအတိုင်း တိတိကျကျ စစ်တဲ့ proofreader တစ်ယောက်လို ဖတ်ပါတယ်။ ကိုယ်ဘာဆိုလိုချင်လဲ ခန့်မှန်းမပေးပါဘူး — semicolon တစ်ခု ကျန်တာ၊ brace တစ်ခု ပိုနေတာ ဒါမှမဟုတ် စကားလုံးတစ်လုံးလုံးကို single quote နဲ့ ဝိုင်းထားတာ တစ်ခုတည်းနဲ့ build ရပ်သွားနိုင်ပါတယ်။ သတင်းကောင်းကတော့ စည်းမျဉ်းတွေ နည်းနည်းလေးပဲ ရှိပြီး အလေ့အထ ဖြစ်သွားရင် သတိတောင် မထားမိတော့ပါဘူး။
Problem → Why → Solution
ပြဿနာ: semicolon တစ်ခု ကျန်ပြီး error နှစ်ခု ရှုပ်ထွေး
ဒီ program တိုလေးကို ကြည့်ပါ။ အမှားက line 4 ရဲ့ အဆုံးမှာ semicolon ကျန်ခဲ့တာ တစ်ခုတည်းပါ။ ဒါပေမယ့် GCC က ပြဿနာကို line 5 မှာ "expected ',' or ';' before 'int'" လို့ report လုပ်ပြီး line 6 မှာ "'pears' undeclared (first use in this function)" ဆိုတဲ့ ဒုတိယ error ကိုပါ ထပ်ပေးပါတယ် — pears ကို အဲဒီနားမှာပဲ declare လုပ်ထားတာတောင်ပါ။
Beginner တွေက list ရဲ့ အောက်ဆုံးကနေ စပြင်တာ ဒါမှမဟုတ် ဘယ်တုန်းကမှ မမှားခဲ့တဲ့ line 6 ကို ပြန်ရေးတာ မကြာခဏ လုပ်မိကြပါတယ်။ File ကြီးတွေမှာဆိုရင် ချော်မှားမှု တစ်ခုတည်းက screen အပြည့် message တွေ ထုတ်ပေးနိုင်ပြီး တစ်ခုချင်း လိုက်ပြင်နေရင် အချိန်ကုန်ပါတယ်။
#include <stdio.h>
int main(void) {
int apples = 3
int pears = 4;
printf("%d\n", apples + pears);
return 0;
}Error က ဘာကြောင့် line မှားမှာ ပေါ်တာလဲ
Compiler က ကိုယ့် code ကို token အစီအတန်းတစ်ခုအဖြစ် ဖတ်ပြီး semicolon ကျန်နေတာကို နေရာမကျတဲ့ နောက် token ကို ရောက်မှပဲ သတိထားမိပါတယ်။ ဒီမှာ int apples = 3 ကို ဖတ်ပြီး နောက် line က int ကို တွေ့တော့မှ complain လုပ်လို့ line 5 မှာ ပေါ်တာပါ။ နေရာပျောက်သွားပြီးနောက် pears ကို declare လုပ်ထားတယ်လို့ မှတ်မထားမိတော့လို့ နောက်ဆက်တွဲ error ထပ်ထွက်လာတာပါ။ ပထမဆုံး message ကို ပြင်ပြီး ပြန် compile လုပ်ပါ — ကျန်တာတွေ အများအားဖြင့် ပျောက်သွားပါလိမ့်မယ်။
Core concept: C file တစ်ခုကို ဘယ်လို ပေါင်းစပ်ထားလဲ
.c file တစ်ခုကို preprocessor က ကိုင်တွယ်ပြီးသွားရင် translation unit လို့ ခေါ်ပါတယ်။ အဲဒီထဲမှာ အုတ်မြစ်အမျိုးအစား အနည်းငယ် ပါပြီး တစ်ခုချင်းစီမှာ ကိုယ်ပိုင် ပုဒ်ဖြတ်စည်းမျဉ်း ရှိပါတယ်။
| အုတ်မြစ် | ဥပမာ | ဘာနဲ့ ဆုံးလဲ |
|---|---|---|
| Preprocessor directive | #include <stdio.h> | Line အဆုံး၊ semicolon မပါ |
| Declaration | int count = 0; | Semicolon |
| Function definition | int main(void) { ... } | ပိတ်တဲ့ brace၊ semicolon မပါ |
| Statement | printf("Hi\n"); | Semicolon |
| Block (compound statement) | { statement; statement; } | ပိတ်တဲ့ brace |
Directive တွေက C statement လုံးဝ မဟုတ်ပါဘူး — compiler မတိုင်ခင် run တဲ့ preprocessor ဆီ ပေးတဲ့ ညွှန်ကြားချက်တွေဖြစ်ပြီး တစ်ခုချင်းက သူ့ line ရဲ့ အဆုံးမှာ ဆုံးပါတယ်။ ကျန်တာအားလုံးကတော့ free-form ပါ။ Token တွေကြားက space, tab နဲ့ line break တွေက အများအားဖြင့် အရေးမကြီးလို့ printf ခေါ်တာ အရှည်ကြီးကို line နှစ်ကြောင်း ခွဲရေးလို့ရသလို statement သုံးခုကို line တစ်ကြောင်းထဲ ညှပ်ရေးလည်း ရပါတယ်။ Layout က လူဖတ်ဖို့အတွက်ပါ၊ layout ကောင်းရင် indentation ကနေ block ဖွဲ့စည်းပုံကို မြင်ရပါတယ်။
C က case-sensitive ပါ။ printf, Printf နဲ့ PRINTF က နာမည်သုံးခု သီးသီးခြားခြားဖြစ်ပြီး standard library ထဲမှာ ပထမတစ်ခုပဲ ရှိပါတယ်။ ကိုယ်တိုင် တီထွင်တဲ့ နာမည်တွေကို identifier လို့ ခေါ်ပြီး letter, digit နဲ့ underscore တွေ ပါလို့ရပါတယ်၊ digit နဲ့ စလို့ မရသလို int, return, while လို C keyword တွေလည်း ဖြစ်လို့ မရပါဘူး။
Quote တွေမှာလည်း အဓိပ္ပာယ် ရှိပါတယ်။ Double quote က "Hello" လို character အစီအတန်း ဖြစ်တဲ့ string literal ကို ဖန်တီးပါတယ်။ Single quote ကတော့ 'H' လို character တစ်လုံးတည်း ဖြစ်တဲ့ character constant ကို ဖန်တီးပါတယ်။ Compiler က သူတို့ကို type မတူတဲ့အရာတွေအဖြစ် ဆက်ဆံလို့ အပြန်အလှန် လဲသုံးလို့ မရပါဘူး။
နောက်ဆုံးအနေနဲ့ main ကိုယ်တိုင်ပါ။ int main(void) က argument မယူတဲ့ main ကို ရေးတဲ့ standard ပုံစံပါ။ C17 မှာ int main() ကိုလည်း လက်ခံသေးပေမယ့် declaration ထဲက ကွင်းအလွတ်က "parameter တွေကို မသတ်မှတ်ထားဘူး" လို့ ဆိုလိုလို့ compiler က ခေါ်တာတွေကို ပိုလျော့ပြီး စစ်ပါတယ်။ C23 ကတော့ ကွင်းအလွတ်ကို parameter မရှိဘူးလို့ အဓိပ္ပာယ် ပြောင်းလိုက်ပါတယ်။ void ရေးတာက standard တိုင်းမှာ အလုပ်လုပ်ပါတယ်။
- Character တွေက token ဖြစ်လာ
- Token တွေက ; နဲ့ဆုံးတဲ့ statement ဖြစ်
- Statement တွေက { } block ထဲ စုဝေး
- Block တွေက function ဖြစ်၊ main အရင် run
Message တစ်ခုက အဓိပ္ပာယ်မရှိသလို ထင်ရရင် token တစ်ခု နောက်ပြန်ကြည့်ပါ — ; ဒါမှမဟုတ် } ကျန်နေတာကို နောက် token က နေရာမကျမှ compiler က သတိထားမိတာပါ။
အထွေထွေ ပုံစံ
#include <header.h> /* directive: no semicolon */
int main(void) { /* function definition */
type name = value; /* declaration */
function_call(argument); /* statement */
{ /* nested block */
statement;
}
return 0;
} /* no semicolon after a function body */Directive တွေက file ရဲ့ အပေါ်ဆုံးမှာ line တစ်ကြောင်းစီ နေရာယူပါတယ်။ main ရဲ့ body ထဲမှာ declaration နဲ့ statement တွေက semicolon နဲ့ ဆုံးပြီး block တွေကိုတော့ brace တွေနဲ့ ဖွင့်ပိတ်ပါတယ်။ Function body ရဲ့ ပိတ်တဲ့ brace နောက်မှာ semicolon မလိုပါဘူး။
Example: စည်းမျဉ်းတစ်ခုစီကို ပြတဲ့ program
ဒီ program က ထုံးစံ နှုတ်ဆက်စာကို ထားပြီး သရုပ်ပြချက် သေးသေးလေး လေးခု ထပ်ထည့်ထားပါတယ် — character constant တစ်ခု၊ string literal တစ်ခု၊ line နှစ်ကြောင်းပေါ် ဖြန့်ရေးထားတဲ့ statement တစ်ခုနဲ့ nested block တစ်ခု။ Print ထုတ်တဲ့ line တိုင်းက ဘယ်စည်းမျဉ်းကို ပြနေလဲ ပြောပြပါတယ်။
#include <stdio.h>
int main(void) {
printf("Hello World!\n");
printf("A character: %c\n", 'C');
printf("A string: %s\n", "C is fun");
printf("%s has %d letters\n",
"main", 4);
{
printf("Inside a nested block\n");
}
return 0;
}Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်
- Line 1
#include <stdio.h>Preprocessor directive တစ်ခုပါ။ Line ရဲ့ အဆုံးမှာ ဆုံးတာမို့ ဒီမှာ semicolon ထည့်ရင် "extra tokens at end of '#include' directive" ဆိုတဲ့ warning ပဲ ရပါတယ်။
- Line 4
printf("Hello World!\n");ပြည့်စုံတဲ့ statement တစ်ခုပါ — function ခေါ်တာနဲ့ နောက်က semicolon။ Semicolon ကို ဖယ်လိုက်ရင် error ကို နောက် line ရဲ့ အစမှာ report လုပ်ပါလိမ့်မယ်။
- Line 5
printf("A character: %c\n", 'C');Single quote ထဲက 'C' က character တစ်လုံးဖြစ်ပြီး %c နဲ့ print ထုတ်ပါတယ်။
- Line 6
printf("A string: %s\n", "C is fun");Double quote ထဲက "C is fun" က string literal ဖြစ်ပြီး %s နဲ့ print ထုတ်ပါတယ်။ Quote ပုံစံနှစ်ခုကို လဲလိုက်ရင် ဒီ line ပျက်သွားပါလိမ့်မယ်။
- Lines 7-8
printf("%s has %d letters\n",Line နှစ်ကြောင်းပေါ်က statement တစ်ခုတည်းပါ။ Compiler က token တွေကိုပဲ ဂရုစိုက်ပြီး statement က semicolon ရှိတဲ့ line 8 ရဲ့ အဆုံးမှာ ဆုံးပါတယ်။
- Lines 9-11
printf("Inside a nested block\n");main ထဲမှာ block တစ်ခုကို သီးသန့် ထားလို့ရပါတယ်။ သူ့ brace တွေက statement တွေကို စုပေးပြီး နောက် lesson တွေမှာ if, loop နဲ့ function တွေအတွက် ဒီ brace တွေကိုပဲ သုံးပါမယ်။
Hello World!
A character: C
A string: C is fun
main has 4 letters
Inside a nested blockIdentifier နဲ့ keyword များ
| နာမည် | မှန်သလား | အကြောင်းရင်း |
|---|---|---|
| total_price | မှန် | Letter နဲ့ underscore တွေပဲ ပါတယ် |
| price2 | မှန် | ပထမ character မဟုတ်ရင် digit ပါလို့ရတယ် |
| 2price | မမှန် | Digit နဲ့ စလို့ မရ |
| total-price | မမှန် | - က minus operator ဖြစ်တယ် |
| return | မမှန် | C က သိမ်းထားတဲ့ keyword |
| Total နဲ့ total | နှစ်ခုလုံး မှန် | C က case-sensitive ဖြစ်လို့ နာမည် မတူဘူး |
_Count ဒါမှမဟုတ် __temp လို underscore နောက်မှာ စာလုံးကြီး လိုက်တဲ့ နာမည်တွေ၊ underscore နှစ်ခုနဲ့ စတဲ့ နာမည်တွေကို ရှောင်ပါ။ Standard က အဲဒါတွေကို compiler နဲ့ library အတွက် သိမ်းထားပါတယ်။ C23 ကလည်း bool, true, false လို နာမည်အချို့ကို keyword အဖြစ် ပြောင်းလိုက်လို့ အဲဒါတွေကိုလည်း ကိုယ်ပိုင် identifier အဖြစ် မသုံးပါနဲ့။
လက်တွေ့ project တွေမှာ
Real project တွေမှာ ဘယ်လို သုံးလဲ
Compiler က layout ကို လျစ်လျူရှုလို့ team တွေက layout တစ်မျိုးကို သဘောတူပြီး tool တွေကို ခိုင်းကြပါတယ်။ clang-format က C codebase တစ်ခုလုံးကို ဘုံ style တစ်ခုနဲ့ စက္ကန့်ပိုင်းအတွင်း ပြန်စီပေးနိုင်ပြီး Linux kernel လို project တွေက brace နေရာချပုံ၊ indentation နဲ့ နာမည်ပေးပုံကို ဖော်ပြတဲ့ coding style document ကို ထုတ်ပြန်ထားပါတယ်။ Code review မှာ layout တစ်သမတ်တည်း ဖြစ်တာ အရေးကြီးပါတယ် — block တိုင်းကို တစ်ပုံစံတည်း indent လုပ်ထားရင် နေရာလွဲနေတဲ့ brace က ချက်ချင်း ပေါ်လွင်ပါတယ်။
Project အစစ်တွေမှာ ဒီနေ့ တွေ့ခဲ့တဲ့ အစိတ်အပိုင်းတွေကို file အများကြီးပေါ် ခွဲထားကြပါတယ်။ Declaration တွေ (function prototype, type definition) ကို တခြား file တွေက #include လုပ်တဲ့ .h header file တွေထဲ ထည့်ပြီး definition တွေကတော့ .c file တွေထဲမှာ နေပါတယ်။ "မသုံးခင် declare လုပ်ထားရမယ်" ဆိုတဲ့ စည်းမျဉ်းက header တွေ ရှိရတဲ့ အကြောင်းရင်း အတိအကျပါ။ Project ကြီးတစ်ခုမှာ build ကျသွားရင် developer တွေက ဒီ lesson ရဲ့ အလေ့အထကို သုံးကြပါတယ် — ပထမဆုံး error ဆီ scroll ဆွဲ၊ ပြင်၊ ပြန် build။
အမှားများ
အဖြစ်များတဲ့ အမှားတွေ
1. မလိုတဲ့နေရာမှာ semicolon ထည့်တာ
#include <stdio.h>; လို့ ရေးရင် "extra tokens at end of '#include' directive" warning ရပါတယ်။ ပိုဆိုးတာက if (ready); လို condition နောက်မှာ တန်းထည့်တဲ့ semicolon ပါ — semicolon က statement အလွတ်ဖြစ်သွားလို့ နောက်က block က အမြဲ run ပါတယ် (-Wextra က "suggest braces around empty body in an 'if' statement" လို့ သတိပေးပါတယ်)။ if ကို နောက် lesson မှာ တွေ့ရမှာပါ၊ စည်းမျဉ်းကိုတော့ အခုကတည်းက မှတ်ထားပါ — directive, function header နဲ့ condition တွေနောက်မှာ semicolon မထည့်ပါ။
2. စာသားကို single quote နဲ့ ဝိုင်းတာ
printf('Hello'); က Hello ကို print မထုတ်ပါဘူး။ 'Hello' က multi-character constant ဖြစ်တဲ့ int တစ်ခုဖြစ်လို့ GCC က "passing argument 1 of 'printf' makes pointer from integer without a cast" နဲ့ ရပ်သွားပါတယ်။ Text ကို double quote ထဲမှာ အမြဲထည့်ပါ၊ single quote ထဲမှာ character တစ်လုံးပဲ ရှိရပါမယ်။
3. စာလုံးအကြီးအသေး မှားတာ
Printf("Hi\n"); က မသိတဲ့ function ဖြစ်လို့ GCC က "implicit declaration of function 'Printf'; did you mean 'printf'?" နဲ့ compile မလုပ်ပေးပါဘူး။ Main အတွက်လည်း ဒီအတိုင်းပါ — function တစ်ခုတည်းကို Main လို့ နာမည်ပေးထားတဲ့ program မှာ main လုံးဝ မရှိလို့ linker က ကျသွားပါတယ် (Windows ပေါ်က MinGW GCC က undefined reference to `WinMain' လို့ report လုပ်ပါတယ်)။
4. Brace မညီတာ
ပိတ်တဲ့ brace တစ်ခု မေ့သွားရင် GCC က "expected declaration or statement at end of input" ကို file ရဲ့ နောက်ဆုံး line မှာ report လုပ်ပါတယ်၊ တကယ် ကွက်လပ်ဖြစ်နေတဲ့ နေရာနဲ့ အဝေးကြီးပါ။ Block တိုင်းကို တစ်သမတ်တည်း indent လုပ်ပြီး brace ဖွင့်တာနဲ့ ပိတ်တဲ့ brace ကို ချက်ချင်း ရိုက်ထားပါ။
Mini challenge
Mini challenge: နာမည်ကတ်
ဘောင်ခတ်ထားတဲ့ နာမည်ကတ်တစ်ခုကို print ထုတ်ပြီး ဒီ lesson ရဲ့ စည်းမျဉ်းလေးခုကို သုံးပြပါ — string literal, character constant, line နှစ်ကြောင်းပေါ်က statement နဲ့ nested block။
- အပေါ်ဘောင်၊ နာမည် line၊ grade line နဲ့ အောက်ဘောင်ကို print ထုတ်ရမယ်
- နာမည်အတွက် string literal နဲ့ %s၊ grade အတွက် character constant နဲ့ %c ကို သုံးရမယ်
- printf ခေါ်တာ အနည်းဆုံး တစ်ခုကို line နှစ်ကြောင်း ခွဲရေးရမယ်
- အလယ်က line နှစ်ကြောင်းကို nested block ထဲမှာ ထားရမယ်
#include <stdio.h>
int main(void) {
// TODO: print the top border, e.g. +------------------+
// TODO: open a nested block { } and inside it:
// - print "| Name: " and a name, using %s and a string literal
// - print "| Grade: " and a grade, using %c and a character constant
// (split this printf call over two lines)
// TODO: print the bottom border
return 0;
}ရလဒ်: ဒီလို ဘောင်ကွက် တစ်ခု ထွက်လာရပါမယ် (နာမည်နဲ့ grade ကိုတော့ ပြောင်းလို့ရပါတယ်): +------------------+ | Name: Aye Aye | | Grade: A | +------------------+
အရိပ်အမြွက် ကြည့်ရန်
Border ကို စာသားတစ်ခုလုံးအဖြစ် print ထုတ်ပြီး နာမည်နဲ့ grade line တွေမှာ ညာဘက် | ကို ဘောင်နဲ့ တန်းညီအောင် space တွေ ထည့်ပါ။ Comma နောက်မှာ line ခွဲရင် statement မပြတ်ပါဘူး — semicolon ရောက်မှပဲ ဆုံးပါတယ်။
အဖြေ ကြည့်ရန်
#include <stdio.h>
int main(void) {
printf("+------------------+\n");
{
printf("| Name: %s |\n", "Aye Aye");
printf("| Grade: %c |\n",
'A');
}
printf("+------------------+\n");
return 0;
}Quick Quiz 1 / 3
Quick Quiz 2 / 3
Quick Quiz 3 / 3
မှတ်ထားရမယ့် အချက်များ
- C file တစ်ခုကို directive, declaration, function definition, statement နဲ့ block တွေနဲ့ တည်ဆောက်ထားပါတယ်။
- Statement နဲ့ declaration တွေက ; နဲ့ ဆုံးပြီး directive တွေက line အဆုံးမှာ၊ block တွေက } မှာ ဆုံးပါတယ်။
- ; ဒါမှမဟုတ် } ကျန်နေတာကို compiler က နောက် token ရောက်မှ သိလို့ ပထမဆုံး error ကို အရင်ပြင်ပြီး ပြန် compile လုပ်ပါ။
- C က case-sensitive ဖြစ်ပြီး identifier တွေက digit နဲ့ မစရ၊ keyword မဖြစ်ရ — 'A' က character၊ "A" က string ပါ။
ISO C Working Group (WG14) — ISO/IEC JTC1/SC22/WG14