ရိုးရိုးလေး ရှင်းပြမယ်
ရိုးရိုးလေး ရှင်းရရင်
Variable ဆိုတာ data သိမ်းတဲ့ တံဆိပ်ကပ်ထားတဲ့ ဘူးလေး တစ်ခုလိုပါ။ C မှာ ဘူးကို မသုံးခင် ဘူးရဲ့ ပုံသဏ္ဌာန် — သူ့ data type — ကို ကြေညာရပါတယ်၊ ဘာကြောင့်လဲဆိုတော့ memory ဘယ်လောက် ဖယ်ထားရမလဲ၊ ထဲက bit တွေကို ဘယ်လို ဖတ်ရမလဲဆိုတာ compiler သိဖို့ လိုလို့ပါ။ int ဘူးက ကိန်းပြည့်တွေ၊ double ဘူးက ဒသမကိန်းတွေ၊ char ဘူးက character တစ်လုံးကို သိမ်းပါတယ်။
ဘူးထဲက အရာကို ကြိုက်သလောက် အကြိမ်ကြိမ် လဲလို့ရပေမယ့် ဘူးရဲ့ ပုံသဏ္ဌာန်ကိုတော့ ဘယ်တော့မှ ပြောင်းလို့ မရပါဘူး။ နောက်ထပ် အရေးကြီးတဲ့ အချက်တစ်ခုက C က ဘူးအသစ်ကို ကိုယ့်အတွက် အလိုလို မရှင်းပေးတာပါ — ပထမဆုံး value ကို ကိုယ်တိုင် မထည့်မချင်း အရင်က ကျန်နေခဲ့တဲ့ အမှိုက်တွေ ရှိနေနိုင်ပါတယ်။
Problem → Why → Solution
ပြဿနာ: ဘယ်တုန်းကမှ value မပေးခဲ့တဲ့ စုစုပေါင်း
ဒီ program က total ကို declare လုပ်ပြီး 5 ပေါင်းကာ ရလဒ်ကို print ထုတ်ပါတယ်။ Total: 5 ထွက်မယ်လို့ ထင်ရပေမယ့် စမ်း run တစ်ခုမှာ Total: 32764 ထွက်ခဲ့ပြီး တခြား compiler, setting ဒါမှမဟုတ် စက်တွေမှာ တခြား number ထွက်နိုင်ပါတယ်။ Flag မပါဘဲ compile လုပ်ရင် GCC က warning တစ်ခုမှ မပေးခဲ့ပါဘူး၊ -Wall ကမှ "'total' is used uninitialized" လို့ သတိပေးပါတယ်။
တကယ့် program ထဲမှာ ဒါမျိုးက စုစုပေါင်းဈေးနှုန်း၊ counter ဒါမှမဟုတ် sensor ပျမ်းမျှတန်ဖိုး တစ်ခုခုဖြစ်နိုင်ပြီး developer ရဲ့ စက်ပေါ်မှာ မှန်သလို ဖြစ်နေပြီး customer ရဲ့ စက်ပေါ်မှာ မှားသွားနိုင်ပါတယ်။
#include <stdio.h>
int main(void) {
int total; /* declared, but never given a value */
total = total + 5;
printf("Total: %d\n", total);
return 0;
}Variable အသစ်က ဘာကြောင့် 0 မဟုတ်တာလဲ
Function ထဲမှာ declare လုပ်တဲ့ variable တစ်ခုအတွက် memory ကို function run တိုင်း ယူပေးပါတယ်။ အဲဒီ memory ကို သုည ဖြည့်ရင် အချိန်ကုန်တာမို့ C က မဖြည့်ပါဘူး — အရင်က သုံးခဲ့တဲ့ bit တွေ ကျန်နေတာပါ။ Standard က ဒီ value ကို indeterminate လို့ ခေါ်ပြီး total + 5 မှာ ဖတ်လိုက်တာက undefined behavior ပါ။ ဖြေရှင်းနည်းက ဘာမှ မကုန်ပါဘူး — int total = 0; လို့ ရေးပြီး variable တိုင်းကို ပထမဆုံး value နဲ့ မွေးဖွားစေပါ။
Core concept: type, value နဲ့ scope ပါတဲ့ နာမည်ပေးထားသော memory
Variable ဆိုတာ type ပုံသေရှိတဲ့ နာမည်ပေးထားသော memory အပိုင်း တစ်ခုပါ။ int age = 20; ဆိုတဲ့ declaration က အလုပ်သုံးခုကို တစ်ပြိုင်နက် လုပ်ပါတယ် — compiler ကို age ဆိုတဲ့ နာမည်ကို ပြောတယ်၊ type ကို int လို့ သတ်မှတ်တယ် (ဒါက byte ဘယ်နှခု ဖယ်ထားမလဲ၊ bit တွေကို ဘယ်လို ဖတ်မလဲကို ဆုံးဖြတ်ပါတယ်)၊ ပြီးတော့ memory ကို 20 နဲ့ initialize လုပ်တယ်။ နောက်ပိုင်းမှာ type ကို ဘယ်တော့မှ ပြောင်းလို့ မရပါဘူး၊ age ရှိနေသရွေ့ int ပဲ ကိုင်ထားပါမယ်။
Initialization နဲ့ assignment က တူသလိုထင်ရပေမယ့် အချိန်မတူပါဘူး။ Initialization က declaration ရဲ့ အစိတ်အပိုင်းအနေနဲ့ ပထမဆုံး value ကို ပေးတာပါ။ age = 21; လို assignment ကတော့ value ကို နောက်မှ အစားထိုးတာဖြစ်ပြီး variable ကို အရင် declare လုပ်ထားပြီး ဖြစ်ရပါမယ်။ = သင်္ကေတက "ညီတယ်" လို့ မဆိုလိုဘဲ "ညာဘက်က value ကို ဘယ်ဘက်က variable ထဲ သိမ်းပါ" လို့ ဆိုလိုပါတယ်။ ဒါကြောင့် age = age + 1; က အဓိပ္ပာယ်ရှိပါတယ် — age ကို ဖတ်၊ တစ် ပေါင်း၊ ရလဒ်ကို ပြန်သိမ်း။ နှိုင်းယှဉ်တာကတော့ == ကို သုံးပြီး Operators lesson မှာ တွေ့ရပါမယ်။
| Type | ဘာသိမ်းလဲ | printf |
|---|---|---|
| int | -3, 0, 42 လို ကိန်းပြည့် | %d |
| double | ဒသမပါတဲ့ number၊ significant digit ၁၅ လုံးလောက် | %f, %.2f |
| float | ပိုသေးပြီး တိကျမှု နည်းတဲ့ ဒသမ (digit ၇ လုံးလောက်)၊ 19.99f လို ရေး | %f |
| char | 'A' လို character တစ်လုံး (integer code သေးသေးလေး) | %c |
Function ထဲမှာ initializer မပါဘဲ declare လုပ်ထားတဲ့ variable က သုည နဲ့ မစပါဘူး၊ problem section မှာ မြင်ခဲ့သလို indeterminate ဖြစ်ပါတယ်။ တခြား language တွေက default value ဒါမှမဟုတ် error နဲ့ ဒါကို ဖုံးထားပေမယ့် C ကတော့ နှစ်ခုလုံး မလုပ်ပါဘူး။
နောက်ဆုံးအနေနဲ့ variable တစ်ခုက သူ့ declaration ကနေ သူ့ကို ပါဝင်တဲ့ block ရဲ့ ပိတ်တဲ့ brace အထိပဲ ရှိပါတယ်။ အဲဒီ region ကို scope လို့ ခေါ်ပါတယ်။ အပြင်ဘက်မှာ နာမည်ကို မသိတော့ပဲ memory ကိုလည်း ပြန်သုံးနိုင်ပါတယ်။ Variable တွေကို သူတို့လိုတဲ့ အသေးဆုံး block ထဲမှာ declare လုပ်တာက သုံးတဲ့နေရာနဲ့ နီးနီးကပ်ကပ် ထားပေးပြီး တခြား code က မတော်တဆ မပြောင်းမိအောင် ကာကွယ်ပေးပါတယ်။
Three variables after the example runs
--------------------------------------
name type value
+-------+---------+--------------+
| age | int | 20 -> 21 | assignment replaced the value
| price | double | 19.99 |
| grade | char | 'A' -> 'B' | the type never changes
+-------+---------+--------------+- Declare: နာမည်နဲ့ type ရွေး
- Initialize: ပထမ value ထည့်
- Assign: = နဲ့ value ကို အစားထိုး
- Block ရဲ့ } မှာ scope ဆုံး
ဘူးရဲ့ ပုံသဏ္ဌာန် (type) က ထာဝရ ပုံသေဖြစ်ပြီး အထဲက အရာပဲ ပြောင်းပါတယ်။ C မှာ ဗလာလို့ ထင်ရတဲ့ ဘူးက ဘယ်တော့မှ ဗလာ မဟုတ်ပါဘူး — value မထည့်မချင်း ကျန်နေတဲ့ bit တွေ ရှိနေပါတယ်။
အထွေထွေ ပုံစံ
type name; /* declaration only: value not set yet */
type name = value; /* declaration with initialization */
name = new_value; /* assignment: change it later */
type first = 1, second = 2; /* several variables of the same type */
{
type inner = value; /* visible only inside this block */
}ပထမ line လို initializer မပါတဲ့ ပုံစံကို ရှောင်ပါ — ပထမဆုံး value ကို မသိသေးရင်တောင် 0 ပေးထားပါ။ Assignment မှာ type ကို ပြန်မရေးရပါဘူး၊ ရေးရင် variable အသစ်တစ်ခု declare လုပ်သလို ဖြစ်သွားပါတယ်။
Example: variable သုံးခုနဲ့ assignment နှစ်ခု
ဒီ program က type မတူတဲ့ variable သုံးခုကို declare လုပ်ပြီး ချက်ချင်း initialize လုပ်ကာ type နဲ့ ကိုက်တဲ့ specifier တွေနဲ့ print ထုတ်ပါတယ်။ ပြီးရင် assignment နှစ်ခုက value တွေကို ပြောင်းလိုက်ပါတယ် — variable အသစ် မဖန်တီးဘဲ ဘူးတူတူထဲက အရာကိုပဲ အစားထိုးတာ သတိထားကြည့်ပါ။
#include <stdio.h>
int main(void) {
int age = 20;
double price = 19.99;
char grade = 'A';
printf("Age: %d\n", age);
printf("Price: %.2f\n", price);
printf("Grade: %c\n", grade);
age = age + 1; /* assignment: same variable, new value */
grade = 'B';
printf("Next year: %d, new grade: %c\n", age, grade);
return 0;
}Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်
- Line 4
int age = 20;Declaration နဲ့ initialization ကို တစ်ပြိုင်နက် လုပ်တာပါ — int အတွက် memory ဖယ်ပြီး 20 ကို ချက်ချင်း ထည့်ပါတယ်။
- Line 5
double price = 19.99;19.99 လို literal က double ဖြစ်လို့ double variable ထဲ ပြောင်းလဲမှု မလိုဘဲ ဝင်ပါတယ်။ float ကို သုံးချင်ရင် float price = 19.99f; လို့ f suffix နဲ့ ရေးပါ။
- Line 6
char grade = 'A';char က character တစ်လုံးကို ကိုင်ထားပြီး single quote ထဲမှာ ရေးရပါတယ်။ Memory ထဲမှာတော့ သူ့ integer code ကို သိမ်းထားတာပါ။
- Line 9
printf("Price: %.2f\n", price);%.2f က double ကို ဒသမနောက် ဂဏန်းနှစ်လုံးနဲ့ ပြလို့ ဈေးနှုန်းတွေအတွက် အသုံးဝင်ပါတယ်။ Specifier က variable ရဲ့ type နဲ့ ကိုက်ရပါမယ်။
- Line 12
age = age + 1; /* assignment: same variable, new value */ညာဘက်ကို အရင် တွက်ပါတယ် — age ရဲ့ လက်ရှိ value 20 ကို ဖတ်ပြီး 1 ပေါင်းကာ 21 ကို age ထဲ ပြန်သိမ်းပါတယ်။ ရှေ့မှာ int မပါလို့ ဒါက variable အသစ် မဟုတ်ပါဘူး။
- Line 14
printf("Next year: %d, new grade: %c\n", age, grade);Specifier နှစ်ခု၊ argument နှစ်ခုကို အစဉ်လိုက် တွဲပါတယ်။ Variable တွေက update ဖြစ်ပြီးသား value တွေ 21 နဲ့ B ကို ပြပါတယ်။
Age: 20
Price: 19.99
Grade: A
Next year: 21, new grade: Bfloat နဲ့ double
C ရဲ့ default floating type က double ပါ — 19.99 လို literal တစ်ခုက double ဖြစ်ပါတယ်။ float က system အများစုမှာ memory တစ်ဝက်ပဲ သုံးပေမယ့် significant digit ၇ လုံးလောက်ပဲ ထိန်းထားနိုင်ပါတယ်။ ဒသမ နှစ်နေရာမှာတော့ နှစ်ခုလုံး 19.99 ပြပေမယ့် ဒသမ ဆယ်နေရာ တောင်းလိုက်ရင် float ရဲ့ rounding error ပေါ်လာပါတယ်။ Memory အရမ်းကျပ်မှ မဟုတ်ရင် double ကို ရွေးပြီး float သုံးတဲ့အခါ literal ကိုက်အောင် 19.99f လို f suffix ထည့်ပါ။ float price = 19.99; လို့ ရေးရင်လည်း အလုပ်လုပ်ပါတယ် (double literal ကို float အဖြစ် ပြောင်းပေးလို့ပါ)၊ ဒါပေမယ့် f suffix က ကိုယ့်ရည်ရွယ်ချက်ကို ပြောပြပါတယ်။
#include <stdio.h>
int main(void) {
float price_f = 19.99f;
double price_d = 19.99;
printf("%.2f %.2f\n", price_f, price_d); /* 19.99 19.99 */
printf("%.10f\n", price_f); /* 19.9899997711 */
printf("%.10f\n", price_d); /* 19.9900000000 */
return 0;
}Scope ကို လက်တွေ့ ကြည့်ခြင်း
အောက်က bonus က အတွင်းဘက် brace တွေထဲမှာပဲ ရှိပါတယ်။ count ကတော့ main ရဲ့ block မှာ declare လုပ်ထားလို့ အတွင်းဘက် block ကနေ ဖတ်တာ၊ ပြောင်းတာ လုပ်နိုင်ပြီး block ပြီးသွားတဲ့နောက်မှာလည်း ရှိနေဆဲပါ။ ပိတ်တဲ့ brace နောက်မှာ bonus ကို သုံးဖို့ ကြိုးစားရင် GCC က "'bonus' undeclared (first use in this function)" error ပေးပါတယ်။ Function အားလုံးရဲ့ အပြင်ဘက်မှာ declare လုပ်ထားတဲ့ variable တွေကတော့ file scope ရှိပြီး program run နေသရွေ့ ရှိနေပါတယ် — အဲဒီ အဆင်ပြေမှုရဲ့ ပေးဆပ်ရတဲ့ အဖိုးကို real-world section မှာ ကြည့်ပါမယ်။
#include <stdio.h>
int main(void) {
int count = 1;
{
int bonus = 10; /* exists only inside these braces */
count = count + bonus;
printf("inside: count=%d bonus=%d\n", count, bonus);
}
/* bonus is gone here; using it would be a compile error */
printf("outside: count=%d\n", count);
return 0;
}လက်တွေ့ project တွေမှာ
Real project တွေမှာ ဘယ်လို သုံးလဲ
Embedded firmware က sensor reading တစ်ခုကို တစ်စက္ကန့်ကို အကြိမ်များစွာ variable ထဲ ဖတ်ထည့်ပြီး fan ဖွင့်မလား ဆုံးဖြတ်ပါတယ်။ အဲဒီမှာ type ရွေးတာက တကယ့် design ဆုံးဖြတ်ချက်ပါ — millisecond တွေ ရေတွက်တဲ့ int counter တစ်ခုက တစ်ချိန်ချိန်မှာ overflow ဖြစ်မှာဖြစ်ပြီး float က ငွေကြေးအတွက် တိကျမှု မလုံလောက်နိုင်ပါဘူး။ C codebase တွေမှာ နာမည်တွေကို retry_count, max_users လို snake_case နဲ့ ပေးလေ့ရှိပြီး style guide အတော်များများက variable တစ်ခုချင်းမှာ ကိုယ်ပိုင် comment ရှိနိုင်အောင် line တစ်ကြောင်းမှာ declaration တစ်ခုပဲ ထားဖို့ တောင်းဆိုပါတယ်။
Team တွေက variable တွေကို local အဖြစ်လည်း ထားကြပါတယ်။ Function အားလုံးရဲ့ အပြင်ဘက်မှာ declare လုပ်ထားတဲ့ global variable တစ်ခုကို program ထဲက ဘယ် function ကမဆို ပြောင်းနိုင်လို့ မမြင်ရတဲ့ မှီခိုမှုတွေ ဖြစ်လာပါတယ် — function တစ်ခုရဲ့ ရလဒ်က တခြား code က အရင်က ဘာလုပ်ခဲ့လဲ ပေါ်မူတည်လာတယ်၊ test တွေ အချင်းချင်း နှောင့်ယှက်တယ်၊ thread တွေနဲ့ဆိုရင် program ရဲ့ အပိုင်းနှစ်ခုက တစ်ပြိုင်နက် ရေးမိနိုင်ပါတယ်။ -Wall -Wextra လို compiler flag တွေ၊ Valgrind နဲ့ Clang ရဲ့ MemorySanitizer လို tool တွေက initialize မလုပ်ထားတဲ့ variable တွေကို ဖတ်တာကို product မထွက်ခင် ဖမ်းပေးနိုင်ပါတယ်။
အမှားများ
အဖြစ်များတဲ့ အမှားတွေ
1. Value မပေးခင် variable ကို ဖတ်တာ
Problem section က total လိုပါပဲ — value က indeterminate ဖြစ်ပြီး -Wall ကလည်း အမြဲ မဖမ်းနိုင်ပါဘူး။ Loop ထဲမှာ update လုပ်တဲ့ uninitialized sum တစ်ခုကို စမ်းကြည့်တဲ့အခါ -O2 မပါရင် GCC က warning မပေးခဲ့ပါဘူး။ Declare လုပ်တဲ့နေရာမှာ ချက်ချင်း initialize လုပ်ပါ။
2. Block တစ်ခုထဲမှာ နာမည်တူကို နှစ်ခါ declare လုပ်တာ
int score = 10; နောက်မှာ int score = 20; လို့ ရေးရင် GCC က "redefinition of 'score'" error ပေးပြီး "previous definition of 'score' with type 'int'" ဆိုတဲ့ note နဲ့ ပထမ declaration ကို ညွှန်ပြပါတယ်။ Value ပြောင်းချင်ရုံဆိုရင် type မပါဘဲ score = 20; လို့ assignment ရေးပါ။
3. int ထဲမှာ ဒသမကိန်း သိမ်းတာ
int price = 19.99; က compile ဖြစ်ပြီး 19 ကို သိမ်းပါတယ် — ဒသမ အပိုင်းကို round မလုပ်ဘဲ အသံတိတ် ဖြတ်ပစ်ပါတယ်။ -Wall -Wextra က ဘာမှ မပြောပါဘူး၊ -Wconversion ကမှ "conversion from 'double' to 'int' changes value" လို့ သတိပေးပါတယ်။ ဒသမ လိုတဲ့ value တွေအတွက် double ကို သုံးပါ။
4. Variable ကို သူ့ block အပြင်ဘက်မှာ သုံးတာ
Block တစ်ခုထဲမှာ declare လုပ်ထားတဲ့ variable ကို ပိတ်တဲ့ brace နောက်မှာ မရှိတော့ပါဘူး၊ ဒါကြောင့် scope section က bonus ကို အပြင်မှာ ခေါ်ရင် undeclared error ရပါတယ်။ Block ပြီးသွားတဲ့နောက်မှာ value လိုသေးရင် variable ကို block အပြင်ဘက်မှာ declare လုပ်ပြီး အထဲကနေ assign လုပ်ပါ။
Mini challenge
Mini challenge: ပိုက်ဆံအိတ် tracker
ပစ္စည်းအရေအတွက်၊ လက်ကျန်ငွေနဲ့ ငွေကြေး letter ကို variable သုံးခုနဲ့ ခြေရာခံပြီး ဈေးဝယ်မတိုင်ခင်နဲ့ ဝယ်ပြီးနောက် print ထုတ်ပါ။
- ပစ္စည်းအရေအတွက်အတွက် int (0)၊ လက်ကျန်ငွေအတွက် double (5000.00)၊ ငွေကြေး letter အတွက် char ('K') ကို declare လုပ်ပြီး initialize လုပ်ရမယ်
- စတင် value တွေကို ကိုက်ညီတဲ့ specifier တွေနဲ့ print ထုတ်ရမယ်
- တစ်ခု 1250.50 တန် ပစ္စည်း ၂ ခု ဝယ်ပါ — variable တွေကို ပြန် declare မလုပ်ဘဲ assignment နဲ့ update လုပ်ရမယ်
- Value အသစ်တွေကို print ထုတ်ရမယ်၊ လက်ကျန်ငွေကို ဒသမ နှစ်နေရာနဲ့ ပြရမယ်
#include <stdio.h>
int main(void) {
// TODO: declare and initialize three variables:
// an int for the item count (start at 0),
// a double for the balance (start at 5000.00),
// a char for the currency letter ('K' for kyat)
// TODO: print "Start: ..." using %d, %.2f and %c
// TODO: buy 2 items at 1250.50 each by ASSIGNING new values
// (do not declare the variables again)
// TODO: print "After shopping: ..." with the updated values
return 0;
}ရလဒ်: ဒီလို line နှစ်ကြောင်း ထွက်ရပါမယ်: Start: 0 items, balance 5000.00 K After shopping: 2 items, balance 2499.00 K
အရိပ်အမြွက် ကြည့်ရန်
Assignment ရဲ့ ညာဘက်မှာ variable ရဲ့ value အဟောင်းကို သုံးလို့ရပါတယ် — items = items + 2; လိုမျိုးပါ။ printf တစ်ခုထဲမှာ specifier သုံးခု သုံးရင် argument သုံးခုကို အစဉ်အတိုင်း ပေးရပါမယ်။
အဖြေ ကြည့်ရန်
#include <stdio.h>
int main(void) {
int items = 0;
double balance = 5000.00;
char currency = 'K'; /* K for kyat */
printf("Start: %d items, balance %.2f %c\n", items, balance, currency);
items = items + 2;
balance = balance - 2 * 1250.50;
printf("After shopping: %d items, balance %.2f %c\n", items, balance, currency);
return 0;
}Quick Quiz 1 / 3
Quick Quiz 2 / 3
Quick Quiz 3 / 3
မှတ်ထားရမယ့် အချက်များ
- Declaration က variable ကို နာမည်နဲ့ ပုံသေ type ပေးပြီး initialization က statement တစ်ခုတည်းမှာ ပထမ value ကို ပေးပါတယ်။
- = က assign လုပ်တာပါ — ညာဘက် value ကို ဘယ်ဘက် variable ထဲ သိမ်းလို့ age = age + 1; က မှန်ကန်ပါတယ်။
- ကိန်းပြည့်အတွက် int၊ ဒသမအတွက် double၊ character တစ်လုံးအတွက် char ကို သုံးပြီး printf မှာ %d, %f, %c နဲ့ ကိုက်ပါစေ။
- Initialize မလုပ်ထားတဲ့ local variable တွေမှာ garbage ရှိပါတယ် — variable တိုင်းကို declare လုပ်တဲ့နေရာမှာ initialize လုပ်ပါ။
- Variable က declare လုပ်ခဲ့တဲ့ block ထဲမှာပဲ ရှိပါတယ် — အဲဒီ scope ကို ဖြစ်နိုင်သမျှ သေးအောင် ထားပါ။
GNU C compiler diagnostics — GNU Project