Thuta Learning
C
AdvancedProgrammingအခြေခံ

User Input

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

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

  • scanf() က ဘာ return ပြန်လဲ (1၊ 0၊ EOF) ရှင်းပြပြီး မစစ်ရင် ဖြစ်လာမယ့် bug တွေကို ပြနိုင်ဖို့
  • fgets(buf, sizeof buf, stdin) နဲ့ line တစ်ကြောင်းလုံးကို ဘေးကင်းစွာ ဖတ်ပြီး EOF နဲ့ line ရှည်လွန်းတာကို ကိုင်တွယ်နိုင်ဖို့
  • strtol() ရဲ့ end pointer နဲ့ errno ကို သုံးပြီး ဂဏန်းမဟုတ်တာ၊ အပိုစာလုံးနဲ့ overflow ကို ခွဲခြား စစ်နိုင်ဖို့
  • gets() ကို C11 က ဘာကြောင့် ဖယ်ရှားခဲ့လဲ ရှင်းပြနိုင်ဖို့

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

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

Keyboard ကနေ ဝင်လာတဲ့ data က number မဟုတ်ပါဘူး၊ user ရိုက်လိုက်တဲ့ စာလုံးတွေ တန်းစီနေတာပါ။ "20" လို့ ရိုက်ပြီး Enter နှိပ်ရင် program ဆီ '2'၊ '0' နဲ့ newline ('\n') ဆိုတဲ့ character သုံးလုံး ရောက်လာပါတယ်။ Number အဖြစ် သုံးချင်ရင် ကိုယ်တိုင် ပြောင်းပြီး မှန်မမှန် စစ်ရပါတယ်။

User က "twenty" လို့ ရိုက်နိုင်တယ်၊ Enter ပဲ နှိပ်နိုင်တယ်၊ စာကြောင်း အရှည်ကြီး paste လုပ်နိုင်တယ်၊ ဘာမှ မရိုက်ဘဲ input ကို ပိတ်လိုက်နိုင်ပါတယ်။ ဒါကြောင့် input ကို ဧည့်သည် ယူလာတဲ့ ထုပ်ပိုး တစ်ခုလို သဘောထားပြီး မဖွင့်ခင် စစ်ဆေးရပါတယ်။

Problem → Why → Solution

ပြဿနာ: "abc" ရိုက်လိုက်တော့ program ရူးသွားတယ်

အောက်က program က 0 နဲ့ 130 ကြား အသက် ရမချင်း ထပ်မေးဖို့ ရည်ရွယ်ပါတယ်။ 20 ရိုက်ရင် ကောင်းကောင်း အလုပ်လုပ်ပါတယ်။ "abc" ရိုက်လိုက်ရင်တော့ "Enter your age: " က အဆုံးမရှိ ထပ်ခါထပ်ခါ ပေါ်လာပြီး ဘာမှ ထပ်ရိုက်ခွင့် မရတော့ပါဘူး။ Loop မပါတဲ့ version မှာဆိုရင် age ထဲက ကြုံရာ garbage value ကို print ထုတ်ပါတယ်။

age_bug.c
#include <stdio.h>

int main(void) {
    int age = -1;
    while (age < 0 || age > 130) {
        printf("Enter your age: ");
        scanf("%d", &age);          /* return value ignored */
    }
    printf("You are %d years old.\n", age);
    return 0;
}

ဘာကြောင့် loop က မဆုံးတော့တာလဲ

scanf("%d") က 'a' ကို တွေ့တာနဲ့ ရပ်ပြီး 0 (conversion တစ်ခုမှ မအောင်ဘူး) ကို return ပြန်ပါတယ်။ "abc" က input buffer ထဲမှာ မစားရသေးဘဲ ကျန်နေလို့ နောက် loop တစ်ကြိမ်မှာ scanf က အဲဒီ 'a' ကိုပဲ ပြန်တွေ့ပါတယ်၊ age ကလည်း ဘယ်တော့မှ မပြောင်းပါဘူး။ Return value ကို စစ်ပြီး မှားတဲ့ line ကို ဖယ်ရှားမှသာ ဒီ loop က ပြေလည်ပါမယ်။

Core concept: line လိုက်ဖတ်၊ ပြီးမှ စစ်ပြီး ပြောင်း

Terminal က input ကို line လိုက် စုထားပြီး user က Enter နှိပ်မှ program ဆီ ပို့ပါတယ်၊ newline character လည်း အဲဒီ input ထဲမှာ ပါပါတယ်။ Input အဆုံးကို EOF (end of file) လို့ ခေါ်ပြီး Linux နဲ့ macOS မှာ Ctrl+D၊ Windows မှာ Ctrl+Z ပြီး Enter နဲ့ ပို့လို့ရပါတယ်။ Input ကို file တစ်ခုကနေ redirect လုပ်ထားရင်တော့ file ဆုံးတာနဲ့ EOF ရပါတယ်။

scanf("%d", &age) က ရလဒ်ကို variable ထဲ ရေးရလို့ address (&age) လိုပါတယ်။ သူ့ရဲ့ return value က အောင်မြင်တဲ့ conversion အရေအတွက်ပါ: 1 က အောင်မြင်တာ၊ 0 က စာသားက number နဲ့ မစတာ (မှားတဲ့ စာလုံးတွေ buffer ထဲ ကျန်နေ)၊ EOF က input ကုန်သွားတာပါ။ 0 ဒါမှမဟုတ် EOF ရချိန်မှာ age ကို လုံးဝ မထိလို့ initialize မလုပ်ထားရင် garbage ဖြစ်နေပါတယ်။ int ထဲ မဆံ့တဲ့ number ကို %d နဲ့ ဖတ်တာကလည်း standard အရ undefined behavior ပါ၊ number နောက်က newline ကိုလည်း buffer ထဲ ချန်ထားခဲ့ပါတယ်။

ပိုခိုင်မာတဲ့ နည်းက အဆင့်နှစ်ဆင့် ခွဲတာပါ။ ပထမ fgets(buf, sizeof buf, stdin) နဲ့ line တစ်ကြောင်းလုံးကို ဖတ်ပါ၊ သူက sizeof buf - 1 ထက် ပိုမရေးဘဲ '\0' ကို အမြဲ ထည့်ပေးပြီး EOF ဒါမှမဟုတ် error ဆိုရင် NULL ပြန်ပါတယ်။ Line ထဲမှာ '\n' မပါရင် line က buffer ထက် ရှည်နေတာပါ။ ဒုတိယ strtol(buf, &end, 10) နဲ့ ပြောင်းပါ: end == buf ဆိုရင် digit တစ်လုံးမှ မရှိ၊ end နောက်မှာ space မဟုတ်တဲ့ စာလုံး ကျန်ရင် "12abc" လို အပိုစာ ပါနေ၊ errno == ERANGE ဆိုရင် long ထဲ မဆံ့ပါဘူး။ နောက်ဆုံးမှာ program အတွက် range မှန်မမှန် စစ်ပါ။

ရှေးက gets(buf) ကတော့ buffer အရွယ်အစားကို လက်မခံလို့ user က ရှည်ရှည် ရိုက်တာနဲ့ buffer အပြင်ဘက် memory ကို ဖျက်ဆီးပါတယ်။ ဘယ်လိုမှ ဘေးကင်းအောင် သုံးလို့ မရလို့ C11 က language ထဲကနေ ဖယ်ရှားလိုက်ပြီး fgets() က အစားထိုးပါတယ်။

မှတ်ထားရမယ့် ပုံစံ: စစ်ဆေးရေး ဂိတ်လေးခု
  1. fgets: line ရလား၊ EOF လား
  2. Line ထဲ '\n' ပါလား (ရှည်လွန်းလား)
  3. strtol: digit ရှိလား၊ အပိုစာ ကျန်လား
  4. ERANGE နဲ့ min..max range စစ်

ဂိတ်လေးခုလုံး ဖြတ်ပြီးမှ value ကို program ထဲ သုံးပါ။

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

c
char buf[SIZE];
if (fgets(buf, sizeof buf, stdin) == NULL) {
    /* EOF or read error: stop or use a default */
}
char *end;
errno = 0;
long value = strtol(buf, &end, 10);
/* reject if: end == buf, junk after end, errno == ERANGE,
   or value outside [min, max] */

fgets က line တစ်ကြောင်းကို buffer အရွယ်အစားအတွင်းမှာပဲ ဖတ်ပြီး strtol က ပြောင်းတဲ့အပြင် ဘယ်နေရာမှာ ရပ်လဲ (end) ကိုပါ ပြောပြပါတယ်။ စစ်ချက်လေးခုထဲက တစ်ခုခု မှားရင် input ကို ငြင်းပါ။

Example: input line တွေကို စစ်ပြီး ပြောင်းခြင်း

ဒီ example က keyboard ကို မစောင့်ဘဲ fgets() ပေးမယ့် ပုံစံအတိုင်း ('\n' အပါ) ပြင်ဆင်ထားတဲ့ line ခုနစ်ကြောင်းကို parse_int() နဲ့ စစ်ပါတယ်။ ဒါကြောင့် ဘယ်အချိန် run run output တူတူ ရပြီး user က ရိုက်နိုင်တဲ့ အမှားမျိုးစုံကို တစ်ခါတည်း မြင်ရပါတယ်။ တကယ့် keyboard နဲ့ ချိတ်တဲ့ version ကို deeper section မှာ ကြည့်ပါ။

parse_age.c
#include <ctype.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/* Parses a whole line as an int in [min, max].
   Returns NULL on success, or a message saying what is wrong. */
const char *parse_int(const char *line, int min, int max, int *out) {
    char *end;
    errno = 0;
    long value = strtol(line, &end, 10);
    if (end == line) {
        return "not a number";
    }
    while (isspace((unsigned char)*end)) {
        end++;                          /* allow trailing spaces and '\n' */
    }
    if (*end != '\0') {
        return "extra characters after the number";
    }
    if (errno == ERANGE || value < min || value > max) {
        return "out of range";
    }
    *out = (int)value;
    return NULL;
}

int main(void) {
    /* Lines exactly as fgets() would hand them over, '\n' included. */
    const char *lines[] = {"20\n", "  35\n", "abc\n", "12abc\n",
                           "150\n", "\n", "99999999999999999999\n"};
    int count = (int)(sizeof lines / sizeof lines[0]);

    for (int i = 0; i < count; i++) {
        int age;
        int shown = (int)strcspn(lines[i], "\n");   /* length before '\n' */
        const char *problem = parse_int(lines[i], 0, 130, &age);
        if (problem == NULL) {
            printf("\"%.*s\" -> age %d\n", shown, lines[i], age);
        } else {
            printf("\"%.*s\" -> rejected: %s\n", shown, lines[i], problem);
        }
    }
    return 0;
}

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

  1. Convert and remember where it stoppedlong value = strtol(line, &end, 10);

    strtol က ရှေ့က space တွေကို ကျော်ပြီး digit တွေကို long အဖြစ် ပြောင်းကာ ပထမဆုံး ပြောင်းမရတဲ့ character ရဲ့ address ကို end ထဲ ထည့်ပါတယ်။ 10 က decimal ဖြစ်တယ်လို့ ပြောတာပါ။ ခေါ်မယ့်အခါ errno ကို 0 လို့ အရင် ပြန်ထားပါတယ်။

  2. No digits at allif (end == line) {

    end က line အစမှာပဲ ရှိနေရင် digit တစ်လုံးမှ မတွေ့တာပါ ("abc" နဲ့ line အလွတ်)။ strtol က 0 ပြန်ပေးလို့ ဒီစစ်ချက် မပါရင် "abc" ကို အသက် 0 လို့ မှားလက်ခံမိပါမယ်။

  3. Trailing junkif (*end != '\0') {

    Space နဲ့ '\n' ကို ကျော်ပြီးနောက် string အဆုံး မရောက်သေးရင် "12abc" လို အပိုစာလုံး ကျန်နေတာပါ။ scanf("%d") ကတော့ ဒီလို line ကို 12 အဖြစ် ငြိမ်ငြိမ်လေး လက်ခံပါတယ်။

  4. Overflow and rangeif (errno == ERANGE || value < min || value > max) {

    Number ကြီးလွန်းရင် strtol က errno ကို ERANGE ထားပါတယ်။ long က Windows မှာ 32-bit၊ Linux 64-bit မှာ 64-bit ဖြစ်ပေမယ့် ဒီစစ်ချက်နှစ်ခုကြောင့် platform နှစ်ခုလုံးမှာ "out of range" ရပါတယ်။

  5. Showing the line without its newlineint shown = (int)strcspn(lines[i], "\n"); /* length before '\n' */

    strcspn က '\n' မတိုင်ခင် character အရေအတွက်ကို ပြန်ပေးပြီး %.*s က အဲဒီ အရေအတွက်အထိပဲ print ထုတ်ပါတယ်။ fgets ရဲ့ newline ကို ဖယ်ဖို့ line[strcspn(line, "\n")] = '\0'; လို့လည်း သုံးလေ့ရှိပါတယ်။

You should see
"20" -> age 20
"  35" -> age 35
"abc" -> rejected: not a number
"12abc" -> rejected: extra characters after the number
"150" -> rejected: out of range
"" -> rejected: not a number
"99999999999999999999" -> rejected: out of range

တကယ့် keyboard ကနေ ဖတ်ခြင်း

အောက်က program က stdin ကနေ တကယ် ဖတ်ပါတယ်။ Terminal မှာ run ရင် prompt ပြပြီး စောင့်ပါမယ်။ Input မရှိဘဲ run ရင် (ဥပမာ ./age < /dev/null) fgets က ချက်ချင်း NULL ပြန်လို့ program က ယဉ်ကျေးစွာ ပြီးဆုံးပါတယ်။ Prompt မှာ newline မပါလို့ fflush(stdout) နဲ့ ချက်ချင်း ပေါ်အောင် လုပ်ထားပါတယ်။

age_prompt.c
#include <ctype.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void) {
    char line[32];

    printf("Enter your age: ");
    fflush(stdout);                      /* show the prompt before waiting */
    if (fgets(line, sizeof line, stdin) == NULL) {
        printf("\nNo input (end of file), nothing to do.\n");
        return 0;
    }
    if (strchr(line, '\n') == NULL && !feof(stdin)) {
        int c;                           /* line too long: drain the rest */
        while ((c = getchar()) != '\n' && c != EOF) {
        }
        printf("That line is too long.\n");
        return 0;
    }

    char *end;
    errno = 0;
    long age = strtol(line, &end, 10);
    int no_digits = (end == line);
    while (isspace((unsigned char)*end)) {
        end++;
    }
    if (no_digits || *end != '\0' || errno == ERANGE || age < 0 || age > 130) {
        printf("Please type a whole number from 0 to 130.\n");
        return 0;
    }
    printf("You are %ld years old.\n", age);
    return 0;
}

ရှည်လွန်းတဲ့ line ရဲ့ ကျန်တဲ့ အပိုင်း

Line က buffer ထဲ မဆံ့ရင် ကျန်တဲ့ စာလုံးတွေက stdin ထဲမှာ ကျန်နေပြီး နောက် fgets ခေါ်တဲ့အခါ "line အသစ်" အဖြစ် ရောက်လာပါတယ်။ getchar() loop က '\n' ဒါမှမဟုတ် EOF ရောက်တဲ့အထိ အဲဒီ အပိုင်းကို စားပစ်ပါတယ်။

scanf၊ fgets + strtol နဲ့ gets ကို နှိုင်းယှဉ်ခြင်း

နည်းလမ်းအားသာချက်သတိထားရမယ့်အချက်
scanf("%d", &n)ရေးရ တိုတယ်Return value မစစ်ရင် garbage၊ "12abc" ကို 12 လို့ လက်ခံ၊ '\n' ကျန်
fgets + strtolLine တစ်ကြောင်းလုံးကို စစ်နိုင်၊ overflow ကို ERANGE နဲ့ သိCode ပိုရှည်၊ '\n' ကို ကိုယ်တိုင် ကိုင်တွယ်ရ
gets(buf)မရှိပါC11 က ဖယ်ရှားပြီး၊ buffer overflow ကို မကာကွယ်နိုင်

Compiler တွေကလည်း gets ကို တားပါတယ်။ MinGW-w64 GCC 16 က -std=c17 နဲ့ "Using gets() is always unsafe - use fgets() instead" လို့ warning ပေးပြီး Linux ရဲ့ glibc ကတော့ C11 နဲ့ နောက်ပိုင်း mode တွေမှာ gets ကို ကြေညာတောင် မကြေညာတော့ပါဘူး။

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

Real project တွေမှာ ဘယ်လို တွေ့ရလဲ

Command-line tool တွေက user ရိုက်တဲ့ menu ရွေးချယ်မှု၊ port number နဲ့ config value တွေကို fgets + strtol ပုံစံနဲ့ ဖတ်လေ့ရှိပြီး မှားရင် ဘာမှားလဲ ရှင်းရှင်း ပြောပြီး ထပ်မေးပါတယ်။ Input ကို file ဒါမှမဟုတ် pipe ကနေ ပေးနိုင်လို့ (./tool < answers.txt) EOF ကို ကိုင်တွယ်တာက automation script တွေ ပိတ်မိမနေအောင် အရေးကြီးပါတယ်။

Security ဘက်ကကြည့်ရင် gets() နဲ့ width မပါတဲ့ scanf("%s") လို buffer အရွယ်အစား မစစ်တဲ့ input code က exploit ရှာတဲ့သူတွေ အရင်ဆုံး ကြည့်တဲ့ နေရာပါ။ 1988 ခုနှစ်က Morris worm က fingerd program ထဲက gets() ကို overflow လုပ်ပြီး ပျံ့နှံ့ခဲ့တာ နာမည်ကြီးပါတယ်။ Embedded device တွေနဲ့ server တွေမှာ input တိုင်းကို အရှည်၊ ပုံစံ၊ range စစ်ပြီးမှ သုံးတာကို code review တွေမှာ အမြဲ တောင်းဆိုပါတယ်။

အမှားများ

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

1. scanf() ရဲ့ return value ကို လျစ်လျူရှုတာ

"abc" ရိုက်ရင် scanf က 0 ပြန်ပြီး variable ကို မထိပါဘူး၊ Ctrl+D / Ctrl+Z ဆိုရင် EOF ပြန်ပါတယ်။ int age = 7; ထားပြီး စမ်းကြည့်ရင် အမှားနှစ်မျိုးလုံးမှာ age က 7 အတိုင်း ကျန်ပါတယ်။ if (scanf("%d", &age) != 1) { ... } လို့ စစ်ပြီး မှားတဲ့ line ကို ဖယ်ရှားပါ၊ ပိုကောင်းတာက fgets + strtol ကို ပြောင်းသုံးတာပါ။

2. scanf မှာ & မေ့တာ

scanf("%d", age) က age ရဲ့ value ကို address အဖြစ် သုံးလို့ program crash ဖြစ်နိုင်ပါတယ်။ -Wall ဖွင့်ထားရင် GCC က "format '%d' expects argument of type 'int *', but argument 2 has type 'int'" လို့ ပြပါတယ်။ ဒီ warning ကို ဘယ်တော့မှ လျစ်လျူ မရှုပါနဲ့။

3. Width မပါတဲ့ %s နဲ့ string ဖတ်တာ

char name[20]; scanf("%s", name); က စာလုံး 20 ကျော်ရိုက်ရင် buffer ကို ကျော်ရေးပါတယ်၊ ဒီ code ကို -Wall က warning မပေးပါဘူး။ scanf("%19s", name) လို width ထည့်ပါ (null terminator အတွက် တစ်နေရာ ချန်ပါ)၊ ဒါမှမဟုတ် fgets ကို သုံးပါ။

4. scanf နဲ့ fgets ကို ရောသုံးတာ

scanf("%d", &age) က number နောက်က '\n' ကို buffer ထဲ ချန်ခဲ့လို့ နောက်က fgets(name, ...) က line အလွတ်ကို ချက်ချင်း ပြန်ပေးပြီး user ကို နာမည် ရိုက်ခွင့် မပေးပါဘူး။ Program တစ်ခုလုံးမှာ fgets ကိုပဲ သုံးပြီး line တစ်ကြောင်းချင်း parse လုပ်တာ အရှင်းဆုံးပါ။

Best Practices

အကောင်းဆုံး အလေ့အကျင့်များ

  • Input ကို fgets နဲ့ line လိုက်ဖတ်ပြီး strtol / strtod နဲ့ ပြောင်းကာ check လေးခုလုံး လုပ်ပါ။
  • Parse လုပ်တဲ့ code ကို parse_int() လို function သီးသန့်ထဲ ထားပြီး string တွေနဲ့ test လုပ်ပါ။
  • Prompt နောက်မှာ fflush(stdout) ခေါ်ပြီး EOF ရရင် program ကို ယဉ်ကျေးစွာ ရပ်ပါ။
  • User ကို ဘာမှားလဲ (number မဟုတ်၊ range ကျော်) တိတိကျကျ ပြောပြပါ။

Mini challenge

Mini challenge: order line တစ်ကြောင်း စစ်ခြင်း

"3 4.50" လို "အရေအတွက် ဈေးနှုန်း" ပုံစံ line တစ်ကြောင်းကို strtol နဲ့ strtod ဆက်တိုက် သုံးပြီး parse လုပ်ကာ မှန်ရင် စုစုပေါင်း ကျသင့်ငွေ၊ မှားရင် rejected လို့ print ထုတ်ပါ။

  • int parse_order(const char *line, long *quantity, double *price) — အောင်မြင်ရင် 1၊ မဟုတ်ရင် 0 ပြန်ရမယ်
  • Quantity က 1 ကနေ 100၊ price က 0 ထက်ကြီးပြီး 10000 ထက် မကျော်ရ
  • strtol ရဲ့ end ကနေ strtod ကို ဆက်စပြီး နောက်ဆုံး space မဟုတ်တဲ့ အပိုစာ ကျန်ရင် ငြင်းရမယ်
  • fgets ပေးမယ့် ပုံစံ line ခြောက်ကြောင်း ({"3 4.50\n", "2 0.99\n", "three 4.50\n", "3\n", "3 4.50 extra\n", "0 4.50\n"}) နဲ့ စမ်းရမယ်
order_line.c
#include <ctype.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/* Parses "quantity price", e.g. "3 4.50". Returns 1 on success. */
int parse_order(const char *line, long *quantity, double *price) {
    // TODO: strtol for the quantity; check digits, ERANGE, 1..100
    // TODO: strtod from where strtol stopped; check digits, ERANGE, range
    // TODO: skip trailing spaces; anything else left means reject
    // TODO: store the results through the pointers
    return 0;
}

int main(void) {
    // TODO: an array with the six test lines from the requirements
    // TODO: for each line print "total %.2f" or "rejected"
    return 0;
}

ရလဒ်: Program က ဒီလို print ထုတ်ရပါမယ်: 3 4.50 total 13.50 2 0.99 total 1.98 three 4.50 rejected 3 rejected 3 4.50 extra rejected 0 4.50 rejected

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

strtol ကို ခေါ်ပြီးရင် end ကို const char *rest ထဲ မှတ်ထားပြီး strtod(rest, &end) ကို ခေါ်ပါ။ end == rest ဆိုရင် price မပါပါဘူး ("3" လို line)။ Column ညီအောင် printf("%-14.*s", shown, line) ကို သုံးနိုင်ပါတယ်။

အဖြေ ကြည့်ရန်
order_line.c
#include <ctype.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/* Parses "quantity price", e.g. "3 4.50". Returns 1 on success. */
int parse_order(const char *line, long *quantity, double *price) {
    char *end;
    errno = 0;
    long q = strtol(line, &end, 10);
    if (end == line || errno == ERANGE || q < 1 || q > 100) {
        return 0;
    }
    const char *rest = end;
    errno = 0;
    double p = strtod(rest, &end);
    if (end == rest || errno == ERANGE || p <= 0.0 || p > 10000.0) {
        return 0;
    }
    while (isspace((unsigned char)*end)) {
        end++;
    }
    if (*end != '\0') {
        return 0;                        /* something left over */
    }
    *quantity = q;
    *price = p;
    return 1;
}

int main(void) {
    const char *lines[] = {"3 4.50\n", "2 0.99\n", "three 4.50\n",
                           "3\n", "3 4.50 extra\n", "0 4.50\n"};
    int count = (int)(sizeof lines / sizeof lines[0]);

    for (int i = 0; i < count; i++) {
        long quantity;
        double price;
        int shown = (int)strcspn(lines[i], "\n");
        if (parse_order(lines[i], &quantity, &price)) {
            printf("%-14.*s total %.2f\n", shown, lines[i], quantity * price);
        } else {
            printf("%-14.*s rejected\n", shown, lines[i]);
        }
    }
    return 0;
}

Quick Quiz 1 / 3

int n = 7; int r = scanf("%d", &n); ကို run ပြီး user က abc လို့ ရိုက်ရင် r နဲ့ n က ဘာဖြစ်မလဲ။

Quick Quiz 2 / 3

C11 က gets() ကို language ထဲကနေ ဘာကြောင့် ဖယ်ရှားခဲ့တာလဲ။

Quick Quiz 3 / 3

char *end; long v = strtol("12abc", &end, 10); ပြီးရင် ဘာဖြစ်မလဲ။

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

  • Keyboard input က newline ပါတဲ့ စာသား ဖြစ်ပြီး EOF နဲ့လည်း ဆုံးနိုင်လို့ ယုံလို့မရတဲ့ data အဖြစ် သဘောထားပါ။
  • scanf() က အောင်မြင်တဲ့ conversion အရေအတွက် (ဒါမှမဟုတ် EOF) ကို ပြန်ပေးလို့ မစစ်ရင် garbage နဲ့ အဆုံးမရှိ loop ဖြစ်နိုင်ပါတယ်။
  • fgets(buf, sizeof buf, stdin) နဲ့ ဖတ်ပြီး strtol ရဲ့ end၊ errno နဲ့ range ကို စစ်တာက စံ ပုံစံပါ။
  • gets() ကို C11 က ဖယ်ရှားလိုက်ပြီး buffer အရွယ်အစား ပါတဲ့ fgets() က အစားထိုးပါတယ်။

GNU C compiler diagnostics — GNU Project

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

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

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

User Input | Thuta Learning