Thuta Learning
C
BasicProgrammingအခြေခံ

Output

Cသင်ခန်းစာ 425 မိနစ်

ဒီခန်းပြီးရင် ဘာတတ်သွားမလဲ

  • %d, %f, %c, %s, %zu နဲ့ %% ကို သုံးပြီး type မှန်ကန်တဲ့ value တွေကို print ထုတ်နိုင်ဖို့
  • \n, \t, \" နဲ့ \\ escape sequence တွေကို သုံးပြီး line ဘယ်မှာ ဆုံးလဲ ထိန်းနိုင်ဖို့
  • %5d, %-8s, %.2f လို width နဲ့ precision တွေနဲ့ column တွေ တန်းညီအောင် စီနိုင်ဖို့
  • Format-type မကိုက်တာနဲ့ format-string အန္တရာယ်ကို ရှင်းပြပြီး ကာကွယ်နိုင်ဖို့

ရိုးရိုးလေး ရှင်းပြမယ်

ရိုးရိုးလေး ရှင်းရရင်

printf() က C program တစ်ခု console ပေါ်မှာ ကိုယ့်ကို စကားပြောတဲ့ နည်းလမ်းပါ။ ကွက်လပ်တွေပါတဲ့ ဝါကျတစ်ကြောင်း — format string — ကို ပေးပြီး နောက်မှာ ကွက်လပ်တွေကို အစဉ်လိုက် ဖြည့်မယ့် value တွေကို ပေးရပါတယ်။ %d က ကိန်းပြည့်အတွက် ကွက်လပ်၊ %f က ဒသမကိန်းအတွက်၊ %s က text အတွက် ကွက်လပ်ပါ။ Escape sequence လို့ခေါ်တဲ့ character နှစ်လုံးပါ code အထူးတွေက မမြင်ရတဲ့ အရာတွေကို text ထဲ ထည့်ပေးပါတယ် — \n က line အသစ်ကို ဆင်းပေးပါတယ်။

ဒါက ပုံစံတူ စာ (form letter) နဲ့ နည်းနည်း တူပါတယ် — template ကို တစ်ခါပဲ ရေးပြီး ပို့တဲ့အခါမှ အသေးစိတ်တွေကို ဖြည့်ထည့်တာပါ။ မှတ်ထားရမယ့် စည်းမျဉ်းတစ်ခုကတော့ ကွက်လပ်တစ်ခုချင်းကို ဘယ်လို value မျိုးနဲ့ ဖြည့်မလဲဆိုတာ printf က ကိုယ့်ကို လုံးဝ ယုံထားတာပါ။

Problem → Why → Solution

ပြဿနာ: compile ဖြစ်ပြီး run ပေမယ့် အလွဲတွေ print ထုတ်တဲ့ ငွေတောင်းခံလွှာ

ဒီ receipt program က compile ဖြစ်ပြီး run လည်း run ပါတယ်။ ပထမ line က မှန်ပါတယ်။ ဒုတိယ line က 12.5 အစား အဓိပ္ပာယ်မရှိတဲ့ number တစ်ခုကို print ထုတ်ပြီး တတိယ line ကတော့ ဘယ်သူမှ မပေးခဲ့တဲ့ ဒုတိယ number တစ်ခုကို print ထုတ်ပါတယ်။ စမ်း run တစ်ကြိမ်မှာ Total: 0 နဲ့ Paid: 3 of 1 ထွက်ခဲ့ပြီး တခြားစက်မှာ number တွေ ကွဲပြားနိုင်ပါတယ်။ Thank you! ကလည်း နောက်ဆုံးမှာ newline မပါလို့ terminal ရဲ့ prompt နဲ့ ကပ်သွားပါတယ်။

-Wall ဖွင့်ထားရင် GCC က line 5 နဲ့ 6 အတွက် warning ပေးပါတယ်: "format '%d' expects argument of type 'int', but argument 2 has type 'double'" နဲ့ "format '%d' expects a matching 'int' argument"။ Warning ပိတ်ထားရင်တော့ ဘာမှ မပြောပါဘူး။

receipt.c
#include <stdio.h>

int main(void) {
    printf("Items: %d\n", 3);
    printf("Total: %d\n", 12.5);      /* %d, but 12.5 is a double */
    printf("Paid: %d of %d\n", 3);    /* two %d, only one value */
    printf("Thank you!");             /* no newline at the end */
    return 0;
}

printf က ဘာကြောင့် မဖမ်းနိုင်တာလဲ

printf က variadic function တစ်ခုပါ — type မရွေး argument အရေအတွက် မရွေး လက်ခံပြီး သူတို့ရဲ့ type ကို runtime မှာ format string ကို ဖတ်ပြီးမှ သိရပါတယ်။ Format က int လို့ ပြောပြီး double ကို ပို့လိုက်ရင် printf က နေရာမှားကနေ bit မှားတွေကို ဖတ်ပါတယ် — ဒါက undefined behavior ပါ။ Format ကို string literal အဖြစ် ရေးထားရင် compiler က စစ်ပေးနိုင်ပေမယ့် warning တွေ ဖွင့်ထားမှသာ စစ်ပေးတာပါ။

Core concept: format string က ကတိတစ်ခုပါ

printf က format string ကို ဘယ်ကနေ ညာကို လျှောက်ဖတ်ပါတယ်။ ရိုးရိုး character တွေကို ဒီအတိုင်း ကူးထုတ်ပြီး % တစ်ခုကို တွေ့တိုင်း conversion specification တစ်ခုကို ဖတ်ကာ နောက် argument ကို ယူပြီး text အဖြစ် ပြောင်းပါတယ်။ ဒါကြောင့် ပထမ specifier က ပထမ argument နဲ့၊ ဒုတိယ specifier က ဒုတိယ argument နဲ့ တွဲပြီး specifier နဲ့ argument အရေအတွက်ကလည်း ညီရပါမယ်။

SpecifierArgument typeဥပမာ output
%dint42
%f, %.2fdouble (float ကလည်း double အဖြစ် ပြောင်းပြီးမှ ရောက်လာ)3.141590, 3.14
%cchar (int အဖြစ် ပို့တာ)A
%sstring (char array)hello
%zusize_t (sizeof နဲ့ strlen က ပြန်ပေးတာ)5
%lldlong long9000000000
%%Argument မယူ%

Type column ကို အသေးစိတ် နှစ်ချက်က ရှင်းပြပါတယ်။ float ဒါမှမဟုတ် char တစ်ခုကို printf ဆီ ပို့တဲ့အခါ default argument promotions လို့ခေါ်တဲ့ စည်းမျဉ်းတွေအရ C က အရင် ပြောင်းပေးပါတယ် — float ကို double အဖြစ်၊ char ကို int အဖြစ်ပါ။ ဒါကြောင့် %f က float ရော double ရော print ထုတ်ပေးပြီး printf မှာ %lf က %f နဲ့ အဓိပ္ပာယ် တူတူပါပဲ (scanf မှာကျမှ ကွာပါတယ်)။ ပိုကြီးတဲ့ type တွေအတွက်တော့ letter ရှေ့မှာ length modifier လိုပါတယ် — long အတွက် l၊ long long အတွက် ll၊ size_t အတွက် z။

% နဲ့ letter ကြားမှာ flag, width နဲ့ precision တွေ ထည့်လို့ရပါတယ်။ %5d က အနည်းဆုံး column ငါးခု နေရာယူပြီး ညာဘက်ကပ် စီပါတယ်၊ %-8s က - flag ကြောင့် ဘယ်ဘက်ကပ် စီပါတယ်၊ %.2f က ဒသမနောက် ဂဏန်းနှစ်လုံးအထိ round လုပ်ပြီး %8.2f က နှစ်ခုလုံးကို ပေါင်းသုံးတာပါ။ Width က အနည်းဆုံးပဲ ဖြစ်လို့ value က ပိုရှည်ရင် ဖြတ်မပစ်ဘဲ column ကိုသာ ချဲ့သွားပါတယ်။

Escape sequence တွေက တခြား mechanism တစ်ခုပါ။ \n, \t, \" နဲ့ \\ တွေကို compiler က string literal တည်ဆောက်တဲ့အချိန်မှာ character တစ်လုံးစီအဖြစ် ပြောင်းပြီးသား ဖြစ်လို့ printf ဆီ ရောက်တဲ့အခါ တကယ့် newline, tab, quote နဲ့ backslash ဖြစ်နေပါပြီ။ % specifier တွေကိုတော့ printf ကိုယ်တိုင် runtime မှာ ကိုင်တွယ်ပါတယ်၊ ဒါကြောင့် percent sign အစစ် တစ်လုံးလိုရင် %% လို့ ရေးရတာပါ။

text
How specifiers pair with arguments
----------------------------------
printf("%-8s|%5d|%8.2f\n", "Tea", 2, 1.5);
        ^^^^ ^^^ ^^^^^     ^^^^^  ^  ^^^
        1    2   3         1      2  3   <- spec N takes argument N

prints:  Tea     |    2|    1.50
         <---8--> <-5-> <---8-->
Mental model: printf က template ကို ဖြည့်တယ်
  1. printf က format ကို ဘယ်ကနေ ညာ ဖတ်
  2. ရိုးရိုး character တွေကို ဒီအတိုင်း ကူး
  3. % spec တိုင်းက နောက် argument ကို ယူ
  4. Value မဟုတ်ဘဲ spec က type ကို ဆုံးဖြတ်

Argument တွေကို ရေတွက်ပြီး specifier တွေနဲ့ အတွဲလိုက် ချိတ်ကြည့်ပါ — ကိုက်မနေရင် printf က ကိုယ့်အတွက် ပြင်မပေးပါဘူး။

အထွေထွေ ပုံစံ

c
printf("plain text\n");
printf("%d %f %c %s\n", int_value, double_value, char_value, "text");
printf("[%5d] [%-8s] [%8.2f]\n", number, word, amount);   /* width, precision */
printf("100%%\n");                                         /* a literal percent */
puts("puts adds the newline for you");
fprintf(stderr, "error: %s\n", message);                   /* the error stream */

Specifier တစ်ခုစီ နောက်မှာ သူနဲ့ ကိုက်တဲ့ type ရှိတဲ့ argument တစ်ခု လိုက်ရပါမယ်။ Width က column အကျယ်၊ precision က ဒသမ ဂဏန်းအရေအတွက်ကို ထိန်းပါတယ်။ puts က fixed text အတွက် ရိုးရှင်းတဲ့ နည်းလမ်းဖြစ်ပြီး fprintf(stderr, ...) က error message တွေကို သီးခြား stream ဆီ ပို့ပါတယ်။

Example: line ခွဲထားတဲ့ output နဲ့ တန်းညီတဲ့ ဇယား

ပထမ line သုံးကြောင်းက newline ကြောင့် line သီးခြားစီ ဖြစ်သွားတာကို ပြပါတယ်။ အဲဒီနောက် ဇယားသေးလေးက width နဲ့ precision ကို သုံးပြီး ဈေးနှုန်းတွေကို တန်းညီအောင် စီပြပြီး နောက်ဆုံး line က escape sequence တွေနဲ့ %% ကို စုပြထားပါတယ်။

output.c
#include <stdio.h>

int main(void) {
    printf("Hello World!\n");
    printf("I am learning C.\n");
    printf("C is fast and powerful.\n");

    printf("%-8s|%5s|%8s\n", "Item", "Qty", "Price");
    printf("%-8s|%5d|%8.2f\n", "Tea", 2, 1.5);
    printf("%-8s|%5d|%8.2f\n", "Noodles", 1, 3.25);
    printf("Quote: \"C\"  Backslash: \\  Percent: 100%%\n");
    return 0;
}

Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်

  1. Lines 4-6printf("C is fast and powerful.\n");

    Line သုံးကြောင်းလုံး \n နဲ့ ဆုံးပါတယ်။ နောက်ဆုံး line မှာ \n မပါရင် shell prompt က စာသားနောက်မှာ တန်းကပ်ပြီး ပေါ်လာပါလိမ့်မယ်။

  2. Line 8printf("%-8s|%5s|%8s\n", "Item", "Qty", "Price");

    Header row ပါ။ %-8s က Item ကို column ရှစ်ခုထဲမှာ ဘယ်ဘက်ကပ်ထားပြီး %5s နဲ့ %8s က Qty နဲ့ Price ကို ညာဘက်ကပ်ထားပါတယ်၊ ဒါကြောင့် အောက်က number တွေနဲ့ တန်းညီပါတယ်။

  3. Line 9printf("%-8s|%5d|%8.2f\n", "Tea", 2, 1.5);

    Argument တွေကို အစဉ်လိုက် တွဲပါတယ်: string က %-8s ဆီ၊ int 2 က %5d ဆီ၊ double 1.5 က %8.2f ဆီ။ .2 ကြောင့် 1.5 က 1.50 အဖြစ် ထွက်ပါတယ်။

  4. Line 10printf("%-8s|%5d|%8.2f\n", "Noodles", 1, 3.25);

    Format string တူတူကို ပြန်သုံးတာက row တိုင်း column တူတူ ရစေပါတယ်။ Noodles က character ခုနစ်လုံး ဖြစ်လို့ ရှစ်ခုမြောက် column ကို space တစ်ခုနဲ့ ဖြည့်ပါတယ်။

  5. Line 11printf("Quote: \"C\" Backslash: \\ Percent: 100%%\n");

    \" က string ကို မပိတ်ဘဲ double quote တစ်လုံး ထည့်ပေးပြီး \\ က backslash တစ်လုံးကို print ထုတ်ပါတယ်။ %% ကိုတော့ printf က percent sign တစ်လုံးအဖြစ် ပြောင်းပါတယ်။

You should see
Hello World!
I am learning C.
C is fast and powerful.
Item    |  Qty|   Price
Tea     |    2|    1.50
Noodles |    1|    3.25
Quote: "C"  Backslash: \  Percent: 100%

puts, fputs နဲ့ putchar

Value ဖြည့်စရာ မလိုတဲ့ fixed text အတွက် printf ကို မလိုပါဘူး။ puts(text) က text ကို print ထုတ်ပြီး newline တစ်ခု အလိုလို ထည့်ပေးပါတယ်။ fputs(text, stdout) က newline မထည့်တာမို့ line တစ်ကြောင်းကို အပိုင်းလိုက် တည်ဆောက်လို့ရပြီး putchar က character တစ်လုံးကို ထုတ်ပါတယ်။ ဒီ function တွေက text ထဲက % ကို ဘာမှ အထူးမလုပ်ပါဘူး။ printf ကိုယ်တိုင်လည်း print ထုတ်ခဲ့တဲ့ character အရေအတွက်ကို return ပြန်ပေးပါတယ် (error ဖြစ်ရင် အနုတ်ကိန်း)၊ ဒါကြောင့် output ကို ယုံကြည်စိတ်ချရဖို့ လိုတဲ့ program တွေက အဲဒါကို စစ်ကြပါတယ်။

puts_demo.c
#include <stdio.h>

int main(void) {
    puts("puts adds the newline for you");
    fputs("fputs does not, ", stdout);
    fputs("so lines can be built in pieces\n", stdout);
    putchar('O');
    putchar('K');
    putchar('\n');
    return 0;
}

stdout, stderr နဲ့ buffering

printf က standard output (stdout) ဆီ ရေးပါတယ်။ Error message တွေအတွက် fprintf(stderr, ...) နဲ့ standard error ဆီ ရေးပါ။ ./report > out.txt လို့ output ကို file ထဲ redirect လုပ်တဲ့အခါ stdout line ပဲ file ထဲ ရောက်ပြီး stderr line က screen ပေါ်မှာ ဆက်ပေါ်နေလို့ user က error ကို မြင်ရဆဲပါ။

Output ကို ချက်ချင်း ရေးတာ မဟုတ်ဘဲ buffer ထဲ ခဏ စုထားနိုင်ပါတယ်။ C standard က stdout ဟာ interactive device နဲ့ မချိတ်ထားဘူးဆိုတာ သေချာရင် fully buffered ဖြစ်ရမယ်လို့ ပြောပြီး terminal တွေမှာတော့ implementation အများစုက newline တိုင်းမှာ flush လုပ်ပါတယ်။ stderr ကတော့ fully buffered မဖြစ်ပါဘူး။ ဒါကြောင့် pipe ထဲမှာ stderr line က stdout line ထက် အရင် ပေါ်လာနိုင်ပါတယ်။ Text ကို ချက်ချင်း ပေါ်စေချင်ရင် fflush(stdout) ကို ခေါ်ပါ။

streams.c
#include <stdio.h>

int main(void) {
    printf("Report: 3 files checked\n");
    fprintf(stderr, "warning: config.txt not found, using defaults\n");
    return 0;
}

လက်တွေ့ project တွေမှာ

Real project တွေမှာ ဘယ်လို သုံးလဲ

Command-line tool တွေက printf ရဲ့ width နဲ့ precision ကို အမြဲ သုံးကြပါတယ် — file listing, process table နဲ့ benchmark report တွေမှာ column တန်းညီနေတာက ဒီလို specifier တွေကြောင့်ပါ။ Program တွေက ပုံမှန် output ကို stdout ဆီ၊ diagnostic တွေကို stderr ဆီ ခွဲပို့ကြလို့ script တွေက data ကို pipe ထဲ ပို့နေချိန်မှာ error တွေကို လူက ဆက်မြင်နေရပါတယ်။ Embedded system တွေမှာ toolchain အများအပြားက printf ကို serial port ဆီ ပြန်လမ်းကြောင်းပေးနိုင်လို့ screen မရှိတဲ့ board တွေကိုတောင် debug လုပ်လို့ရပါတယ်။

Format language တစ်ခုတည်းကို နေရာအများကြီးမှာ ပြန်သုံးပါတယ် — fprintf က file တွေဆီ၊ snprintf က memory buffer ထဲ ရေးပြီး scanf က input ဖတ်ဖို့ ဆင်တူ specifier တွေကို သုံးပါတယ်။ Format-string bug တွေက တကယ့် security vulnerability အမျိုးအစားတစ်ခု ဖြစ်ခဲ့ဖူးလို့ build setup အတော်များများက -Wformat-security ကို ထပ်ထည့်ကြပြီး user ပေးတဲ့ text ကို format နေရာမှာ ဘယ်တော့မှ မထားဖို့ code review မှာ စစ်ကြပါတယ်။

အမှားများ

အဖြစ်များတဲ့ အမှားတွေ

1. Specifier နဲ့ type မကိုက်တာ

printf("%d\n", 12.5); က undefined behavior ဖြစ်ပြီး ဘာ number မဆို print ထွက်နိုင်ပါတယ်။ size_t လည်း ထောင်ချောက်တစ်ခုပါ — strlen ရဲ့ ရလဒ်ကို %d နဲ့ print ထုတ်ရင် ဒီ lesson ကို စမ်းခဲ့တဲ့ Windows GCC က "argument 2 has type 'size_t' {aka 'long long unsigned int'}" လို့ warning ပေးပါတယ်။ int အတွက် %d၊ double အတွက် %f၊ size_t အတွက် %zu၊ long long အတွက် %lld ကို သုံးပြီး -Wall ကို အမြဲ ဖွင့်ထားပါ။

2. String ထဲမှာ Enter နှိပ်လိုက်တာ

String literal တစ်ခုကို line နှစ်ကြောင်း ဖြန့်ပြီး ရေးလို့ မရပါဘူး — quote ထဲမှာ Enter နှိပ်လိုက်ရင် GCC က missing terminating " character ဆိုတဲ့ error ပေးပြီး နောက်ဆက်တွဲ error တွေ အတန်းလိုက် ထွက်လာပါတယ်။ Output မှာ line အသစ်လိုရင် \n ကို ရေးပါ။ Source code ထဲမှာ စာသားရှည်ကို ခွဲချင်ရင် "Hello, " "world" လို string literal နှစ်ခုကို ကပ်ရေးပါ — compiler က တစ်ခုတည်းအဖြစ် ပေါင်းပေးပါတယ်။

3. User text ကို format အဖြစ် ပို့တာ

printf(name); က name ထဲမှာ "100%sure" လို % တစ်ခု ပါလာတဲ့အထိ အဆင်ပြေသလို ထင်ရပါတယ်။ ပါလာတဲ့အခါ printf က မရှိတဲ့ argument တစ်ခုကို ဖတ်ဖို့ ကြိုးစားပါတယ်။ -Wall -Wextra က ဒါကို မပြောပါဘူး၊ -Wformat-security (ဒါမှမဟုတ် -Wformat=2) ကမှ "format not a string literal and no format arguments" လို့ သတိပေးပါတယ်။ printf("%s", name) ဒါမှမဟုတ် fputs(name, stdout) လို့ ရေးပါ။

4. %.2f က ဂဏန်းကို တိတိကျကျ round လုပ်မယ်လို့ ထင်တာ

%.2f က memory ထဲ သိမ်းထားတဲ့ binary value ကို round လုပ်တာပါ၊ ကိုယ်ရိုက်ခဲ့တဲ့ decimal ဂဏန်းတွေကို မဟုတ်ပါဘူး။ 2.675 က binary မှာ 2.67499... နဲ့ နီးနီးကပ်ကပ် သိမ်းထားလို့ 2.67 ထွက်ပြီး 1.005 ကလည်း 1.00 ထွက်ပါတယ်။ ငွေကြေးလို တိကျမှု လိုတဲ့ value တွေကို integer cent တွေအဖြစ် သိမ်းကြလေ့ ရှိပါတယ်။

Mini challenge

Mini challenge: တန်းညီတဲ့ ငွေတောင်းခံလွှာ

Column တွေ တန်းညီပြီး total ကို C က တွက်ပေးတဲ့ ငွေတောင်းခံလွှာ သေးသေးလေး တစ်ခုကို print ထုတ်ပါ။

  • Item (column ၁၀ ခု ဘယ်ကပ်)၊ Qty (၄ ခု ညာကပ်) နဲ့ Cost (၈ ခု ညာကပ်) ပါတဲ့ header row
  • Rice ၂ ခု (တစ်ခု 1.20) နဲ့ Eggs ၁၂ ခု (တစ်ခု 0.25) အတွက် row နှစ်ခု၊ cost ကို argument ထဲမှာ တွက်ပြီး %8.2f နဲ့ ပြရမယ်
  • Number က Cost column အောက်မှာ တန်းညီနေတဲ့ Total row
  • Percent sign အစစ် ပါတဲ့ နောက်ဆုံး line: Tax: 0% (included)
receipt.c
#include <stdio.h>

int main(void) {
    // TODO: header row: "Item" left-aligned in 10 columns,
    //       "Qty" right-aligned in 4, "Cost" right-aligned in 8

    // TODO: a row for 2 x Rice at 1.20 each; compute the cost in the
    //       printf argument and show it with two decimals

    // TODO: a row for 12 x Eggs at 0.25 each

    // TODO: a Total row whose number ends under the Cost column

    // TODO: print Tax: 0% (included)  (how do you print a % sign?)

    return 0;
}

ရလဒ်: Column တွေ တန်းညီနေတဲ့ ဒီ line ငါးကြောင်း: Item Qty Cost Rice 2 2.40 Eggs 12 3.00 Total 5.40 Tax: 0% (included)

အရိပ်အမြွက် ကြည့်ရန်

Header နဲ့ item row တွေမှာ format width တူတူ (10, 4, 8) ကို သုံးပါ။ Total row မှာ Qty နဲ့ Cost column နှစ်ခုကို ဖုံးဖို့ width 4 + 8 = 12 ရှိတဲ့ specifier တစ်ခုတည်း သုံးလို့ရပါတယ်။

အဖြေ ကြည့်ရန်
receipt.c
#include <stdio.h>

int main(void) {
    printf("%-10s%4s%8s\n", "Item", "Qty", "Cost");
    printf("%-10s%4d%8.2f\n", "Rice", 2, 2 * 1.20);
    printf("%-10s%4d%8.2f\n", "Eggs", 12, 12 * 0.25);
    printf("%-10s%12.2f\n", "Total", 2 * 1.20 + 12 * 0.25);
    printf("Tax: 0%% (included)\n");
    return 0;
}

Quick Quiz 1 / 3

printf("[%5d]\n", 42); က ဘာကို print ထုတ်မလဲ။

Quick Quiz 2 / 3

size_t value n တစ်ခုကို ဘယ် call က မှန်မှန်ကန်ကန် print ထုတ်လဲ။

Quick Quiz 3 / 3

Program က user နာမည်ကို name ထဲ ဖတ်ပြီး printf(name); လို့ ခေါ်ပါတယ်။ ဒါ ဘာကြောင့် အန္တရာယ်ရှိလဲ။

မှတ်ထားရမယ့် အချက်များ

  • printf က format string ကို ဘယ်ကနေ ညာ ဖတ်ပြီး % specifier တစ်ခုချင်းကို နောက် argument နဲ့ အစဉ်လိုက် တွဲပါတယ်။
  • Specifier က type နဲ့ ကိုက်ရပါမယ်: int အတွက် %d၊ double အတွက် %f၊ char အတွက် %c၊ string အတွက် %s၊ size_t အတွက် %zu။
  • \n လို escape sequence တွေကို compiler က ပြောင်းပြီး %% လို specifier တွေကို printf က runtime မှာ ကိုင်တွယ်ပါတယ်။
  • %5d, %-8s, %.2f လို width နဲ့ precision တွေက column တွေကို တန်းညီစေပါတယ်။
  • User text ကို format အဖြစ် ဘယ်တော့မှ မပို့ပါနဲ့ — printf("%s", text) ကို သုံးပြီး -Wall ကို ဖွင့်ထားပါ။

GNU C compiler diagnostics — GNU Project

နောက်ဆုံးပြင်ဆင်ရက်

Progress ကို ဤ browser (ဤစက်) တွင်သာ သိမ်းထားပါသည်။ အခြားစက်များနှင့် sync မလုပ်ပါ။

ဤသင်ခန်းစာတွင် အမှားတွေ့ပါက အကြောင်းကြားရန်သင့် email app ကို ဤသင်ခန်းစာလိပ်စာ ဖြည့်ပြီးသားဖြင့် ဖွင့်ပေးပါမည် — သင်ကိုယ်တိုင် ပို့မှသာ ပို့မည်ဖြစ်သည်။

Output | Thuta Learning