Thuta Learning
C++
BasicProgrammingအခြေခံ

Data Types

C++သင်ခန်းစာ 630 မိနစ်

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

  • int, double, char, bool, std::string ကို သူတို့ကိုင်ထားတဲ့ value အလိုက် ရွေးနိုင်ဖို့
  • sizeof နဲ့ std::numeric_limits ကို သုံးပြီး type တစ်ခုရဲ့ byte အရေအတွက်နဲ့ အကြီးဆုံး value ကို ရှာနိုင်ဖို့
  • std::int32_t, std::int64_t, std::uint8_t လို fixed-width type တွေကို ဘယ်အချိန် သုံးသင့်လဲ ရှင်းပြနိုင်ဖို့
  • 7 / 2 က ဘာကြောင့် 3 ဖြစ်လဲ၊ 0.1 + 0.2 က ဘာကြောင့် 0.3 နဲ့ မညီလဲ ရှင်းပြနိုင်ဖို့
  • static_cast နဲ့ conversion တွေကို ရှင်းရှင်းလင်းလင်း လုပ်ပြီး overflow ကို ကြိုမြင်နိုင်ဖို့

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

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

Data type ဆိုတာ variable တစ်ခုက ဘယ်လို value မျိုး ကိုင်ထားလဲ၊ memory ဘယ်လောက် လိုလဲ ဆိုတာကို compiler ကို ပြောတဲ့ အရာပါ။ ဆေးသေတ္တာ၊ ရေသန့်ဗူး၊ ကုန်တင်သေတ္တာကြီး အသီးသီးက ထည့်မယ့် ပစ္စည်းနဲ့ ကိုက်အောင် ပုံသဏ္ဌာန် ရှိတာလိုပါပဲ၊ မှားထည့်ရင် မဆံ့ဘူး ဒါမှမဟုတ် ပစ္စည်း ပျက်ပါတယ်။ C++ မှာ ကိန်းပြည့်၊ ဒသမကိန်း၊ character တစ်လုံး၊ true/false အတွက် built-in type တွေ ပါပြီး text အတွက်တော့ standard library က std::string ကို ထပ်ပေးထားပါတယ်။

ရွေးလိုက်တဲ့ type က operator တွေရဲ့ အဓိပ္ပာယ်ကိုပါ ဆုံးဖြတ်ပါတယ်။ 7 / 2 ကို ကိန်းပြည့်နှစ်ခုနဲ့ စားရင် 3 ရပြီး တစ်ခုက ဒသမပါရင် 3.5 ရပါတယ်။ ဒါကြောင့် type ရွေးတာက သိမ်းဆည်းမှုအကြောင်းသက်သက် မဟုတ်ဘဲ range, တိကျမှုနဲ့ အပြုအမူကို ရွေးတာပါ။

Problem → Why → Solution

ပြဿနာ: error မပြဘဲ မှားသွားတဲ့ ဂဏန်းသုံးခု

ဆိုင်တစ်ဆိုင်ရဲ့ script က ဈေးနှုန်း၊ ပျမ်းမျှ rating နဲ့ page view စုစုပေါင်းကို report လုပ်ပါတယ်။ ရိုးရိုးလေး ထင်ရတဲ့ line သုံးကြောင်း တစ်ပြိုင်နက် မှားပါတယ် — int price = 19.99; က 19 ကို သိမ်းတယ်၊ int average = 7 / 2; က 3 ကို သိမ်းတယ်၊ int views = 3'000'000'000; က အနုတ်ကိန်း တစ်ခုကို သိမ်းပါတယ်။ g++ -Wall -Wextra နဲ့ compile လုပ်ရင် ဘယ်တစ်ကြောင်းမှ warning မပေးဘဲ program က အဆုံးထိ run ပြီး 19 3 -1294967296 လို့ print ထုတ်ပါတယ်။

shop_report.cpp
#include <iostream>

int main() {
    int price = 19.99;            // 19: the .99 is dropped
    int average = 7 / 2;          // 3: whole-number division
    int views = 3'000'000'000;    // does not fit in a 32-bit int
    std::cout << price << ' ' << average << ' ' << views << '\n';
}

ဘာကြောင့် ဒီလို ဖြစ်တာလဲ

ဒီရလဒ်တွေအားလုံးက type စည်းမျဉ်းတွေအရ တရားဝင်တဲ့ အကျိုးဆက်တွေပါ။ int က များသောအားဖြင့် byte ၄ ခုပဲ ရှိလို့ ၂၁၄၇၄၈၃၆၄၇ အထိပဲ ဆံ့ပါတယ်၊ ကြီးတဲ့ value ကို ထည့်ရင် 2^32 နဲ့ ပတ်လည်ပြီး ဆံ့တဲ့အပိုင်းကိုပဲ ယူပါတယ်။ ငယ်တဲ့ ဒါမှမဟုတ် တိကျမှု နည်းတဲ့ type ထဲ ပြောင်းတဲ့အခါ ဆံ့တာကို ထားပြီး ကျန်တာ ဖယ်ပစ်ပါတယ်။ ကိန်းပြည့်နှစ်ခု စားရင်လည်း ကိန်းပြည့် စားခြင်းပဲ ဖြစ်ပါတယ်။ Compiler က ကိုယ်တမင်လုပ်တယ်လို့ ယူဆပါတယ်။ Brace initialization က အဲဒီထဲက ပထမနဲ့ တတိယကို ဖမ်းပေးပါတယ်၊ int price{19.99}; က compile မဖြစ်ပါဘူး။

Core concept: type က byte အရေအတွက်နဲ့ အဓိပ္ပာယ်ကို ဆုံးဖြတ်တယ်

Type တစ်ခုက compiler ကို အချက်နှစ်ချက် ပြောပါတယ် — value တစ်ခုက byte ဘယ်နှခု နေရာယူလဲ၊ အဲဒီ byte ထဲက bit တွေကို ဘယ်လို ဖတ်ရမလဲ ဆိုတာပါ။ bit ရှစ်ခု ပုံစံတူတစ်ခုက int ဆိုရင် number 65၊ char ဆိုရင် စာလုံး A ဖြစ်နိုင်ပါတယ်။ sizeof operator က type ဒါမှမဟုတ် variable တစ်ခုရဲ့ byte အရေအတွက်ကို ပြောပြပြီး <limits> ထဲက std::numeric_limits<T> က အဲဒီ type ရဲ့ အကြီးဆုံး၊ အသေးဆုံး value တွေကို ပြောပြပါတယ်။

ကိန်းပြည့်မိသားစုမှာ int, short, long, long long နဲ့ unsigned ပုံစံတွေ ပါပါတယ်။ Standard က အနည်းဆုံးပမာဏကိုပဲ အာမခံလို့ အရွယ်အစားတွေ ကွာနိုင်ပါတယ် — int က အဓိက platform တွေမှာ byte ၄ ခုဖြစ်ပေမယ့် long က Windows မှာ ၄ ခု၊ 64-bit Linux နဲ့ macOS မှာ ၈ ခုပါ။ အရွယ်အစား အတိအကျ လိုရင် <cstdint> ထဲက fixed-width type တွေဖြစ်တဲ့ std::int32_t, std::int64_t, std::uint8_t ကို သုံးပါ၊ နာမည်ထဲမှာတင် bit အရေအတွက် ပါပါတယ်။

ကိန်းပြည့် type တိုင်းမှာ range ရှိပါတယ် — byte ၄ ခုပါတဲ့ int က 2,147,483,647 မှာ ရပ်ပါတယ်။ unsigned type တွေကတော့ အဓိပ္ပာယ်အရပဲ ပတ်လည်ပါတယ် — 32-bit unsigned int ရဲ့ 0 ထဲက 1 နုတ်ရင် 4,294,967,295 ဖြစ်ပါတယ်။ Signed overflow ကတော့ မတူပါဘူး၊ undefined behavior ဖြစ်လို့ ပတ်လည်မယ်လို့ အားမကိုးပါနဲ့။

double (byte ၈ ခု၊ ဒသမ ၁၅ လုံးခန့်) နဲ့ float (byte ၄ ခု၊ ဒသမ ၆ လုံးခန့်) က binary နဲ့ အပိုင်းကိန်း သိမ်းပါတယ်။ ဒသမ အပိုင်းကိန်း အများစုဖြစ်တဲ့ 0.1 လိုတန်ဖိုးတွေကို binary နဲ့ အတိအကျ မဖော်ပြနိုင်လို့ ရလဒ်က အနည်းငယ် လွဲပါတယ် — 0.1 + 0.2 က 0.30000000000000004 ပါ။ double ကို default လုပ်ပြီး == အစား ခံနိုင်ရည်နဲ့ နှိုင်းယှဉ်ပါ။

char က character တစ်လုံးကို သေးငယ်တဲ့ integer code အဖြစ် ကိုင်ပြီး bool က true ဒါမှမဟုတ် false ကို ကိုင်ပါတယ်။ std::string ကတော့ built-in type မဟုတ်ဘဲ library type ဖြစ်ပြီး ဘယ်လောက်ရှည်ရှည် text ကို ကိုင်နိုင်ပါတယ်။ Operator တွေက operand ရဲ့ type အတိုင်း အလုပ်လုပ်တာမို့ 7 / 2 က နှစ်ခုလုံး int ဖြစ်လို့ 3 ရပြီး 7 / 2.0 က 3.5 ရပါတယ်။

Mental model: type ကို ကန့်သတ်ချက်သုံးခုအဖြစ် မြင်ပါ
  1. Type က byte အရေအတွက်ကို ပုံသေ သတ်မှတ်
  2. Bit တွေကို type အလိုက် ဖတ်
  3. Operator တွေက type အတိုင်း အလုပ်လုပ်
  4. Range ကို ကျော်ရင် အမှားက တိတ်တိတ်ပဲ ဖြစ်

ဂဏန်းက မှန်နေပေမယ့် type က မှားနေရင် compiler က မပြောတတ်ပါဘူး၊ ဒါကြောင့် value ဘယ်လောက် ကြီးနိုင်လဲ ကိုယ်တိုင် မေးပါ။

အထွေထွေ ပုံစံ: type တွေကို ရွေးပြီး တိုင်းတာတာ

cpp
Type name{value};                  // int, double, char, bool, std::string
std::int32_t exact{value};         // fixed width: int8/16/32/64_t, uint8/16/32/64_t
sizeof(Type)                       // bytes used by a type (or by a variable)
std::numeric_limits<Type>::max()   // largest value; needs <limits>
static_cast<Target>(value)         // explicit, visible conversion
7 / 2    ->  3                     // int / int: whole-number division
7 / 2.0  ->  3.5                   // one double: decimal division

ပထမနှစ်ကြောင်းက variable ကြေညာပုံ၊ sizeof နဲ့ numeric_limits က type ရဲ့ အရွယ်နဲ့ range ကို မေးတာဖြစ်ပြီး static_cast က conversion ကို code ထဲမှာ မြင်ရအောင် လုပ်တာပါ။ နောက်ဆုံး line နှစ်ကြောင်းကတော့ operand type က ရလဒ်ကို ဘယ်လို ပြောင်းလဲလဲ ပြတာပါ။

Example: course ရဲ့ အချက်အလက်တွေကို type အလိုက် ကြည့်မယ်

ဒီ program က value ခြောက်ခုကို type မတူတဲ့ variable တွေထဲ ထည့်ပြီး အရေအတွက်၊ ပျမ်းမျှ၊ section၊ အောင်/ကျ နဲ့ download အရေအတွက်ကို print ထုတ်ပါတယ်။ တစ်ကြောင်းစီမှာ type က byte ဘယ်နှခု သုံးလဲ ဆိုတာကိုလည်း sizeof နဲ့ ပြပါတယ်။ downloads က ၃ ဘီလီယံပါလို့ int ထဲ မဆံ့ပါဘူး၊ ဒါကြောင့် std::int64_t ကို သုံးထားတာပါ။ Output ကို အရင် ခန့်မှန်းကြည့်ပါ၊ အထူးသဖြင့် bool ကို ဘယ်လို print ထုတ်မလဲ ဆိုတာပါ။

course_facts.cpp
#include <cstdint>
#include <format>
#include <iostream>
#include <string>

int main() {
    int students{35};
    double average{82.75};
    char section{'B'};
    bool passed{true};
    std::string course{"C++ Foundation"};
    std::int64_t downloads{3'000'000'000};  // does not fit in a 32-bit int

    std::cout << course << '\n';
    std::cout << std::format("Students: {} ({} bytes)\n", students, sizeof(students));
    std::cout << std::format("Average: {} ({} bytes)\n", average, sizeof(average));
    std::cout << std::format("Section: {} ({} byte)\n", section, sizeof(section));
    std::cout << std::format("Passed: {} ({} byte)\n", passed, sizeof(passed));
    std::cout << std::format("Downloads: {} ({} bytes)\n", downloads, sizeof(downloads));
    return 0;
}

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

  1. Line 7int students{35};

    ကိန်းပြည့်တစ်ခုပါ။ int က နေ့စဉ်သုံး default type ဖြစ်ပြီး အရေအတွက်၊ index၊ အသက် တွေအတွက် သင့်တော်ပါတယ်။

  2. Line 8double average{82.75};

    ဒသမပါတဲ့ တိုင်းတာမှုအတွက် double ပါ။ 82.75 လို value ကို binary နဲ့ အတိအကျ သိမ်းနိုင်ပေမယ့် 0.1 လို value တွေကိုတော့ အတိအကျ မသိမ်းနိုင်ပါဘူး။

  3. Line 9char section{'B'};

    Single quote ဆိုတာ character တစ်လုံးကို ဆိုလိုပြီး ၁ byte ပဲ ယူပါတယ်။ Double quote နဲ့ ရေးရင်တော့ char မဟုတ်တော့ဘဲ string literal ဖြစ်သွားပါမယ်။

  4. Line 10bool passed{true};

    bool က true ဒါမှမဟုတ် false ပဲ ကိုင်ပါတယ်။ std::format က bool ကို true လို့ စာလုံးနဲ့ ရေးပေးပြီး std::cout တစ်ခုတည်းနဲ့ဆိုရင် boolalpha မပါလို့ 1 ထွက်ပါလိမ့်မယ်။

  5. Line 12std::int64_t downloads{3'000'000'000};

    int64_t က အတိအကျ ၆၄ bit ရှိပြီး ၃ ဘီလီယံကို ဆံ့ပါတယ်။ ' သင်္ကေတတွေက ဂဏန်းရှည်ကို ဖတ်ရလွယ်အောင် ခွဲတာဖြစ်ပြီး value ကို မပြောင်းပါဘူး။

  6. Line 15sizeof(students)

    variable ကို ပေးတဲ့အခါ အဲဒီ variable ရဲ့ type အရွယ်အစားကို byte နဲ့ ပြန်ပေးပါတယ်။ ဒီ platform မှာ int က 4၊ double က 8၊ char နဲ့ bool က 1 ဖြစ်ပါတယ်။

You should see
C++ Foundation
Students: 35 (4 bytes)
Average: 82.75 (8 bytes)
Section: B (1 byte)
Passed: true (1 byte)
Downloads: 3000000000 (8 bytes)

အခြေခံ type တွေကို တစ်ခုချင်း ကြည့်မယ်

Typeပုံမှန် အရွယ်ဘာအတွက် သုံးလဲ
int4 byteကိန်းပြည့်: အရေအတွက်၊ index၊ အသက်
std::int64_tအတိအကျ 8 byteကြီးတဲ့ အရေအတွက်၊ timestamp၊ file အရွယ်
std::uint8_tအတိအကျ 1 byteRaw byte နဲ့ 0 ကနေ 255 အထိ flag
double8 byteတိုင်းတာမှု၊ ပျမ်းမျှ၊ ဒသမကိန်း တွက်ချက်မှု
float4 byteGraphics နဲ့ memory ချွေတာရမယ့် array ကြီးတွေ
char1 bytecharacter တစ်လုံး
bool1 byteဟုတ်/မဟုတ် ဆုံးဖြတ်ချက်
std::stringပြောင်းလဲ (library type)ရှည်တိုမတူတဲ့ text

ဒီစက်မှာ ကိုယ်တိုင် တိုင်းကြည့်လို့ရပါတယ်။ အောက်က program က int နဲ့ int64_t ရဲ့ အကြီးဆုံး value၊ uint8_t ရဲ့ အကြီးဆုံး value နဲ့ float, double က ဒသမ ဘယ်နှလုံး လုံးဝ မပျက်မယ့် ဆိုတာကို print ထုတ်ပါတယ်။ uint8_t ရှေ့က + ကိုလည်း သတိထားပါ၊ ဘာကြောင့် လိုလဲ ဆိုတာ Common mistakes မှာ ပါပါတယ်။

limits.cpp
#include <cstdint>
#include <iostream>
#include <limits>

int main() {
    std::cout << "int max:       " << std::numeric_limits<int>::max() << '\n';
    std::cout << "int64 max:     " << std::numeric_limits<std::int64_t>::max() << '\n';
    std::cout << "uint8 max:     " << +std::numeric_limits<std::uint8_t>::max() << '\n';
    std::cout << "double digits: " << std::numeric_limits<double>::digits10 << '\n';
    std::cout << "float digits:  " << std::numeric_limits<float>::digits10 << '\n';
    return 0;
}
output
int max:       2147483647
int64 max:     9223372036854775807
uint8 max:     255
double digits: 15
float digits:  6

Conversion တွေ: ကျယ်စေတာ၊ ဖြတ်တာ၊ static_cast

C++ က type တွေကြား အလိုအလျောက် ပြောင်းပေးတာကို implicit conversion လို့ ခေါ်ပါတယ်။ char ကနေ int၊ int ကနေ double လို value မပျက်တဲ့ အကျယ်ချဲ့ ပြောင်းလဲမှုတွေက ဘေးကင်းပါတယ်။ double ကနေ int ကတော့ ဒသမကို သုညဘက်ကို ဖြတ်လို့ -2.7 က -2 ဖြစ်ပြီး 2.99 က 2 ပါ၊ အဝိုင်းမချပါဘူး။ ပြောင်းတာကို ကိုယ်တမင်လုပ်ကြောင်း code ထဲမှာ ပေါ်စေချင်ရင် static_cast<Target>(value) ကို သုံးပါ။

အောက်က program မှာ wrong က int / int ကို အရင်တွက်ပြီးမှ double ဖြစ်လို့ 84 ပဲ ရတာကို မြင်ရပါမယ်။ right ကတော့ cast ကို အရင်လုပ်ပြီးမှ စားတာမို့ 84.67 ရပါတယ်။ wrong ကို = နဲ့ ရေးထားတာ ဆန်းတယ်လို့ မထင်ပါနဲ့ — {} နဲ့ရေးရင် GCC က int variable ကနေ double ကို narrowing အဖြစ် report လုပ်လို့ ဒီနေရာမှာ ရည်ရွယ်ချက်ရှိရှိ = ကို သုံးထားတာပါ။

conversions.cpp
#include <format>
#include <iostream>

int main() {
    int total{254};
    int count{3};

    double wrong = total / count;                      // int division first, then widened
    double right{static_cast<double>(total) / count};  // convert first, then divide
    std::cout << std::format("{} {:.2f}\n", wrong, right);

    double temperature{-2.7};
    int whole{static_cast<int>(temperature)};          // truncates toward zero
    std::cout << std::format("{} {}\n", whole, static_cast<int>(2.99));

    char letter{'A'};
    int code{letter};                                  // char widens to int safely
    char next{static_cast<char>(letter + 1)};          // letter + 1 is an int again
    std::cout << std::format("{} {} {}\n", letter, code, next);
    return 0;
}
output
84 84.67
-2 2
A 65 B

signed နဲ့ unsigned ကို ရောနှိုင်းတာ

int balance{-1}; unsigned int limit{1}; ဆိုရင် balance < limit က false ပါ၊ -1 ကို unsigned ပြောင်းတဲ့အခါ 4294967295 ဖြစ်သွားလို့ပါ။ GCC က -Wall နဲ့ "comparison of integer expressions of different signedness: 'int' and 'unsigned int' [-Wsign-compare]" လို့ သတိပေးပါတယ်။ အဲဒီ warning ကို လျစ်လျူမရှုပါနဲ့။

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

Project အစစ်တွေမှာ type ရွေးချယ်ပုံ

ငွေကြေးက ထင်ရှားတဲ့ ဥပမာပါ။ ဈေးနှုန်းတွေကို ကိုင်တဲ့ program တွေက အသေးဆုံး ယူနစ်ရဲ့ ကိန်းပြည့်အဖြစ် — ဒေါ်လာဆိုရင် cent၊ အပိုင်းကိန်း မလိုရင် ကျပ်အပြည့် — std::int64_t ထဲ သိမ်းကြပါတယ်၊ double မှာ 0.1 လို value ကို အတိအကျ မသိမ်းနိုင်လို့ပါ။ 0.1 ကို ဆယ်ခါ ပေါင်းရင် 0.9999999999999999 ရပါတယ်၊ 1 မရပါဘူး။ ပြသတဲ့အခါမှ ကိန်းပြည့်ကို format လုပ်ပါတယ် — 1999 cent ကို 19.99 လို့ ပြတာပါ။

စနစ်တွေ ဆုံတဲ့နေရာမှာ အကျယ်က အရေးကြီးပါတယ်။ Network protocol တွေနဲ့ file format တွေက field တစ်ခုစီရဲ့ အရွယ်ကို တိတိကျကျ သတ်မှတ်လို့ ဖတ်တဲ့ code က std::uint8_t, std::uint16_t, std::uint32_t ကို သုံးပါတယ်။ Signed 32-bit integer နဲ့ သိမ်းတဲ့ Unix timestamp တွေက ၂၀၃၈ ဇန်နဝါရီမှာ overflow ဖြစ်တာကြောင့် ခေတ်မီစနစ်တွေက 64-bit အချိန်တန်ဖိုးတွေ သုံးကြပါတယ်။ Game နဲ့ graphics code တွေက coordinate နဲ့ အရောင်အတွက် float ကို သုံးကြပါတယ်၊ GPU က သန်းနဲ့ချီ ကိုင်ရလို့ memory တစ်ဝက်လျော့တာက အရေးကြီးလို့ပါ။ သိပ္ပံနည်းကျ code တွေကတော့ တိကျမှု ပိုရဖို့ double ကို default လုပ်ကြပါတယ်။

အမှားများ

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

1. ကိန်းပြည့်နှစ်ခု စားပြီး ဒသမ မျှော်လင့်တာ

int average = total / count; က ဒသမ ဖြတ်ပစ်ပြီး double result = total / count; ကလည်း မကယ်ပါဘူး — ညာဘက်က int / int အရင် တွက်ပြီးမှ double ပြောင်းလို့ပါ။ total 254 နဲ့ count 3 ဆိုရင် 84.67 မဟုတ်ဘဲ 84 ရပါတယ်။ static_cast<double>(total) / count လို့ စားမယ့် operand တစ်ခုကို ကြိုပြောင်းပါ။

2. double ကို == နဲ့ နှိုင်းယှဉ်တာ၊ ငွေကို double နဲ့ သိမ်းတာ

double sum{0.1 + 0.2}; (sum == 0.3) က false ပါ၊ sum က 0.30000000000000004 ဖြစ်နေလို့ပါ။ std::abs(sum - 0.3) < 1e-9 လို ခံနိုင်ရည်နဲ့ နှိုင်းယှဉ်ပါ (<cmath> လိုပါတယ်)။ ငွေအတွက်ဆိုရင် cent လို အသေးဆုံး ယူနစ်ကို ကိန်းပြည့်နဲ့ သိမ်းပါ။

3. Value က type ထက် ကြီးလာတာကို မသတိထားတာ

int views = 3'000'000'000; က -Wall -Wextra နဲ့ warning တစ်ခုမှ မပေးဘဲ 32 bit နဲ့ ဆံ့အောင် ပတ်လည်ပြီး -1294967296 ကို သိမ်းပါတယ်။ int ပေါင်းလဒ်က overflow ဖြစ်ရင် ပိုဆိုးပါတယ် — undefined behavior ဖြစ်လို့ build တစ်ခုမှာ ပတ်လည်တဲ့ ဂဏန်း ပြပေမယ့် optimization ပြောင်းရင် တခြားပုံစံ ဖြစ်နိုင်ပါတယ်။ ကြီးလာနိုင်တဲ့ value တွေအတွက် std::int64_t လို ပိုကျယ်တဲ့ type ကို ကြိုရွေးပါ။

4. 8-bit integer ကို print ထုတ်ရင် စာလုံးထွက်လာတာ

std::uint8_t age{65}; ကို std::cout << age နဲ့ print ထုတ်ရင် 65 မဟုတ်ဘဲ A ထွက်ပါတယ်၊ uint8_t က char မိသားစုဆီက type ဖြစ်လို့ပါ။ +age ဒါမှမဟုတ် static_cast<int>(age) နဲ့ ထုတ်ရင် 65 ထွက်ပြီး std::format("{}", age) ကလည်း 65 ကို ရေးပါတယ်။

Best Practices

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

  • ကိန်းပြည့်အတွက် int၊ ဒသမကိန်းအတွက် double ကို default လုပ်ပါ၊ တခြား type ကို ရွေးရင် အကြောင်းရင်း ရှိပါစေ။
  • Size အတိအကျ ဒါမှမဟုတ် value ကြီးတာ အရေးကြီးရင် std::int64_t, std::uint8_t လို fixed-width type တွေကို သုံးပါ။
  • Conversion တိုင်းမှာ static_cast ကို သုံးပြီး ဖြတ်တာ၊ ကျဉ်းတာတွေကို code ထဲမှာ မြင်ရအောင် လုပ်ပါ။
  • unsigned အရေအတွက်ကို အနုတ်ကိန်းတွက်ချက်မှုနဲ့ ရောမတွက်ပါနဲ့၊ -Wall -Wextra ရဲ့ sign-compare warning ကို bug တစ်ခုလို သဘောထားပါ။

Mini challenge

Mini challenge: အမှတ်စာရင်း report

အမှတ်သုံးခုကနေ စုစုပေါင်းနဲ့ ပျမ်းမျှကို int အလိုက်တစ်မျိုး၊ ဒသမအလိုက်တစ်မျိုး တွက်ပြီး ကွာခြားချက်ကို မြင်ရအောင် report ထုတ်ပါ။ ပြီးရင် download အရေအတွက်တစ်ခုကို မှန်တဲ့ type နဲ့ သိမ်းပါ။

  • အမှတ် 78, 91, 85 ကို int variable သုံးခုထဲ brace နဲ့ ထည့်ပြီး total ကို int အဖြစ် တွက်ရမယ်
  • total / 3 ကို int ထဲ တစ်ခါ၊ static_cast<double> သုံးပြီး double ထဲ တစ်ခါ သိမ်းရမယ်
  • 3'000'000'000 ကို std::int64_t ထဲ သိမ်းပြီး sizeof နဲ့ byte အရေအတွက်ကိုပါ print ထုတ်ရမယ်
  • std::format သုံးပြီး ပျမ်းမျှကို ဒသမ နှစ်နေရာနဲ့ print ထုတ်ရမယ်
marks_report.cpp
#include <cstdint>
#include <format>
#include <iostream>

int main() {
    // TODO: three int marks (78, 91, 85) and their total

    // TODO: wholeAverage = total / 3 in an int
    // TODO: realAverage = total / 3 in a double; cast one operand first

    // TODO: a std::int64_t downloads variable holding 3'000'000'000

    // TODO: print Total, Integer average, Real average ({:.2f}) and
    //       Downloads with sizeof(downloads)
    return 0;
}

ရလဒ်: Output ဒီလို ဖြစ်ရပါမယ်: Total: 254 Integer average: 84 Real average: 84.67 Downloads: 3000000000 (8 bytes)

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

စားမယ့် total ကို static_cast<double>(total) လို့ ကြိုပြောင်းရင် / 3 က ဒသမ စားခြင်း ဖြစ်သွားပါတယ်။ double ထဲ brace နဲ့ ထည့်တဲ့အခါ ညာဘက် expression က double ဖြစ်နေရပါမယ်။ std::format("{:.2f}", value) က ဒသမ နှစ်နေရာ ပြပါတယ်။

အဖြေ ကြည့်ရန်
marks_report.cpp
#include <cstdint>
#include <format>
#include <iostream>

int main() {
    int mark1{78};
    int mark2{91};
    int mark3{85};
    int total{mark1 + mark2 + mark3};

    int wholeAverage{total / 3};                      // int / int: fraction dropped
    double realAverage{static_cast<double>(total) / 3};
    std::int64_t downloads{3'000'000'000};

    std::cout << std::format("Total: {}\n", total);
    std::cout << std::format("Integer average: {}\n", wholeAverage);
    std::cout << std::format("Real average: {:.2f}\n", realAverage);
    std::cout << std::format("Downloads: {} ({} bytes)\n", downloads, sizeof(downloads));
    return 0;
}

Quick Quiz 1 / 3

int a{7}; int b{2}; std::cout << a / b << ' ' << static_cast<double>(a) / b; က ဘာ print ထုတ်မလဲ။

Quick Quiz 2 / 3

5,000,000,000 ကို ဆံ့ပြီး brace initialization နဲ့ compile ဖြစ်တဲ့ declaration က ဘယ်ဟာလဲ။

Quick Quiz 3 / 3

unsigned int items{0}; items = items - 1; std::cout << items; ရဲ့ output က ဘာလဲ (32-bit unsigned int)။

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

  • Type က byte အရေအတွက်၊ bit ကို ဖတ်ပုံနဲ့ operator တွေရဲ့ အပြုအမူကို ဆုံးဖြတ်ပါတယ်၊ sizeof နဲ့ std::numeric_limits ကို သုံးပြီး တိုင်းကြည့်နိုင်ပါတယ်။
  • int, double, char, bool, std::string ကို value အလိုက် ရွေးပါ၊ size အတိအကျ လိုရင် std::int64_t လို fixed-width type ကို သုံးပါ။
  • int / int က ဒသမ ဖြတ်ပစ်ပါတယ်၊ double တွေက 0.1 လို value ကို အတိအကျ မသိမ်းနိုင်လို့ == နဲ့ မနှိုင်းယှဉ်ပါနဲ့။
  • unsigned က ပတ်လည်ပြီး signed overflow က undefined behavior ဖြစ်ပါတယ်၊ conversion တိုင်းမှာ static_cast ကို သုံးပါ။

The Current ISO C++ Standard (C++23) — Standard C++ Foundation

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

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

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

Data Types | Thuta Learning