ရိုးရိုးလေး ရှင်းပြမယ်
ရိုးရိုးလေး ရှင်းရရင်
C compiler က file တစ်ခုကို အပေါ်ကနေ အောက်ကို တစ်ခေါက်တည်း ဖတ်ပါတယ်။ main() ထဲမှာ add(5, 3) ကို တွေ့တဲ့အချိန် add ရဲ့ code က အောက်မှာ ရှိနေသေးရင် compiler က အဲဒီ function ဘယ်လို ပုံစံလဲ မသိသေးပါဘူး။
Prototype ဆိုတာ စာအုပ်ရဲ့ မာတိကာ (table of contents) လိုပါပဲ။ int add(int a, int b); ဆိုတဲ့ စာကြောင်းတစ်ကြောင်းက "add ဆိုတဲ့ function ရှိတယ်၊ int နှစ်ခု ယူပြီး int တစ်ခု ပြန်ပေးတယ်၊ အသေးစိတ်ကို နောက်မှာ ရေးထားတယ်" လို့ compiler ကို ကြိုပြောထားတာပါ။ ဒါဆိုရင် function တွေကို ကိုယ်ကြိုက်တဲ့ အစီအစဉ်နဲ့ ရေးလို့ ရပါပြီ။
Problem → Why → Solution
ပြဿနာ: main() ကို အပေါ်မှာ ရေးလိုက်တော့ compile မဖြစ်တော့ဘူး
Program ကို ဖတ်ရလွယ်အောင် main() ကို အပေါ်ဆုံးမှာ ထားပြီး helper function တွေကို အောက်မှာ ရေးချင်တာ သဘာဝပါ။ ဒါပေမယ့် prototype မပါဘဲ အောက်က code ကို GCC 16 နဲ့ compile လုပ်ရင် error နှစ်ခု တက်ပါတယ်။ C ဗားရှင်းအဟောင်း စည်းကမ်းအရ compiler က မသိတဲ့ function ကို int ပြန်တယ်လို့ ခန့်မှန်းပြီး ဆက်သွားခဲ့ဖူးလို့ ဒုတိယ error က average ကို double လို့ ရေးထားတာနဲ့ အဲဒီ ခန့်မှန်းချက် မကိုက်ဘူးလို့ ပြောတာပါ။
#include <stdio.h>
int main(void) {
double avg = average(7, 8); /* error: implicit declaration */
printf("%.1f\n", avg);
return 0;
}
double average(int a, int b) { /* error: conflicting types */
return (a + b) / 2.0;
}ဘာကြောင့် compiler က ဒီလောက် တင်းကျပ်ရတာလဲ
Function ခေါ်တဲ့ code ကို ထုတ်ဖို့ compiler က return type နဲ့ parameter type တွေကို သိကို သိရပါမယ်၊ ဒါမှ argument တွေကို မှန်ကန်တဲ့ type အဖြစ် ပြောင်းပြီး return value ကို မှန်တဲ့နေရာက ဖတ်နိုင်မှာပါ။ Compiler အဟောင်းတွေက int လို့ ခန့်မှန်းပြီး warning ပဲ ပေးခဲ့လို့ double ပြန်တဲ့ function တွေက အဓိပ္ပာယ်မရှိတဲ့ value တွေ ထုတ်ခဲ့ပါတယ်။ GCC 14 နဲ့ အသစ်တွေက C99 နဲ့ နောက်ပိုင်း standard တွေမှာ ဒါကို error အဖြစ် သတ်မှတ်ပါတယ်။
Core concept: declaration က ကတိ၊ definition က အကောင်အထည်
Declaration (prototype) တစ်ခုမှာ return type၊ function နာမည်နဲ့ parameter type list ပါပြီး semicolon နဲ့ ဆုံးပါတယ်။ Body မပါပါဘူး။ Definition ကတော့ အဲဒီ header ကိုပဲ { } body နဲ့ ပြန်ရေးပြီး function တကယ် လုပ်မယ့် အလုပ်ကို ဖော်ပြပါတယ်။ Program တစ်ခုလုံးမှာ function တစ်ခုစီအတွက် definition တစ်ခုပဲ ရှိရပေမယ့် declaration ကတော့ အချင်းချင်း ကိုက်ညီနေသရွေ့ ကြိုက်သလောက် ရှိလို့ ရပါတယ်။
Prototype ကို မြင်ပြီးတဲ့နောက် call တိုင်းမှာ compiler က argument အရေအတွက်ကို စစ်ပါတယ်၊ argument တစ်ခုစီကို parameter type အဖြစ် ပြောင်းပါတယ်၊ return value ကို ဘယ် type အဖြစ် ဖတ်ရမလဲ သိပါတယ်။ Prototype မရှိရင် နှိုင်းယှဉ်စရာ အချက်အလက် မရှိတော့လို့ ဒီစစ်ဆေးမှုတွေ တစ်ခုမှ မဖြစ်နိုင်ပါဘူး။ Prototype ထဲက parameter နာမည်တွေကို compiler က လျစ်လျူရှုပေမယ့် int clamp(int value, int low, int high); လို နာမည်ပါတာက ဖတ်တဲ့သူအတွက် documentation ဖြစ်ပါတယ်။
Parameter မရှိတဲ့ function ကို C17 မှာ int menu(void); လို့ ရေးရပါတယ်။ int menu(); လို့ ကွင်းအလွတ်နဲ့ ရေးရင် "parameter တွေ မသိဘူး" လို့ အဓိပ္ပာယ်ရလို့ menu(1, 2, 3) ကို compiler က မစစ်တော့ပါဘူး။ C23 မှာတော့ () က (void) နဲ့ အဓိပ္ပာယ်တူ ဖြစ်သွားပေမယ့် (void) က standard နှစ်ခုလုံးမှာ ရှင်းပါတယ်။
Project ကြီးလာရင် .c file တစ်ခုစီကို သီးခြား compile လုပ်ပြီး linker က call တွေကို definition တွေနဲ့ ဆက်ပေးပါတယ်။ .c file တစ်ခုနဲ့ သူ include လုပ်ထားတာ အားလုံးကို translation unit လို့ ခေါ်ပြီး compiler က တစ်ကြိမ်မှာ translation unit တစ်ခုကိုပဲ မြင်ပါတယ်။ ဒါကြောင့် အပြင်က ခေါ်ခွင့်ပေးမယ့် prototype တွေကို header (.h) file ထဲ ထားပြီး function ကို သုံးတဲ့ file ရော define လုပ်တဲ့ file ပါ include လုပ်ပါတယ်။ ဒါမှ prototype နဲ့ definition မကိုက်ရင် compiler က ဖမ်းပေးနိုင်မှာပါ။ File အပြင်က မခေါ်စေချင်တဲ့ helper တွေကိုတော့ static လို့ မှတ်ပြီး header ထဲ မထည့်ပါဘူး။
- Compiler က အပေါ်ကနေ အောက် တစ်ခေါက် ဖတ်
- Prototype တွေ့ရင် signature ကို မှတ်ထား
- Call တိုင်းကို မှတ်ထားတဲ့ signature နဲ့ စစ်
- Linker က call ကို definition နဲ့ ဆက်ပေး
Prototype မရှိရင် compile အဆင့်မှာ ကျပြီး definition မရှိရင် link အဆင့်မှာ ကျပါတယ်။
အထွေထွေ ပုံစံ
ReturnType name(Type1 param1, Type2 param2); /* declaration */
ReturnType other(void); /* takes no arguments */
int main(void) {
/* calls to name() and other() are checked here */
}
ReturnType name(Type1 param1, Type2 param2) { /* definition */
/* body */
}Prototype ကို semicolon နဲ့ ဆုံးပြီး definition ရဲ့ header နဲ့ type တွေ တစ်ထပ်တည်း ကိုက်ရပါမယ်။ Argument မယူတဲ့ function ကို (void) နဲ့ ကြေညာပါ။ Definition ကတော့ file ထဲ ဘယ်နေရာမှာ ရှိရှိ ရပါတယ်။
Example: main() အပေါ်မှာ prototype၊ အောက်မှာ definition
ဒီ program မှာ add() ကို main() ထဲမှာ ခေါ်ပေမယ့် definition က file အောက်ဆုံးမှာ ရှိပါတယ်။ ထိပ်ဆုံးက prototype တစ်ကြောင်း ရှိနေလို့ compiler က call ကို စစ်ပြီး compile ဖြစ်ပါတယ်။ Prototype line ကို comment ပိတ်ပြီး ပြန် compile လုပ်ကြည့်ရင် implicit declaration error ကို ကိုယ်တိုင် မြင်ရပါမယ်။
#include <stdio.h>
// Function declaration
int add(int a, int b);
int main() {
int result = add(5, 3);
printf("Result: %d", result);
return 0;
}
// Function definition
int add(int a, int b) {
return a + b;
}Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်
- Prototype
int add(int a, int b);Body မပါဘဲ semicolon နဲ့ ဆုံးတဲ့ declaration ပါ။ ဒီ line နောက်ပိုင်း file ထဲက ဘယ်နေရာမှာမဆို add() ကို ခေါ်လို့ ရပါပြီ။
- main()
int main() {Definition မှာ () က parameter မရှိဘူးလို့ ဆိုလိုလို့ အလုပ်လုပ်ပါတယ်။ ဒါပေမယ့် C17 မှာ prototype အပြည့်အစုံ ဖြစ်ဖို့ int main(void) လို့ ရေးတာ ပိုကောင်းပြီး -Wstrict-prototypes ကလည်း () ကို သတိပေးပါတယ်။
- Checked call
int result = add(5, 3);Prototype ရှိလို့ compiler က argument နှစ်ခု ပါမပါနဲ့ int ဖြစ်မဖြစ် စစ်ပါတယ်။ add(5) လို့ ခေါ်ရင် "too few arguments" error ရပါမယ်။
- Output
printf("Result: %d", result);Result: 8 ကို print ထုတ်ပါတယ်။ \n မပါလို့ terminal prompt က တစ်ကြောင်းတည်းမှာ ကပ်ပေါ်နိုင်ပါတယ်၊ ကိုယ့် program တွေမှာ \n ထည့်တာ ကောင်းပါတယ်။
- Definition
int add(int a, int b) {Prototype နဲ့ return type၊ parameter type နဲ့ အစီအစဉ် တစ်ထပ်တည်း ဖြစ်ရပါမယ်။ double add(...) လို့ ပြောင်းရေးမိရင် "conflicting types for 'add'" error ရပါမယ်။
Result: 8File အများကြီးနဲ့ program: header file
Program ကြီးလာရင် ဆက်စပ်တဲ့ function တွေကို ကိုယ်ပိုင် .c file ထဲ ခွဲထားပြီး အပြင်ကို ပေးသုံးမယ့် prototype တွေကို ကိုက်ညီတဲ့ .h file ထဲ ထားပါတယ်။ #ifndef / #define / #endif သုံးကြောင်းက include guard ပါ၊ header တစ်ခုတည်းကို နှစ်ခါ include မိရင်တောင် အထဲက content ကို တစ်ခါပဲ ထည့်ပါတယ်။
#ifndef TEMPERATURE_H
#define TEMPERATURE_H
double c_to_f(double celsius);
int is_freezing(double celsius);
#endif#include "temperature.h" /* lets the compiler compare prototypes */
static double scale(double value) { /* private to this file */
return value * 9.0 / 5.0;
}
double c_to_f(double celsius) {
return scale(celsius) + 32.0;
}
int is_freezing(double celsius) {
return celsius <= 0.0;
}#include <stdio.h>
#include "temperature.h"
int main(void) {
double today = -3.5;
printf("%.1f C = %.1f F\n", today, c_to_f(today));
printf("freezing: %s\n", is_freezing(today) ? "yes" : "no");
return 0;
}Build လုပ်တဲ့အခါ .c file နှစ်ခုလုံးကို compiler ဆီ ပေးရပါတယ်၊ header ကိုတော့ #include က ယူသွားလို့ command line မှာ မထည့်ရပါဘူး။ Program က "-3.5 C = 25.7 F" နဲ့ "freezing: yes" ကို print ထုတ်ပါတယ်။
gcc -std=c17 -Wall -Wextra main.c temperature.c -o temperature
./temperatureလက်တွေ့ project တွေမှာ
Real project တွေမှာ ဘယ်လို တွေ့ရလဲ
#include <stdio.h> လို့ ရေးတိုင်း printf၊ fopen စတဲ့ function တွေရဲ့ prototype တွေကို ယူသုံးနေတာပါ။ Definition တွေကတော့ C library ထဲမှာ compile လုပ်ပြီးသား ရှိနေပြီး linker က ချိတ်ပေးပါတယ်။ SQLite၊ zlib လို library တွေကလည်း sqlite3.h လို header တစ်ခုကို public API အဖြစ် ထုတ်ပေးပြီး implementation ကို သီးခြား ဖုံးထားပါတယ်။
Team project တွေမှာ header file ကို module တစ်ခုရဲ့ စာချုပ်လို သဘောထားပါတယ်။ Header ထဲက prototype ကို ပြောင်းလိုက်ရင် အဲဒီ header ကို include လုပ်ထားတဲ့ file တိုင်း ပြန် compile ဖြစ်ပြီး call အဟောင်းတွေ မကိုက်တော့ရင် compiler က ချက်ချင်း ပြပါတယ်။ Build system (make၊ CMake) တွေကလည်း header ပြောင်းတာကို စောင့်ကြည့်ပြီး လိုအပ်တဲ့ file တွေကိုပဲ ပြန် build လုပ်ပေးပါတယ်။ "undefined reference to" ဆိုတဲ့ linker error တွေ့ရင် prototype ရှိပေမယ့် definition ပါတဲ့ .c file ကို build ထဲ ထည့်ဖို့ မေ့ခဲ့တာ ဖြစ်လေ့ရှိပါတယ်။
အမှားများ
အဖြစ်များတဲ့ အမှားတွေ
1. Prototype နဲ့ definition မကိုက်တာ
double average(int a, int b); လို့ ကြေညာပြီး double average(double a, double b) လို့ define လုပ်ရင် GCC က "conflicting types for 'average'" error ပေးပါတယ်။ ပိုအန္တရာယ်များတာကတော့ prototype က header ထဲမှာ ရှိပြီး define လုပ်တဲ့ .c file က အဲဒီ header ကို include မလုပ်တဲ့ အခြေအနေပါ၊ compiler က နှိုင်းယှဉ်ခွင့် မရလို့ program က အမှား value တွေကို တိတ်တဆိတ် တွက်နိုင်ပါတယ်။ ကိုယ့် header ကို ကိုယ့် .c file ထဲမှာ အမြဲ include လုပ်ပါ။
2. C17 မှာ ကွင်းအလွတ် () နဲ့ ကြေညာတာ
int area(); ဆိုတာ C17 မှာ prototype မဟုတ်ပါဘူး။ area(2, 3, 4) နဲ့ area(2) နှစ်ခုလုံးကို gcc -std=c17 -Wall -Wextra က warning တစ်ခုမှ မပြဘဲ compile လုပ်ပေးပါတယ်။ Argument အရေအတွက် မှားခေါ်တာက undefined behavior ဖြစ်ပြီး area(2) ဆိုရင် h နေရာမှာ ကြုံရာ garbage value ကို သုံးမိပါတယ်။ int area(int w, int h); လို type အပြည့်နဲ့ ရေးပါ၊ -Wstrict-prototypes က "function declaration isn't a prototype" လို့ သတိပေးပါလိမ့်မယ်။
3. Header ထဲမှာ function definition ထည့်တာ
int twice(int x) { return 2 * x; } ကို header ထဲ ရေးပြီး .c file နှစ်ခုက include လုပ်ရင် file တစ်ခုစီမှာ definition တစ်ခုစီ ရှိသွားလို့ linker က "multiple definition of `twice'" error ပေးပါတယ်။ Include guard က file တစ်ခုအတွင်း ထပ်ဝင်တာကိုပဲ ကာကွယ်တာမို့ ဒါကို မကယ်နိုင်ပါဘူး။ Header ထဲမှာ prototype ပဲ ထားပြီး body ကို .c file တစ်ခုတည်းမှာ ထားပါ။
4. Prototype ရှိပြီး definition ကို build ထဲ မထည့်တာ
Prototype က compiler ကို ကျေနပ်စေပေမယ့် definition ကို ကိုယ်တိုင် မဖန်တီးပေးပါဘူး။ gcc main.c လို့ပဲ build လုပ်ပြီး temperature.c ကို မေ့ရင် "undefined reference to `c_to_f'" ရပါတယ်။
Best Practices
အကောင်းဆုံး အလေ့အကျင့်များ
- Parameter မရှိတဲ့ function တွေကို (void) နဲ့ ကြေညာပြီး -Wstrict-prototypes ကို ဖွင့်ထားပါ။
- Prototype တွေမှာ parameter နာမည် အဓိပ္ပာယ်ရှိရှိ ထည့်ရေးပါ၊ ဒါက အခမဲ့ documentation ပါ။
- Module တစ်ခုစီအတွက် .h နဲ့ .c တစ်တွဲ ထားပြီး .c file က ကိုယ့် .h ကို ပထမဆုံး include လုပ်ပါ။
- File အပြင်က မခေါ်ရမယ့် helper တွေကို static လုပ်ပြီး header ထဲ မထည့်ပါနဲ့။
Mini challenge
Mini challenge: main() ကို အရင်ရေးတဲ့ ငွေတောင်းခံလွှာ
main() ကို prototype တွေရဲ့ အောက်၊ definition တွေရဲ့ အပေါ်မှာ ထားပြီး ဆိုင်ငွေတောင်းခံလွှာ သေးသေးလေး print ထုတ်တဲ့ program ရေးပါ။
- double line_total(int quantity, double unit_price) — အရေအတွက် × ဈေးနှုန်း
- double apply_discount(double amount, int percent) — percent ရာခိုင်နှုန်း လျှော့ပေးရမယ်
- void print_line(const char *item, int quantity, double unit_price) — ပစ္စည်းတစ်ကြောင်း print ထုတ်
- Prototype သုံးခုလုံး main() ရဲ့ အပေါ်မှာ၊ definition သုံးခုလုံး main() ရဲ့ အောက်မှာ ရှိရမယ်၊ -Wstrict-prototypes နဲ့ warning မရှိရ
#include <stdio.h>
/* TODO: prototypes for line_total, apply_discount and print_line */
int main(void) {
// TODO: print a line for 2 Notebooks at 1.50 and 5 Pens at 0.50
// TODO: compute the subtotal with line_total()
// TODO: print the subtotal and the total after a 10% discount
return 0;
}
/* TODO: definitions, matching the prototypes exactly */ရလဒ်: Program က ဒီလို print ထုတ်ရပါမယ်: Notebook x2 3.00 Pen x5 2.50 subtotal: 5.50 after 10% discount: 4.95
အရိပ်အမြွက် ကြည့်ရန်
Prototype ကို ရေးဖို့ အလွယ်ဆုံးနည်းက definition ရဲ့ ပထမ line ကို copy ကူးပြီး { အစား ; ထည့်တာပါ။ printf ထဲမှာ % သင်္ကေတကို စာသားအဖြစ် ထုတ်ချင်ရင် %% လို့ ရေးပါ။
အဖြေ ကြည့်ရန်
#include <stdio.h>
/* Prototypes: the compiler reads these before main() */
double line_total(int quantity, double unit_price);
double apply_discount(double amount, int percent);
void print_line(const char *item, int quantity, double unit_price);
int main(void) {
print_line("Notebook", 2, 1.50);
print_line("Pen", 5, 0.50);
double subtotal = line_total(2, 1.50) + line_total(5, 0.50);
printf("subtotal: %.2f\n", subtotal);
printf("after 10%% discount: %.2f\n", apply_discount(subtotal, 10));
return 0;
}
/* Definitions: same return types and parameter types as above */
double line_total(int quantity, double unit_price) {
return quantity * unit_price;
}
double apply_discount(double amount, int percent) {
return amount * (100 - percent) / 100.0;
}
void print_line(const char *item, int quantity, double unit_price) {
printf("%-9s x%d %6.2f\n", item, quantity, line_total(quantity, unit_price));
}Quick Quiz 1 / 3
Quick Quiz 2 / 3
Quick Quiz 3 / 3
မှတ်ထားရမယ့် အချက်များ
- Compiler က file ကို အပေါ်ကနေ အောက် တစ်ခေါက်ပဲ ဖတ်လို့ function ကို မခေါ်ခင် prototype ဒါမှမဟုတ် definition မြင်ပြီးသား ဖြစ်ရပါမယ်။
- Prototype မှာ return type၊ နာမည်နဲ့ parameter type တွေ ပါပြီး definition နဲ့ တစ်ထပ်တည်း ကိုက်ရပါမယ်။
- C17 မှာ parameter မရှိရင် (void) လို့ ရေးပါ၊ () က argument တွေကို မစစ်တော့ပါဘူး။
- Prototype တွေကို include guard ပါတဲ့ header ထဲ ထားပြီး definition တွေကို .c file တစ်ခုတည်းမှာ ထားပါ။
GNU C compiler diagnostics — GNU Project