ရိုးရိုးလေး ရှင်းပြမယ်
ရိုးရိုးလေး ရှင်းရရင်
C ဆိုတာ hardware နဲ့ အရမ်းနီးနီးကပ်ကပ် အလုပ်လုပ်တဲ့ programming language သေးသေးလေး တစ်ခုပါ။ ကိုယ်ပေးချင်တဲ့ ညွှန်ကြားချက်တွေကို .c file ထဲမှာ ရိုးရိုး text အနေနဲ့ ရေးရပြီး compiler လို့ခေါ်တဲ့ program က အဲဒီ file တစ်ခုလုံးကို machine code — processor က တိုက်ရိုက် run တဲ့ instruction တွေ — အဖြစ် ဘာသာပြန်ပေးပါတယ်။ Program run နေချိန်မှာ ဘာသာပြန်ပေးနေရမယ့်သူ မလိုတော့တာကြောင့် C program တွေက မြန်မြန် စတင်ပြီး memory နည်းနည်းပဲ သုံးကြတာပါ။
အဲဒီ မြန်ဆန်မှုအတွက် ပေးရတဲ့ အဖိုးကတော့ တာဝန်ယူမှုပါ။ ကိုယ်တောင်းထားတဲ့ memory အတွင်းမှာပဲ နေဖို့၊ value တိုင်းကို type မှန်မှန် ပေးဖို့၊ သုံးပြီးသားတွေကို ကိုယ်တိုင် ရှင်းဖို့ C က ကိုယ့်ကို ယုံကြည်ပြီး လွှဲထားပါတယ်။ Manual ဂီယာ ကားတစ်စီးလို့ မြင်ကြည့်ပါ — ထိန်းချုပ်မှု အပြည့်ရပေမယ့် ဂီယာ မှားထိုးရင်လည်း ကားက တားမှာ မဟုတ်ပါဘူး။
Problem → Why → Solution
ပြဿနာ: ကိုယ်မှားနေတဲ့အချိန်မှာတောင် C က ယုံနေတယ်
Beginner တစ်ယောက် ရေးမိနိုင်တဲ့ program တစ်ခုကို ကြည့်ရအောင်။ Array ထဲမှာ score သုံးခုပဲ ရှိပေမယ့် code က ခြောက်ခုမြောက် score ကို တောင်းနေပါတယ်။ Python ဒါမှမဟုတ် Java လို language တွေက ဒီ line မှာ index error နဲ့ ရပ်သွားပါလိမ့်မယ်။ C ကတော့ compile လုပ်ပေးတယ်၊ run ပေးတယ်၊ ပြီးတော့ အဲဒီ memory နေရာမှာ ရှိနေတဲ့ bit တွေကို print ထုတ်ပေးလိုက်ပါတယ်။ GCC နဲ့ စမ်း run ကြည့်တဲ့အခါ -1555296038 ထွက်လာပြီး -Wall -Wextra ဖွင့်ထားတာတောင် warning တစ်ခုမှ မပေးပါဘူး။
-O2 (optimization) ကိုပါ ထပ်ထည့်လိုက်မှ GCC က "array subscript 5 is above array bounds of 'int[3]'" လို့ သတိပေးပါတယ်။ ဒီကွာခြားချက်က ဒီ course ရဲ့ ပထမဆုံး သင်ခန်းစာပါ — C မှာ tool တွေက ကိုယ်တောင်းသလောက်ပဲ ကူညီပေးပါတယ်။
#include <stdio.h>
int main(void) {
int scores[3] = {90, 75, 88};
printf("%d\n", scores[5]); /* index 5 does not exist */
return 0;
}C က ဒါကို ဘာကြောင့် ခွင့်ပြုတာလဲ
C ကို ၁၉၇၀ ပြည့်လွန်နှစ် အစောပိုင်းမှာ Unix operating system ရေးဖို့ ဖန်တီးခဲ့တာပါ၊ အဲဒီတုန်းက instruction တစ်ခုချင်းစီက အရေးကြီးပါတယ်။ Array ကို access လုပ်တိုင်း runtime မှာ စစ်နေရင် အချိန်ကုန်တာမို့ language က အဲဒီတာဝန်ကို programmer ဆီ လွှဲထားပါတယ်။ C standard က ဒီလို code ကို undefined behavior လို့ ခေါ်ပါတယ် — program က ပျက်နေပေမယ့် ဘယ်သူကမှ ကိုယ့်ကို ပြောပြဖို့ တာဝန်မရှိပါဘူး။ ဒါကြောင့် ဒီ course ရဲ့ lesson တိုင်းမှာ warning တွေ ဖွင့်ပြီး compile လုပ်ပြီး output မှန်တာ တစ်ခါကို သက်သေအဖြစ် မဟုတ်ဘဲ အရိပ်အမြွက်လို့ပဲ သဘောထားပါတယ်။
Core concept: text file ကနေ run နေတဲ့ program အထိ
C က compiled, statically typed language တစ်ခုပါ။ Compiled ဆိုတာ ဘာသာပြန်တဲ့ အလုပ်ကို program မ run ခင် တစ်ကြိမ်တည်း လုပ်တာကို ဆိုလိုပါတယ်။ Statically typed ဆိုတာကတော့ variable တိုင်းမှာ compiler သိတဲ့ type တစ်ခု ရှိတာပါ — ဥပမာ ကိန်းပြည့်အတွက် int၊ ဒသမကိန်းအတွက် double။ Type တွေကို compiler က သိနေတော့ ကိုယ့် code ကို ကျစ်လျစ်တဲ့ machine instruction တွေအဖြစ် ပြောင်းပေးနိုင်ပြီး ဘာမှ မ run ရသေးခင်ကတည်းက အမှားအတော်များများကို ဖမ်းပေးနိုင်ပါတယ်။ C က procedural language လည်း ဖြစ်ပါတယ် — program တစ်ခုဆိုတာ function တွေ စုထားတာဖြစ်ပြီး run တာက main လို့ နာမည်ပေးထားတဲ့ function တစ်ခုကနေ အမြဲ စပါတယ်။
1. Preprocess
# နဲ့ စတဲ့ line တွေကို အရင်ဆုံး ကိုင်တွယ်ပါတယ်။ #include <stdio.h> က printf လို standard input/output function တွေရဲ့ declaration တွေကို ကိုယ့် file ထဲ ကူးထည့်ပေးပါတယ်။
2. Compile
Compiler က syntax နဲ့ type တွေကို စစ်ပြီး .c file တစ်ခုချင်းစီကို machine code ပါတဲ့ object file အဖြစ် ဘာသာပြန်ပါတယ်။ Syntax error တွေနဲ့ warning အများစုက ဒီအဆင့်ကနေ လာတာပါ။
3. Link
Linker က ကိုယ့် object file တွေကို printf ရဲ့ တကယ့် code ရှိတဲ့ C standard library နဲ့ ချိတ်ဆက်ပေးပါတယ်။ Function တစ်ခု ပျောက်နေရင် ဒီအဆင့်မှာ "undefined reference" error အဖြစ် ပေါ်လာပါတယ်။
4. Run
Operating system က executable ကို memory ထဲ load လုပ်ပြီး main() ကို ခေါ်ပါတယ်။ main က 0 ကို return ပြန်ရင် program အောင်မြင်စွာ ပြီးဆုံးကြောင်း system ကို ပြောတာပါ။
The C build pipeline
--------------------
hello.c --> [preprocessor] --> [compiler] --> hello.o
|
C standard library (printf, ...) --> [linker] <---+
|
v
hello (hello.exe) --> run main()C က product တစ်ခုတည်း မဟုတ်ဘဲ standard တစ်ခုလည်း ဖြစ်ပါတယ်။ ISO က စည်းမျဉ်းတွေကို ထုတ်ပြန်ပြီး GCC, Clang နဲ့ Microsoft ရဲ့ MSVC လို compiler တွေက အဲဒီ စည်းမျဉ်းတွေကို အကောင်အထည်ဖော်ပါတယ်။ အဓိက revision တွေကို နှစ်နဲ့ ခေါ်ကြပါတယ်: C89/C90, C99, C11, C17 နဲ့ C23။ ဒီ course က major compiler တွေရဲ့ လက်ရှိ version တွေ လက်ခံတဲ့ portable C17 ကို သင်ပြီး ကိုယ်ရိုက်မယ့် code ကို C23 က ပြောင်းလဲသွားစေတဲ့ နေရာအနည်းငယ်ကိုလည်း ထောက်ပြပေးပါမယ်။
ဒီအဆင့်တွေကို နောက်ပိုင်းမှာ ထပ်ခါထပ်ခါ တွေ့ရပါလိမ့်မယ်။ ဘယ်အဆင့်က complain လုပ်လဲ ပေါ်မူတည်ပြီး error message ပုံစံ ကွဲပြားလို့ အဆင့်ကို သိရင် ဘယ်နေရာကို ကြည့်ရမလဲဆိုတာ သိပါတယ်။
- hello.c ထဲမှာ source text ရေး
- Preprocessor က header တွေ ကူးထည့်
- Compiler က machine code ထုတ်
- Linker က library ချိတ်၊ OS က run
တစ်ခုခု ပျက်ရင် ဘယ်မှတ်တိုင်က complain လုပ်လဲ အရင်မေးပါ — compiler error တွေက line တစ်ကြောင်းကို ညွှန်ပြီး linker error တွေက ပျောက်နေတဲ့ function နာမည်ကို ပြောပါတယ်။
အထွေထွေ ပုံစံ: အသေးဆုံး C program
#include <header.h> /* bring in declarations you need */
int main(void) { /* the program starts here */
statement; /* each statement ends with ; */
statement;
return 0; /* 0 tells the OS: success */
}ဒီ course ထဲက program တိုင်းက ဒီပုံစံကနေ ကြီးထွားလာတာပါ — #include line အနည်းငယ်၊ ပြီးရင် main၊ main ရဲ့ body ထဲမှာတော့ အပေါ်ကနေ အောက်ကို run မယ့် statement တွေ။ return 0 က success status ကို operating system ဆီ ပြန်ပေးပါတယ်။
Example: ကိုယ့်ရဲ့ ပထမဆုံး program
ဒီ program က နှုတ်ဆက်စာတစ်ကြောင်းနဲ့ course ရဲ့ အရွယ်အစားကို print ထုတ်ပါတယ်။ Lesson အရေအတွက်ကို စာသားအနေနဲ့ ရိုက်ထည့်ထားတာ မဟုတ်ဘဲ chapter ငါးခုရဲ့ lesson အရေအတွက်တွေကို program က ပေါင်းပေးတာမို့ C က တကယ့်အလုပ် လုပ်နေတာကို မြင်ရပါမယ်။ Compile လုပ်ပြီး run မကြည့်ခင် output ကို အရင် ခန့်မှန်းကြည့်ပါ။
#include <stdio.h>
int main(void) {
printf("Welcome to C!\n");
printf("Chapters: Basic, Intermediate, Advanced, Projects, Exercises\n");
printf("Lessons: %d\n", 15 + 7 + 8 + 2 + 2);
return 0;
}Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်
- Line 1
#include <stdio.h>printf ကို declare လုပ်ထားတဲ့ stdio.h header ကို ကူးထည့်ဖို့ preprocessor ကို ခိုင်းတာပါ။ ဒီ line မပါရင် GCC ရဲ့ မကြာသေးခင် version တွေက printf ခေါ်တာကို compile မလုပ်ပေးပါဘူး။
- Line 3
int main(void) {Operating system က ပထမဆုံး ခေါ်တဲ့ function ဖြစ်တဲ့ main ကို define လုပ်တာပါ။ int က ပြန်ပေးမယ့် status ရဲ့ type ဖြစ်ပြီး void က argument ဘာမှ မယူဘူးလို့ ပြောတာပါ။
- Line 4
printf("Welcome to C!\n");စာသားကို print ထုတ်ပါတယ်။ အဆုံးက \n က newline အတွက် escape sequence ဖြစ်လို့ နောက် output က line အသစ်ကနေ စပါတယ်။ Semicolon က statement ကို အဆုံးသတ်ပေးပါတယ်။
- Line 6
printf("Lessons: %d\n", 15 + 7 + 8 + 2 + 2);%d က int တစ်ခုအတွက် နေရာယူထားတဲ့ placeholder ပါ။ printf က အဲဒါကို နောက်က argument ရဲ့ value — ပေါင်းလဒ် 34 — နဲ့ အစားထိုးပါတယ်။ Placeholder တွေကို Output lesson မှာ အသေးစိတ် လေ့လာရပါမယ်။
- Line 7
return 0;main ကို အဆုံးသတ်ပြီး success ကို report လုပ်ပါတယ်။ 0 မဟုတ်တဲ့ value ဆိုရင် program ကို run ခဲ့တဲ့ shell ဒါမှမဟုတ် script ကို တစ်ခုခု မှားသွားကြောင်း ပြောတာ ဖြစ်ပါတယ်။
Welcome to C!
Chapters: Basic, Intermediate, Advanced, Projects, Exercises
Lessons: 34ဒီ course ကို ဘယ်လို စီစဉ်ထားလဲ
| Chapter | Lessons | သင်ယူရမယ့် အရာ |
|---|---|---|
| Basic | 1-15 | Program ပုံစံ၊ output, comment, variable, data type, constant, operator, boolean, if/else, switch နဲ့ loop တွေ |
| Intermediate | 16-22 | Array, string, function နဲ့ parameter, declaration, recursion နဲ့ math library |
| Advanced | 23-30 | User input, file I/O, memory address, pointer, dynamic memory, structure, union နဲ့ enum |
| Projects | 31-32 | အရင် lesson တွေကို ပေါင်းသုံးတဲ့ mini project နဲ့ course summary |
| Exercises | 33-34 | Fundamentals နဲ့ pointer သုံးတဲ့ function တွေအတွက် လေ့ကျင့်ခန်း set နှစ်ခု |
Lesson တိုင်းက လမ်းကြောင်း တစ်ခုတည်းကို လိုက်ပါတယ် — ရိုးရှင်းတဲ့ ရှင်းလင်းချက်၊ တကယ့်ပြဿနာ၊ core concept၊ run ကြည့်လို့ရတဲ့ example၊ အဖြစ်များတဲ့ အမှားတွေ၊ mini challenge နဲ့ quiz တိုလေး။ Challenge ရဲ့ starter code က တမင် skeleton သက်သက်ပဲ ပေးထားတာပါ — code ကို ကိုယ်တိုင် ရိုက်တာက syntax ကို မှတ်မိစေတဲ့ နည်းလမ်းပါ။
Lesson တစ်ခုချင်းကို ဘယ်လို လေ့လာမလဲ
1. အရင် ခန့်မှန်းပါ
Program ကို မ run ခင် ဘာ print ထုတ်မလဲ ချရေးထားပါ။ ခန့်မှန်းတာ မှားရင် ဘယ်အပိုင်းကို ပြန်ဖတ်ရမလဲ အတိအကျ သိရပါတယ်။
2. Paste မလုပ်ဘဲ ရိုက်ပါ
ကိုယ်တိုင် ရိုက်တာက C အလိုရှိတဲ့ semicolon, brace နဲ့ quote တွေကို အလေ့အထ ဖြစ်စေပါတယ်။
3. တမင် ဖျက်ကြည့်ပါ
Semicolon တစ်ခု ဖျက်ကြည့်ပါ ဒါမှမဟုတ် printf ကို စာလုံးပေါင်း မှားရေးကြည့်ပြီး message ကို ဖတ်ပါ။ ကိုယ်တိုင် တမင်ဖန်တီးခဲ့ဖူးတဲ့ error တွေက နောက်ပိုင်းမှာ အများကြီး ကြောက်စရာ နည်းသွားပါတယ်။
4. Warning တွေကို ဖွင့်ထားပါ
ကိုယ့်စက်ပေါ်မှာ compile လုပ်တဲ့အခါ gcc -std=c17 -Wall -Wextra ကို သုံးပါ။ နောက် lesson မှာ ဘယ်လို လုပ်ရမလဲ ပြပါမယ်။
လက်တွေ့ project တွေမှာ
ဒီနေ့ C ဘယ်မှာ run နေလဲ
C က နေရာတိုင်းမှာ ရှိပေမယ့် အများအားဖြင့် မျက်စိနဲ့ မမြင်ရပါဘူး။ Web server အများစုနဲ့ Android ဖုန်းတိုင်းကို run ပေးနေတဲ့ Linux kernel ကို အဓိကအားဖြင့် C နဲ့ ရေးထားပါတယ်။ ဖုန်းတွေနဲ့ web browser တွေထဲမှာ ပါတဲ့ database ဖြစ်တဲ့ SQLite နဲ့ Python code ကို run ပေးတဲ့ CPython program ကိုလည်း C နဲ့ ရေးထားတာပါ။ Python ကနေ မြန်တဲ့ library တစ်ခုကို ခေါ်လိုက်တဲ့အခါ နောက်ကွယ်မှာ C ရှိနေတာ မကြာခဏပါ။
Device သေးသေးလေးတွေမှာလည်း C က လွှမ်းမိုးထားပါတယ်။ အဝတ်လျှော်စက်၊ ကား engine ထိန်းတဲ့ unit, router နဲ့ ဆေးဘက်ဆိုင်ရာ sensor တွေထဲက microcontroller တွေမှာ memory kilobyte အနည်းငယ်ပဲ ရှိပြီး operating system မရှိတာ မကြာခဏပါ။ C က ကိုယ့် code အပေါ်မှာ ဘာမှ သိပ်မထပ်ပေးလို့ အဲဒီနေရာတွေမှာ အံဝင်ပါတယ်။ နောက်ဆုံးအနေနဲ့ C က language တွေကြားက ဘုံဘာသာစကားလိုလည်း ဖြစ်နေပါတယ် — Rust, Go, Python နဲ့ တခြား language အများကြီးက C ရဲ့ calling convention ကို လိုက်နာတဲ့ function တွေကို ခေါ်နိုင်လို့ C နဲ့ ရေးထားတဲ့ library တွေကို နေရာတိုင်းမှာ ပြန်သုံးကြပါတယ်။
အမှားများ
C စလေ့လာချိန် အဖြစ်များတဲ့ အမှားတွေ
1. Output မှန်တာကို သက်သေလို့ ယူဆတာ
အပေါ်က array example က number တစ်ခု print ထုတ်ပြီး ပုံမှန်အတိုင်း ပြီးသွားပေမယ့် ကိုယ်မပိုင်တဲ့ memory ကို ဖတ်ခဲ့ပါတယ်။ C မှာ undefined behavior ပါတဲ့ program က ဒီနေ့ test အမြန်တစ်ခုကို အောင်နိုင်ပြီး တခြား compiler၊ တခြား optimization level ဒါမှမဟုတ် တခြားစက်ပေါ်မှာ ပျက်သွားနိုင်ပါတယ်။ Output ကိုပဲ မကြည့်ဘဲ code ကို စစ်ပါ။
2. Warning ပိတ်ပြီး compile လုပ်တာ
C အမှားအတော်များများက syntax အရ မှန်နေတာမို့ compiler က warning အနေနဲ့ပဲ ပြောပြီး ဒါတောင် တောင်းမှ ပြောတာပါ။ gcc file.c လို့ ဗလာ compile လုပ်တဲ့ beginner က bug ကို ညွှန်ပြပေးနိုင်မယ့် message တွေကို လွတ်သွားပါတယ်။ Build တိုင်းမှာ -Wall -Wextra ထည့်ပြီး warning တစ်ခုချင်းကို ဖြေရမယ့် မေးခွန်းတစ်ခုလို သဘောထားပါ။
3. Python ဒါမှမဟုတ် JavaScript အလေ့အထတွေ ပါလာမယ်လို့ ထင်တာ
C မှာ built-in string type မရှိ၊ bounds check အလိုအလျောက် မရှိ၊ garbage collector လည်း မရှိပါဘူး။ Text ဆိုတာ character array တစ်ခုဖြစ်ပြီး ကိုယ်တိုင် တောင်းယူထားတဲ့ memory ကို ကိုယ်တိုင်ပဲ ပြန်လွှတ်ပေးရပါတယ်။ တခြား language နဲ့ program ရေးဖူးပြီးသား ဖြစ်နေရင်တောင် Strings နဲ့ Dynamic Memory lesson တွေကို ဖြည်းဖြည်း ဖတ်ပါ။
Best Practices
အကောင်းဆုံး အလေ့အကျင့်များ
- Online editor ထဲမှာတင် မဟုတ်ဘဲ example တွေကို ကိုယ့်စက်ပေါ်မှာ compile လုပ်ကြည့်ပါ၊ ဒါမှ toolchain က လျှို့ဝှက်ဆန်းကြယ်တဲ့ အရာ မဖြစ်တော့မှာပါ။
- တွေ့ခဲ့ရတဲ့ error message တိုင်းနဲ့ ဘာက ပြင်ပေးခဲ့လဲဆိုတာကို notes file သေးသေးလေး တစ်ခုမှာ မှတ်ထားပါ။
- Pointer ဆီ မခုန်ခင် Basic chapter ကို ပြီးအောင် လုပ်ပါ — pointer တွေက type, operator နဲ့ array တွေပေါ်မှာ အခြေခံထားလို့ပါ။
Mini challenge
Mini challenge: ကိုယ့်ရဲ့ study card
ဒီ course အတွက် study card သေးသေးလေး တစ်ခု print ထုတ်တဲ့ program ရေးပါ။ Terminal မှာ သပ်သပ်ရပ်ရပ် မြင်ရပြီး number တစ်ခုကို C ကိုယ်တိုင် တွက်ပေးရပါမယ်။
- === My C study card === လို title line တစ်ကြောင်း print ထုတ်ရမယ်
- Lesson စုစုပေါင်းကို %d နဲ့ print ထုတ်ရမယ်၊ chapter အရွယ်အစား 15, 7, 8, 2, 2 ကနေ တွက်ရမယ်
- တစ်ပတ်ကို lesson ဘယ်နှခု ပြီးအောင် လုပ်မလဲဆိုတာ print ထုတ်ရမယ်
- Line တိုင်းကို \n နဲ့ အဆုံးသတ်ပြီး main ကနေ 0 return ပြန်ရမယ်
#include <stdio.h>
int main(void) {
// TODO: print a title line for your card
// TODO: print "Lessons to finish: " followed by the sum of the
// chapter sizes (15, 7, 8, 2, 2) using %d
// TODO: print your weekly goal, e.g. "Lessons per week: 4"
return 0;
}ရလဒ်: ကိုယ့် program က ဒီလို line လေးကြောင်း ထုတ်ပေးရပါမယ် (တစ်ပတ်စာ ပန်းတိုင်ကတော့ ကိုယ်ကြိုက်သလို ပြောင်းလို့ရပါတယ်): === My C study card === Lessons to finish: 34 Lessons per week: 4 First stop: Get Started
အရိပ်အမြွက် ကြည့်ရန်
printf("Total: %d\n", 1 + 2); က Total: 3 ကို print ထုတ်ပါတယ်။ ပေါင်းတဲ့ expression ကို comma နောက်မှာ ထားပြီး %d ကို quote ထဲမှာပဲ ထားပါ။
အဖြေ ကြည့်ရန်
#include <stdio.h>
int main(void) {
printf("=== My C study card ===\n");
printf("Lessons to finish: %d\n", 15 + 7 + 8 + 2 + 2);
printf("Lessons per week: %d\n", 4);
printf("First stop: Get Started\n");
return 0;
}Quick Quiz 1 / 3
Quick Quiz 2 / 3
Quick Quiz 3 / 3
မှတ်ထားရမယ့် အချက်များ
- C က hardware နဲ့ နီးကပ်စွာ run တဲ့ compiled, statically typed language ဖြစ်လို့ kernel တွေ၊ database တွေနဲ့ device သေးသေးလေးတွေကို စွမ်းအားပေးနေပါတယ်။
- Program တစ်ခုက operating system က main() ကို မခေါ်ခင် preprocess, compile, link အဆင့်တွေကို ဖြတ်သန်းရပါတယ်။
- C က programmer ကို ယုံပါတယ် — array အပြင်ဘက် ဖတ်တာလို အမှားတွေက သေချာပေါက် error မဟုတ်ဘဲ undefined behavior ပါ။
- Warning တွေ ဖွင့်ပြီး compile လုပ်ပါ၊ output မှန်တာ တစ်ခါကို သက်သေ မဟုတ်ဘဲ အရိပ်အမြွက်လို့ပဲ သဘောထားပါ။
- Course မှာ Basic, Intermediate, Advanced, Projects, Exercises ဆိုတဲ့ chapter ငါးခုထဲမှာ lesson ၃၄ ခု ပါပါတယ်။
ISO C Working Group (WG14) — ISO/IEC JTC1/SC22/WG14