Thuta Learning
C
BasicProgrammingအခြေခံ

Data Types

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

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

  • Value တစ်ခုအတွက် int, double, float, char ထဲက သင့်တော်တာကို ရွေးပြီး ကိုက်ညီတဲ့ format specifier နဲ့ print ထုတ်နိုင်ဖို့
  • 7 / 2 က 3 ဖြစ်ပြီး 7 / 2.0 က 3.5 ဖြစ်ရတဲ့ အကြောင်းရင်းကို ရှင်းပြနိုင်ဖို့
  • အာမခံထားတဲ့ size (sizeof(char) က 1) နဲ့ platform ပေါ် မူတည်တဲ့ size (long က Windows မှာ 4 byte၊ Linux မှာ 8 byte) ကို ခွဲခြားနိုင်ဖို့
  • Width အတိအကျ လိုတဲ့အခါ <stdint.h> ရဲ့ fixed-width type တွေနဲ့ size_t ကို သုံးနိုင်ဖို့

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

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

Data type ဆိုတာ variable တစ်ခုပေါ်မှာ ကပ်ထားတဲ့ တံဆိပ်တစ်ခုလိုပါပဲ။ အဲဒီ တံဆိပ်က compiler ကို အချက်နှစ်ချက် ပြောပါတယ် — memory ထဲမှာ byte ဘယ်နှစ်ခု ဖယ်ထားရမလဲ၊ အဲဒီ byte တွေထဲက bit တွေကို ဘယ်လို အဓိပ္ပာယ်ဖွင့်ရမလဲ ဆိုတာပါ။ Bit 32 ခု အတူတူကိုပဲ type ပေါ်မူတည်ပြီး whole number တစ်ခုအဖြစ်လည်း ဖတ်နိုင်သလို decimal number တစ်ခုအဖြစ်လည်း ဖတ်နိုင်ပါတယ်။

နေ့စဉ် code ရေးတဲ့အခါ type လေးမျိုးကို အများဆုံး သုံးပါလိမ့်မယ် — ပစ္စည်းအရေအတွက်လို whole number အတွက် int၊ အကွာအဝေးလို decimal အတွက် double၊ grade letter လို character တစ်လုံးအတွက် char၊ memory ချွေတာဖို့ လိုပြီး precision သိပ်မလိုတဲ့အခါ float ပါ။ အစကတည်းက type မှန်မှန် ရွေးထားရင် calculation ရော print ထုတ်တဲ့ output ပါ မှန်ကန်နေပါမယ်။

Problem → Why → Solution

ပြဿနာ: တစ်ဝက် ပျောက်သွားတဲ့ ပျမ်းမျှ

ပစ္စည်းနှစ်ခုအတွက် 1999 cents သိမ်းထားပြီး တစ်ခုချင်းရဲ့ ပျမ်းမျှဈေးကို တွက်ချင်တယ် ဆိုပါစို့။ average ကို double အဖြစ်တောင် ကြေညာထားလို့ 999.5 ရမယ်လို့ မျှော်လင့်ပါတယ်။ ဒါပေမယ့် program က အဓိပ္ပာယ်မရှိတဲ့ value တစ်ခုကို print ထုတ်ပြီး compiler ပြခဲ့တဲ့ warning ကိုလည်း scroll ဆွဲပြီး ကျော်သွားမိပါတယ်။ ဒီ line အနည်းငယ်ထဲမှာ type အမှား နှစ်ခု ပုန်းနေပါတယ် — division က double ဆီ မရောက်ခင် int နှစ်ခုကြားမှာ ဖြစ်သွားပြီး printf ကိုလည်း int (%d) ဖတ်ပါလို့ ပြောထားပေမယ့် double ကို ပေးလိုက်တာပါ။

average.c
#include <stdio.h>

int main(void) {
  int total_cents = 1999;
  int count = 2;
  double average = total_cents / count;   /* int / int: 999, not 999.5 */
  printf("Average: %d\n", average);       /* %d reads an int, gets a double */
  return 0;
}

Type က ဘာလို့ ဒီလောက် အရေးကြီးတာလဲ

C က expression တစ်ခုကို ဘယ်လို တွက်မလဲဆိုတာ ရလဒ်ကို လက်ခံမယ့် variable ကနေ မဟုတ်ဘဲ operand တွေရဲ့ type ကနေ ဆုံးဖြတ်ပါတယ်။ 1999 / 2 က integer division ဖြစ်လို့ assignment က 999 ကို 999.0 အဖြစ် မပြောင်းခင်မှာပဲ .5 ကို လွှင့်ပစ်လိုက်ပါတယ်။ printf ကတော့ ကိုယ့် variable ရဲ့ type ကို လုံးဝ မမြင်ရပါဘူး — format string ကိုပဲ ယုံပြီး မကိုက်ရင် undefined behaviour ဖြစ်ပါတယ်။ -Wall နဲ့ compile လုပ်ရင် GCC က format '%d' expects argument of type 'int', but argument 2 has type 'double' လို့ သတိပေးပါတယ်။

Core concept: size, range နဲ့ အဓိပ္ပာယ်

C type တိုင်းက မေးခွန်း သုံးခုကို ဖြေပေးပါတယ် — byte ဘယ်နှစ်ခု နေရာယူလဲ၊ အဲဒီ byte တွေထဲ ဘယ် value တွေ ဆံ့လဲ၊ အဲဒီ type ပေါ်က operation တွေ ဘယ်လို အလုပ်လုပ်လဲ ဆိုတာပါ။ ပထမမေးခွန်းကို sizeof က ဖြေပေးပါတယ်။ char ကတော့ definition အရ 1 byte အတိအကျ ဖြစ်ပြီး mainstream platform တိုင်းမှာ byte တစ်ခုက 8 bit ပါ။ တခြား integer type တွေမှာတော့ အနည်းဆုံး အာမခံချက်ပဲ ရှိပါတယ် — int က အနည်းဆုံး 16 bit (ခန့်မှန်းခြေ -32,767 ကနေ 32,767)၊ long က အနည်းဆုံး 32 bit၊ long long က အနည်းဆုံး 64 bit ဖြစ်ရပါမယ်။

Width အစစ်ကိုတော့ platform က ရွေးပါတယ်။ ဒီနေ့ desktop နဲ့ server compiler တွေမှာ int က 32 bit (အပေါင်းအနုတ် ခန့်မှန်းခြေ 2.1 billion) ဖြစ်ပေမယ့် long ကတော့ 64-bit Windows မှာ 32 bit၊ 64-bit Linux နဲ့ macOS မှာ 64 bit ဖြစ်ပါတယ်။ long ထဲမှာ ဂဏန်း ၁၀ လုံးပါတဲ့ ဖုန်းနံပါတ် ဆံ့မယ်လို့ ယူဆထားတဲ့ code က စက်တစ်လုံးမှာ အလုပ်လုပ်ပြီး နောက်တစ်လုံးမှာ တိတ်တဆိတ် ပျက်သွားပါတယ်။ Width အတိအကျ အရေးကြီးရင် <stdint.h> ကို include လုပ်ပြီး နာမည်ထဲမှာ width ပါတဲ့ int32_t, uint8_t, int64_t တို့ကို သုံးပါ။

Integer type တိုင်းမှာ signed (အနုတ် ဖြစ်နိုင်) နဲ့ unsigned (သုည နဲ့ အထက်၊ ဒါပေမယ့် အပေါင်းဘက် range နှစ်ဆ) ဆိုပြီး နှစ်မျိုး ရှိပါတယ်။ Limit ကို ကျော်တဲ့အခါ ဒီနှစ်မျိုး မတူပါဘူး — unsigned arithmetic က 2 ရဲ့ N ထပ်ကိန်းနဲ့ modulo လုပ်ပြီး ပတ်လည်သွားလို့ 0u - 1 က အကြီးဆုံး unsigned value ဖြစ်သွားပါတယ်။ Signed int overflow ကတော့ undefined behaviour ပါ — program က နံပါတ်မှား ထုတ်တာ၊ crash ဖြစ်တာ၊ optimizer ကြောင့် ထင်မထားတဲ့ ပုံစံ ပြောင်းသွားတာ ဖြစ်နိုင်ပါတယ်။ ဘယ်တစ်ခုကိုမှ အားမကိုးဘဲ နေရာ အပိုရှိတဲ့ type ကို ရွေးပါ။

Decimal တွေအတွက် float က significant digit 6-7 လုံးလောက်၊ double က 15-16 လုံးလောက် ပေးလို့ default အဖြစ် double ကို ရွေးတာ သဘာဝကျပါတယ်။ ဘယ်တစ်ခုကမှ decimal fraction အများစုကို အတိအကျ မသိမ်းနိုင်ပါဘူး — 0.1 ဆိုတာ တကယ်တော့ အနီးစပ်ဆုံး binary fraction တစ်ခုပါ၊ ဒါကြောင့် ငွေကြေးကို integer cents အဖြစ် သိမ်းလေ့ ရှိပါတယ်။ နောက်ဆုံးအနေနဲ့ size နဲ့ အရေအတွက်တွေအတွက် sizeof ကိုယ်တိုင် return ပြန်တဲ့ unsigned type ဖြစ်တဲ့ size_t ကို သုံးပြီး %zu နဲ့ print ထုတ်ပါတယ်။

Typeသုံးလေ့ရှိတဲ့ နေရာprintf
intအရေအတွက်၊ score%d
long longအလွန်ကြီးတဲ့ whole number%lld
unsigned intအနုတ် ဘယ်တော့မှ မဖြစ်တဲ့ value%u
doubleအတိုင်းအတာ၊ ပျမ်းမျှ%f, %.2f
charcharacter တစ်လုံး%c (code ကို %d)
size_tsizeof ရလဒ်၊ length%zu
Mental model: တံဆိပ်ကပ်ထားတဲ့ byte သေတ္တာ
  1. Type က သေတ္တာ size (sizeof) ကို သတ်မှတ်
  2. Type က ဆံ့နိုင်တဲ့ value range ကို သတ်မှတ်
  3. Operand type တွေက arithmetic ပုံစံကို ရွေး
  4. Format specifier က type နဲ့ ကိုက်ရမယ်

Compiler က သေတ္တာပေါ်က တံဆိပ်ကို ဖတ်ပါတယ်၊ printf ကတော့ format string ထဲမှာ ကိုယ်ရေးပေးတဲ့ တံဆိပ်ကိုပဲ ဖတ်ပါတယ်။

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

c
type name = initial_value;

unsigned int count = 0u;      /* u suffix: unsigned literal */
long long big = 0LL;          /* LL suffix: long long literal */
double ratio = 0.0;           /* decimal point: double literal */
float small = 0.0f;           /* f suffix: float literal */
char letter = 'A';            /* single quotes: one character */

printf("%u %lld %f %c %zu\n", count, big, ratio, letter, sizeof(type));

Type ကို အရင်ရေး၊ ပြီးမှ နာမည်နဲ့ initial value ကို ရေးပါတယ်။ Literal နောက်က suffix (u, LL, f) နဲ့ decimal point က literal ကိုယ်တိုင်ရဲ့ type ကို compiler ကို ပြောပါတယ်။ printf ထဲက conversion တိုင်းက သက်ဆိုင်ရာ argument နဲ့ ကိုက်ရပါမယ်။

Example: type တစ်ခုချင်းနဲ့ သူ့ specifier

ဒီ example က နေ့စဉ်သုံး type တစ်ခုစီနဲ့ variable တစ်ခုစီ ကြေညာပြီး ကိုက်ညီတဲ့ specifier နဲ့ print ထုတ်ပါတယ်။ နောက်ဆုံး line လေးကြောင်းက စမ်းသပ်ချက်တွေပါ — float ရဲ့ precision ကန့်သတ်ချက်၊ integer division နဲ့ floating-point division ကွာခြားချက်၊ ယုံကြည်လို့ရတဲ့ size နှစ်ခုကို ပြပါတယ်။ sizeof(int) က mainstream compiler တွေမှာ 4 ထွက်ပေမယ့် standard ကတော့ အနည်းဆုံး 16 bit လို့ပဲ ကတိပေးထားတာကို သတိပြုပါ။

data_types.c
#include <stdio.h>

int main(void) {
  int items = 5;
  double distance = 12.7564;
  float ratio = 0.1f;
  char initial = 'T';

  printf("Items: %d\n", items);
  printf("Distance: %.3f km\n", distance);
  printf("Initial: %c (code %d)\n", initial, initial);
  printf("float  0.1 -> %.10f\n", ratio);
  printf("double 0.1 -> %.10f\n", 0.1);
  printf("7 / 2 = %d, 7 / 2.0 = %.1f\n", 7 / 2, 7 / 2.0);
  printf("sizeof(char) = %zu, sizeof(int) = %zu\n", sizeof(char), sizeof(int));
  return 0;
}

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

  1. Lines 5-6float ratio = 0.1f;

    distance က double ဖြစ်ပြီး ratio က float ပါ။ 0.1f ရဲ့ f suffix က literal ကို float ဖြစ်စေပါတယ်။ f မပါရင် 0.1 က double literal ဖြစ်ပါတယ်။

  2. Line 10printf("Distance: %.3f km\n", distance);

    %.3f က double ကို ဒသမ ၃ နေရာနဲ့ ပြလို့ 12.7564 က 12.756 ဖြစ်သွားပါတယ်။ float argument က printf ဆီ ရောက်တဲ့အခါ double အဖြစ် promote ဖြစ်လို့ printf မှာ float ရော double ပါ %f နဲ့ ရပါတယ် (%lf လည်း ရပါတယ်)။ scanf ကတော့ double အတွက် %lf မဖြစ်မနေ လိုပါတယ်။

  3. Line 11printf("Initial: %c (code %d)\n", initial, initial);

    char က တကယ်တော့ integer အသေးလေး တစ်ခုပါ။ %c က character ကို ပြပြီး %d က အဲဒီ character ရဲ့ code ကို ပြပါတယ် — ASCII မှာ 'T' က 84 ပါ။

  4. Lines 12-13printf("float 0.1 -> %.10f\n", ratio);

    ဒသမ ၁၀ နေရာထိ ချဲ့ကြည့်လိုက်ရင် float က 0.1000000015 လို့ ပြပြီး double က 0.1000000000 လို့ ပြပါတယ်။ နှစ်ခုလုံး 0.1 ကို အတိအကျ မသိမ်းနိုင်ပေမယ့် double ရဲ့ error က ပိုသေးလို့ ဒီ precision မှာ မမြင်ရတော့တာပါ။

  5. Line 147 / 2, 7 / 2.0

    Operand နှစ်ခုလုံး int ဖြစ်တော့ 7 / 2 က integer division ဖြစ်ပြီး 3 ရပါတယ်။ 2.0 က double ဖြစ်လို့ 7 ကို double အဖြစ် ပြောင်းပြီးမှ စားတာမို့ 3.5 ရပါတယ်။

  6. Line 15sizeof(char), sizeof(int)

    sizeof က size_t ကို ပြန်ပေးလို့ %zu နဲ့ print ထုတ်ရပါတယ်။ char က အမြဲ 1 ဖြစ်ပြီး int က ဒီမှာ 4 ပါ — ဒါပေမယ့် ဒါက platform ရွေးထားတာ ဖြစ်ပြီး standard ကတိ မဟုတ်ပါဘူး။

You should see
Items: 5
Distance: 12.756 km
Initial: T (code 84)
float  0.1 -> 0.1000000015
double 0.1 -> 0.1000000000
7 / 2 = 3, 7 / 2.0 = 3.5
sizeof(char) = 1, sizeof(int) = 4

Limit တွေ၊ fixed-width type တွေနဲ့ conversion

<limits.h> က ကိုယ့် platform ပေါ်က integer type တွေရဲ့ limit အစစ်ကို INT_MAX, INT_MIN, UINT_MAX လို macro တွေနဲ့ ပေးပါတယ်။ <stdint.h> ကတော့ int32_t, int64_t, uint8_t လို နာမည်ထဲမှာ bit အရေအတွက် ပါတဲ့ type တွေကို ပေးပါတယ်၊ ဒီ type တွေက ရှိခဲ့ရင် width အတိအကျ ဖြစ်ကြောင်း အာမခံပါတယ်။

Type တစ်ခုကနေ တစ်ခုကို ပြောင်းတဲ့အခါ စည်းမျဉ်းတွေ ရှိပါတယ်။ double ကို int ထဲ ထည့်ရင် fraction ကို သုည ဘက်ကို ဖြတ်ပစ်လို့ (int)-3.99 က -3 ဖြစ်ပါတယ် (round မလုပ်ပါဘူး)။ Unsigned type ထဲ မဆံ့တဲ့ value ကို ထည့်ရင် modulo နဲ့ ပတ်လည်သွားလို့ 300 က unsigned char ထဲမှာ 44 ဖြစ်ပါတယ်။ Signed type ထဲ မဆံ့တာကို ထည့်ရင် ရလဒ်က implementation-defined ဖြစ်ပြီး range ပြင်ပ double ကို int အဖြစ် ပြောင်းတာကတော့ undefined behaviour ပါ။

limits_demo.c
#include <stdio.h>
#include <limits.h>
#include <stdint.h>

int main(void) {
  printf("int: %d to %d\n", INT_MIN, INT_MAX);
  printf("unsigned int max: %u\n", UINT_MAX);
  printf("int64_t: %zu bytes, uint8_t: %zu byte\n", sizeof(int64_t), sizeof(uint8_t));

  double reading = -3.99;
  int truncated = (int)reading;
  int big = 300;
  unsigned char level = big;
  printf("(int)%.2f = %d\n", reading, truncated);
  printf("300 stored in unsigned char = %d\n", level);
  return 0;
}

INT_MAX + 1 ကို မစမ်းပါနဲ့

Signed overflow ဖြစ်တာနဲ့ program တစ်ခုလုံးရဲ့ behaviour ကို standard က အာမမခံတော့ပါဘူး။ ကိုယ့်စက်မှာ -2147483648 ထွက်တာ မြင်ရရင်တောင် optimization level ပြောင်းတာနဲ့ ရလဒ် ပြောင်းနိုင်ပါတယ်။ Limit နားကပ်မယ့် value တွေအတွက် long long ဒါမှမဟုတ် int64_t ကို သုံးပါ။

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

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

Real C code မှာ type ရွေးတာက design ဆုံးဖြတ်ချက် တစ်ခုပါ။ Embedded firmware က hardware register က 16 bit အတိအကျ ဖြစ်လို့ sensor reading ကို uint16_t ထဲ ဖတ်ပါတယ်။ Network protocol parser က wire format မှာ 4 byte လို့ ပြောထားလို့ packet length ကို "int က ဘယ်လောက်ပဲ ကြီးကြီး" မဟုတ်ဘဲ uint32_t နဲ့ သိမ်းပါတယ်။ အဲဒီနေရာမှာ ရိုးရိုး int သုံးရင် ကိုယ့် laptop မှာ အဆင်ပြေပေမယ့် int က 16 bit ပဲ ရှိတဲ့ microcontroller မှာ မှားသွားပါလိမ့်မယ်။

Business software မှာလည်း ကိုယ်ပိုင် စည်းမျဉ်းတွေ ရှိပါတယ်။ 0.10 ကို ဆယ်ခါ ပေါင်းရင် 1.00 အတိအကျ ရစေဖို့ ဈေးနှုန်းတွေကို integer cents (int64_t ဒါမှမဟုတ် long long) အဖြစ် သိမ်းလေ့ ရှိပြီး rounding error အသေးလေးတွေ အရေးမကြီးတဲ့ အကွာအဝေး၊ အပူချိန်၊ ratio လို အတိုင်းအတာတွေအတွက် double ကို သုံးပါတယ်။ Array length နဲ့ buffer size တွေကတော့ size_t နဲ့ သွားလို့ strlen လို function တွေက size_t ကို return ပြန်တာပါ။ Project အသစ် ဒါမှမဟုတ် board အသစ်တစ်ခုမှာ စအလုပ်လုပ်တဲ့အခါ ကိုယ်အားကိုးမယ့် type တွေရဲ့ sizeof ကို print ထုတ်ကြည့်ပြီး -Wall -Wextra နဲ့ compile လုပ်ရင် format နဲ့ conversion မကိုက်တာတွေကို ship မလုပ်ခင် ဖမ်းမိပါတယ်။

အမှားများ

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

1. long က 64 bit လို့ ယူဆတာ

long က 64-bit Linux နဲ့ macOS မှာ 8 byte ဖြစ်ပေမယ့် 64-bit Windows မှာ 4 byte ပဲ ရှိပါတယ်။ ဒါကြောင့် 5000000000 လို value က တစ်ဖက်မှာ ဆံ့ပြီး နောက်တစ်ဖက်မှာ overflow ဖြစ်ပါတယ်။ 64 bit လိုရင် long long ဒါမှမဟုတ် int64_t ကို သုံးပါ။

2. printf specifier မကိုက်တာ

double ကို %d နဲ့၊ long ကို %d နဲ့ print ထုတ်တာက undefined behaviour ဖြစ်ပြီး အမှိုက် value တွေ ထွက်ပါတယ်။ GCC ရဲ့ -Wall က format '%d' expects argument of type 'int', but argument 2 has type 'long int' လို့ ပြောပြီး %ld ကိုတောင် အကြံပြုပါတယ်။ ဒီ warning တွေကို error လို သဘောထားပါ။

3. Integer division ကနေ ဒသမ မျှော်တာ

total နဲ့ count နှစ်ခုလုံး int ဖြစ်နေရင် double avg = total / count; ကလည်း ဖြတ်ပစ်ပါသေးတယ်၊ division က assignment မတိုင်ခင် ဖြစ်လို့ပါ။ Operand တစ်ခုကို ပြောင်းပေးပါ — (double)total / count လို့ ရေးပါ။

Best Practices

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

  • Decimal တွေအတွက် default အနေနဲ့ double ကို သုံးပါ၊ float ကို memory တကယ် ချွေတာဖို့ လိုမှ ရွေးပါ။
  • File format၊ protocol၊ hardware register လို width အတိအကျ သတ်မှတ်ထားတဲ့ data အတွက် <stdint.h> type တွေကို သုံးပါ။
  • ငွေကြေးကို double မဟုတ်ဘဲ integer cents အဖြစ် သိမ်းပါ။
  • အမြဲ -Wall -Wextra နဲ့ compile လုပ်ပြီး format warning တစ်ခုမှ မကျန်စေပါနဲ့။

Mini challenge

Mini challenge: type မှန်တဲ့ ဈေးဝယ်စာရင်း တစ်ကြောင်း

ဈေးဝယ် receipt ရဲ့ line တစ်ကြောင်းကို ဖော်ပြတဲ့ program တစ်ခု ရေးပါ။ Value တိုင်းအတွက် သင့်တော်တဲ့ type ကို ရွေးပြီး specifier မှန်မှန်နဲ့ print ထုတ်ပါ။

  • အရေအတွက် (int) 3၊ unit price (double) 2.75၊ size letter (char) 'M'၊ stock id (<stdint.h> ရဲ့ int32_t) 120045 ကို သိမ်းရမယ်
  • Line total ကို ဒသမ ၂ နေရာနဲ့ print ထုတ်ရမယ်
  • int နှစ်ခုဖြစ်တဲ့ loyalty points 7 ကို အရေအတွက်နဲ့ စားပြီး fraction မပျောက်စေဘဲ ပြရမယ်
  • Stock id ကို long အဖြစ် ပြောင်းပြီး %ld နဲ့၊ သူ့ size ကို %zu နဲ့ print ထုတ်ရမယ်
  • -Wall -Wextra နဲ့ compile လုပ်ရင် warning တစ်ခုမှ မထွက်ရ
receipt_line.c
#include <stdio.h>
#include <stdint.h>

int main(void) {
  /* TODO: quantity, unit price, size letter */
  /* TODO: total_points (int) and stock_id (int32_t) */

  /* TODO: line total = quantity * unit price */
  /* TODO: points per item, keeping the fraction */

  /* TODO: print the size letter, the total and the points (2 decimals) */
  /* TODO: print the stock id and how many bytes it uses */
  return 0;
}

ရလဒ်: Warning မရှိဘဲ compile ဖြစ်ပြီး ဒီ line လေးကြောင်း ထွက်ရပါမယ်: Size: M Line total: 8.25 Points per item: 2.33 Stock id 120045 uses 4 bytes

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

စားတဲ့အခါ fraction မပျောက်ဖို့ (double)total_points / quantity လို့ cast အရင်လုပ်ပါ။ sizeof ကို type name မှာသာမက variable မှာလည်း သုံးလို့ရပြီး size_t ကို ပြန်ပေးပါတယ်။

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

int main(void) {
  int quantity = 3;
  double unit_price = 2.75;
  char size_code = 'M';
  int total_points = 7;
  int32_t stock_id = 120045;

  double line_total = quantity * unit_price;
  double avg_points = (double)total_points / quantity;

  printf("Size: %c\n", size_code);
  printf("Line total: %.2f\n", line_total);
  printf("Points per item: %.2f\n", avg_points);
  printf("Stock id %ld uses %zu bytes\n", (long)stock_id, sizeof stock_id);
  return 0;
}

Quick Quiz 1 / 3

printf("%d\n", 7 / 2); က ဘာ print ထုတ်မလဲ။

Quick Quiz 2 / 3

64-bit Windows ပေါ်က GCC မှာ sizeof(long) က ပုံမှန်အားဖြင့် ဘာ ပြလဲ။

Quick Quiz 3 / 3

double price = 4.5; ကို ဒသမ ၂ နေရာနဲ့ မှန်မှန်ကန်ကန် print ထုတ်တာ ဘယ်ဟာလဲ။

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

  • Type က byte အရေအတွက်၊ ဆံ့နိုင်တဲ့ value တွေနဲ့ operation တွေရဲ့ အလုပ်လုပ်ပုံကို သတ်မှတ်ပါတယ်။
  • char က အမြဲ 1 byte ဖြစ်ပေမယ့် int, long တို့မှာ အနည်းဆုံး အာမခံချက်ပဲ ရှိပါတယ် — long က Windows မှာ 4 byte၊ Linux မှာ 8 byte ပါ။
  • int / int က fraction ကို ဖြတ်ပစ်လို့ ဒသမ လိုရင် operand တစ်ခုကို double ပြောင်းပါ။
  • printf specifier က argument type နဲ့ ကိုက်ရမယ် (%d, %u, %lld, %f, %c, %zu)၊ မကိုက်ရင် undefined behaviour ပါ။
  • Width အတိအကျ လိုရင် int32_t လို <stdint.h> type တွေ၊ size တွေအတွက် size_t ကို သုံးပါ။

GNU C compiler diagnostics — GNU Project

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

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

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

Data Types | Thuta Learning