ข้อ ใด คือ ตัวแปร ประเภท char


�Ԫ� �����¹������������������ͧ�� (���� �32101) �дѺ �Ѹ���֡�һշ�� 5
����ͧ ��Դ�ͧ������ ��е���� �ӹǹ 10 ���
�� �س������������� �ҹ�Ӿѹ�� �ç���¹ ��᫿�ػ�����
����� ���͡ ���� ����ӵͺ���١��ͧ����ش
��ͷ�� 1)
��������誹Դ�ͧ����������ҫ�
   float
   string
   integer
   variable

��ͷ�� 2)
���㴨Ѵ�繢����Ż������ѡ�������ҫ�
   'c'
   0123
   11.25
   "c language"

��ͷ�� 3)
�����繢����Ż�������ͤ���
   'c++'
   $js$
   "joseph upatham"
   c language programing

��ͷ�� 4)
�ѡ���¹�Դ����Ţ㹢����������ö���繢����Ż����� char
   8
   55
   1
   2

��ͷ�� 5)
���㴼Դ����õ�駪��͵����
   name_5
   _name5
   5name
   _name_5

��ͷ�� 6)
�ҡ⨷�� ��û�С�ȵ���ê�Դ� ��˹���� num=3.5
   int
   float
   char
   string

��ͷ�� 7)
���㴤���ٻẺ��û�С�ȵ����
   variable-name size;
   size variable-name;
   variable-name type;
   type variable-name;

��ͷ�� 8)
��ͧ��û�С�ȵ���� ���� i �����纤�ṹ���Ţ�ӹǹ��� 50 ���㴶١��ͧ
   int i = 50;
   char i = '50';
   float i = "50";
   string i = "50";

��ͷ�� 9)
��С�ȵ���ê��� name ������纪�����蹢ͧ�ѡ���¹
   int name;
   float name;
   char name;
   string name;

��ͷ�� 10)
���㴵�駪��͵�������١��ͧ
   register
   happy
   hotmail
   number


ตัวแปรนั้นเป็นสิ่งที่สำคัญสำหรับในการเขียนโปรแกรมทุกภาษา มันถูกใช้เพื่อเก็บข้อมูลในหน่วยความจำและช่วยให้เราสามารถจัดการกับข้อมูลได้อย่างง่ายดาย

ยกตัวอย่างเช่น ถ้าคุณต้องการที่จะจดจำบางอย่าง คุณจะต้องเขียนมันลงไปบนสมุดบันทึกของคุณ เพราะนั่นจะทำให้คุณไม่ลืมเมื่อคุณบันทึกลงไปคุณสามารถนำมาใช้เมื่อไหร่ก็ตามที่คุณต้องการ ดังนั้นในการเขียนโปรแกรมคอมพิวเตอร์ ตัวแปรถูกนำมาใช้ในแนวคิดเดียวกัน

int a = 2;
int b = 2;

int sum = a + b;

ในตัวอย่าง เราเก็บค่า

int n;
float money;
bool t;
3 ในตัวแปร
int n;
float money;
bool t;
4 และ
int n;
float money;
bool t;
5 เพื่อหาผลรวมของตัวแปร
int n;
float money;
bool t;
4 และตัวแปร
int n;
float money;
bool t;
5 เราจำเป็นต้องจดจำ
int n;
float money;
bool t;
3 ไว้ใสตัวแปร
int n;
float money;
bool t;
4 ก่อนจนกว่าเราจะมีค่าของ
int n;
float money;
bool t;
5 และหลังจากนั้นเราได้ทำการรวมค่าของตัวแปรทั้งสองไว้ในอีกตัวแปรคือ
#include <iostream>

using namespace std;

int main()
{
    bool gender = false;
    if (gender)
    {
        cout << "This is a boy." << endl;
    }
    else
    {
        cout << "This is a girl." << endl;
    }

    bool is_playing = true;
    cout << "Playing = " << is_playing << endl;
    cout << "Playing invert = " << !is_playing << endl;

    cout << "1 and True = " << (1 && true) << endl;
    cout << "True and True = " << (true && true) << endl;
    cout << "True and False = " << (true && false) << endl;
    cout << "True or True = " << (true || true) << endl;
    cout << "True or False = " << (true || false) << endl;

    return 0;
}
1

คุณจะเห็นคำว่า

#include <iostream>

using namespace std;

int main()
{
    bool gender = false;
    if (gender)
    {
        cout << "This is a boy." << endl;
    }
    else
    {
        cout << "This is a girl." << endl;
    }

    bool is_playing = true;
    cout << "Playing = " << is_playing << endl;
    cout << "Playing invert = " << !is_playing << endl;

    cout << "1 and True = " << (1 && true) << endl;
    cout << "True and True = " << (true && true) << endl;
    cout << "True and False = " << (true && false) << endl;
    cout << "True or True = " << (true || true) << endl;
    cout << "True or False = " << (true || false) << endl;

    return 0;
}
2 ก่อนชื่อของตัวแปรซึ่ง นั่นเราเรียกว่าคำสั่งที่ใช้กำหนดประเภทของตัวแปร หรือการประกาศประเภทของตัวแปร ถ้าคุณยังไม่เข้าใจในตอนนี้ ไม่ต้องกังวล เราจะเรียนในต่อไปของบทเรียนนี้

ประเภทข้อมูล

ในภาษา C++ มีตัวแปรหลายประเภทที่ช่วยให้เราสามารถจัดการกับข้อมูลประเภทต่างๆ เช่น ฺBoolean, Number, Character, String และ Object เป็นต้น

ในภาษา C++ จะมีกลุ่มของประเภทข้อมูลอยู่ทั้งหมด 4 กลุ่ม ที่คุณสามารถนำไปประกาศตัวแปรได้

  • Character types: นี่เป็นประเภทของข้อมูลที่ใช้เก็บตัวอักษรหนึ่งตัว เช่น 'a', 'b' หรือ '8'.
  • Numerical integer types: ประเภทของข้อมูลชนิดนี้ใช้สำหรับเก็บค่าของจำนวนธรรมชาติ เช่น 1 หรือ 1000 ซึ่งจะแบ่งย่อยไปตามขนาดที่ใช้เก็บ โดยปกตินั้นจะเป็นแบบ int และ long
  • Floating-point types: ประเภทของข้อมูลชนิดนี้ใช้เพื่อเก็บจำนวนจริง เช่น 1.8 100.05 หรือ -5.5
  • Boolean type: ประเภทของข้อมูลชนิดนี้สามารถเก็บค่าได้เพียงสองค่าคือ true และ false

ในตารางข้างล่างนี้ แสดงประเภทของตัวแปรทั้งหมดในภาษา C++

TypeSizeValueschar1 byte-128 to 127 (Char)char16_t2 bytes-32,768 to 32,767 (Char)char32_t4 bytes-2,147,483,648 to 2,147,483,647 (Char)wchar_tMultibytesshort int2 bytes-32,768 to 32,767int2 bytes-32,768 to 32,762long int4 bytes-2,147,483,648 to 2,147,483,647long long int8 bytes-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807unsigned char1 byte0 to 255unsigned short int2 bytes0 to 65,535unsigned int2 byteso to 65,535unsigned long int4 bytes0 to 4,294,967,295unsigned long long int8 bytes0 to 18,446,744,073,709,551,615float4 bytes1.2E-38 to 3.4E+38double8 bytes2.3E-308 to 1.7E+308bool1 byte0 to 1void--decltype(nullptr)--

การประกาศตัวแปร

ภาษา C++ เป็นภาษาที่มีรูปแบบเข้มงวดในการเขียน ตัวแปรจำเป็นต้องประกาศก่อนที่จะมีการใช้งาน นั่นหมายความว่าคอมไพเลอร์จะมีการจองหน่วยความจำที่เพียงพอสำหรับตัวแปร รูปแบบในการประกาศตัวแปรในภาษา C++ นั้นตรงไปตรงมา มาดูตัวอย่างสำหรับการประกาศตัวแปรในภาษา C++

int n;
float money;
bool t;

ในตัวอย่างเราได้ประกาศ 3 ตัวแปร ตัวแปรแรกเราประกาศตัวแปรที่มีชนิดข้อมูลเป็น int และมีชื่อของตัวแปรว่า

int n;
float money;
bool t;
4 ตัวแปรที่สองประกาศตัวแปรที่มีชนิดข้อมูลเป็น
#include <iostream>

using namespace std;

int main()
{
    bool gender = false;
    if (gender)
    {
        cout << "This is a boy." << endl;
    }
    else
    {
        cout << "This is a girl." << endl;
    }

    bool is_playing = true;
    cout << "Playing = " << is_playing << endl;
    cout << "Playing invert = " << !is_playing << endl;

    cout << "1 and True = " << (1 && true) << endl;
    cout << "True and True = " << (true && true) << endl;
    cout << "True and False = " << (true && false) << endl;
    cout << "True or True = " << (true || true) << endl;
    cout << "True or False = " << (true || false) << endl;

    return 0;
}
4 และมีชื่อของตัวแปรว่า
#include <iostream>

using namespace std;

int main()
{
    bool gender = false;
    if (gender)
    {
        cout << "This is a boy." << endl;
    }
    else
    {
        cout << "This is a girl." << endl;
    }

    bool is_playing = true;
    cout << "Playing = " << is_playing << endl;
    cout << "Playing invert = " << !is_playing << endl;

    cout << "1 and True = " << (1 && true) << endl;
    cout << "True and True = " << (true && true) << endl;
    cout << "True and False = " << (true && false) << endl;
    cout << "True or True = " << (true || true) << endl;
    cout << "True or False = " << (true || false) << endl;

    return 0;
}
5 ตัวแปรที่สามนั้นเป็นประเภท boolean ที่มีชื่อว่า
#include <iostream>

using namespace std;

int main()
{
    bool gender = false;
    if (gender)
    {
        cout << "This is a boy." << endl;
    }
    else
    {
        cout << "This is a girl." << endl;
    }

    bool is_playing = true;
    cout << "Playing = " << is_playing << endl;
    cout << "Playing invert = " << !is_playing << endl;

    cout << "1 and True = " << (1 && true) << endl;
    cout << "True and True = " << (true && true) << endl;
    cout << "True and False = " << (true && false) << endl;
    cout << "True or True = " << (true || true) << endl;
    cout << "True or False = " << (true || false) << endl;

    return 0;
}
6 เมื่อตัวแปรถูกสร้าง มันจะสามารถถูกใช้ได้ในขอบเขตของโปรแกรม หลังจากที่เราได้ประกาศตัวแปรแล้ว เราสามารถกำหนดค่าให้มันในตอนเริ่มต้น หรือในตอนที่โปรแกรมรันได้ทันที

Boolean

Boolean เป็นประเภทข้อมูลที่เก็บค่าเพียงสองค่าคือ

#include <iostream>

using namespace std;

int main()
{
    bool gender = false;
    if (gender)
    {
        cout << "This is a boy." << endl;
    }
    else
    {
        cout << "This is a girl." << endl;
    }

    bool is_playing = true;
    cout << "Playing = " << is_playing << endl;
    cout << "Playing invert = " << !is_playing << endl;

    cout << "1 and True = " << (1 && true) << endl;
    cout << "True and True = " << (true && true) << endl;
    cout << "True and False = " << (true && false) << endl;
    cout << "True or True = " << (true || true) << endl;
    cout << "True or False = " << (true || false) << endl;

    return 0;
}
7 และ
#include <iostream>

using namespace std;

int main()
{
    bool gender = false;
    if (gender)
    {
        cout << "This is a boy." << endl;
    }
    else
    {
        cout << "This is a girl." << endl;
    }

    bool is_playing = true;
    cout << "Playing = " << is_playing << endl;
    cout << "Playing invert = " << !is_playing << endl;

    cout << "1 and True = " << (1 && true) << endl;
    cout << "True and True = " << (true && true) << endl;
    cout << "True and False = " << (true && false) << endl;
    cout << "True or True = " << (true || true) << endl;
    cout << "True or False = " << (true || false) << endl;

    return 0;
}
8 และเป็นประเภทข้อมูลที่ใช้หน่วยความจำในการเก็บน้อยที่สุด Boolean มักจะใช้ในการเก็บข้อมูลที่มีเพียงสองสถานะ เช่น เปิดหรือปิด ใช้งานหรือไม่ใช้งาน ออนไลน์หรือออฟไลน์ เป็นต้น Boolean ใช้สำหรับคำสั่งเงื่อนไข เช่น คำสั่ง
#include <iostream>

using namespace std;

int main()
{
    bool gender = false;
    if (gender)
    {
        cout << "This is a boy." << endl;
    }
    else
    {
        cout << "This is a girl." << endl;
    }

    bool is_playing = true;
    cout << "Playing = " << is_playing << endl;
    cout << "Playing invert = " << !is_playing << endl;

    cout << "1 and True = " << (1 && true) << endl;
    cout << "True and True = " << (true && true) << endl;
    cout << "True and False = " << (true && false) << endl;
    cout << "True or True = " << (true || true) << endl;
    cout << "True or False = " << (true || false) << endl;

    return 0;
}
9,
This is a girl.
Playing = 1
Playing invert = 0
1 and True = 1
True and True = 1
True and False = 0
True or True = 1
True or False = 1
0 หรือ
This is a girl.
Playing = 1
Playing invert = 0
1 and True = 1
True and True = 1
True and False = 0
True or True = 1
True or False = 1
1 และเราสามารถสร้างสร้าง Boolean expression ที่ซับซ้อนได้โดยการใช้ตัวดำเนินการกับ Boolean นี่เป็นตัวอย่างการใช้งาน Boolean ในภาษา C++

#include <iostream>

using namespace std;

int main()
{
    bool gender = false;
    if (gender)
    {
        cout << "This is a boy." << endl;
    }
    else
    {
        cout << "This is a girl." << endl;
    }

    bool is_playing = true;
    cout << "Playing = " << is_playing << endl;
    cout << "Playing invert = " << !is_playing << endl;

    cout << "1 and True = " << (1 && true) << endl;
    cout << "True and True = " << (true && true) << endl;
    cout << "True and False = " << (true && false) << endl;
    cout << "True or True = " << (true || true) << endl;
    cout << "True or False = " << (true || false) << endl;

    return 0;
}

ในตัวอย่าง เป็นการประกาศตัวแปร Boolean นั้นจะใช้คำสั่ง

This is a girl.
Playing = 1
Playing invert = 0
1 and True = 1
True and True = 1
True and False = 0
True or True = 1
True or False = 1
2 และตามด้วยชื่อตัวแปรที่ต้องการ ในตอนแรกเราได้ประกาศตัวแปร
This is a girl.
Playing = 1
Playing invert = 0
1 and True = 1
True and True = 1
True and False = 0
True or True = 1
True or False = 1
3 ซึ่งเราได้ให้ความหมายของตัวแปรนี้ว่า ถ้าหากมีค่าเป็น
#include <iostream>

using namespace std;

int main()
{
    bool gender = false;
    if (gender)
    {
        cout << "This is a boy." << endl;
    }
    else
    {
        cout << "This is a girl." << endl;
    }

    bool is_playing = true;
    cout << "Playing = " << is_playing << endl;
    cout << "Playing invert = " << !is_playing << endl;

    cout << "1 and True = " << (1 && true) << endl;
    cout << "True and True = " << (true && true) << endl;
    cout << "True and False = " << (true && false) << endl;
    cout << "True or True = " << (true || true) << endl;
    cout << "True or False = " << (true || false) << endl;

    return 0;
}
7 นั้นจะเป็นผู้ชาย และหากไม่ใช่คือมีค่าเป็น
#include <iostream>

using namespace std;

int main()
{
    bool gender = false;
    if (gender)
    {
        cout << "This is a boy." << endl;
    }
    else
    {
        cout << "This is a girl." << endl;
    }

    bool is_playing = true;
    cout << "Playing = " << is_playing << endl;
    cout << "Playing invert = " << !is_playing << endl;

    cout << "1 and True = " << (1 && true) << endl;
    cout << "True and True = " << (true && true) << endl;
    cout << "True and False = " << (true && false) << endl;
    cout << "True or True = " << (true || true) << endl;
    cout << "True or False = " << (true || false) << endl;

    return 0;
}
8 จะเป็นผู้หญิง และเราใช้คำสั่ง
#include <iostream>

using namespace std;

int main()
{
    bool gender = false;
    if (gender)
    {
        cout << "This is a boy." << endl;
    }
    else
    {
        cout << "This is a girl." << endl;
    }

    bool is_playing = true;
    cout << "Playing = " << is_playing << endl;
    cout << "Playing invert = " << !is_playing << endl;

    cout << "1 and True = " << (1 && true) << endl;
    cout << "True and True = " << (true && true) << endl;
    cout << "True and False = " << (true && false) << endl;
    cout << "True or True = " << (true || true) << endl;
    cout << "True or False = " << (true || false) << endl;

    return 0;
}
9 ในการตรวจสอบค่าของตัวแปร โดยคำสั่ง
#include <iostream>

using namespace std;

int main()
{
    bool gender = false;
    if (gender)
    {
        cout << "This is a boy." << endl;
    }
    else
    {
        cout << "This is a girl." << endl;
    }

    bool is_playing = true;
    cout << "Playing = " << is_playing << endl;
    cout << "Playing invert = " << !is_playing << endl;

    cout << "1 and True = " << (1 && true) << endl;
    cout << "True and True = " << (true && true) << endl;
    cout << "True and False = " << (true && false) << endl;
    cout << "True or True = " << (true || true) << endl;
    cout << "True or False = " << (true || false) << endl;

    return 0;
}
9 นั้นจะใช้สำหรับการตรวจสอบ Boolean expression ที่คุณจะได้เรียนในบทต่อๆ ไป

ต่อมาเราประกาศตัวแปร

This is a girl.
Playing = 1
Playing invert = 0
1 and True = 1
True and True = 1
True and False = 0
True or True = 1
True or False = 1
8 สำหรับการเก็บสถานะของการเล่นและกำหนดค่าให้กับตัวแปรเป็น
#include <iostream>

using namespace std;

int main()
{
    bool gender = false;
    if (gender)
    {
        cout << "This is a boy." << endl;
    }
    else
    {
        cout << "This is a girl." << endl;
    }

    bool is_playing = true;
    cout << "Playing = " << is_playing << endl;
    cout << "Playing invert = " << !is_playing << endl;

    cout << "1 and True = " << (1 && true) << endl;
    cout << "True and True = " << (true && true) << endl;
    cout << "True and False = " << (true && false) << endl;
    cout << "True or True = " << (true || true) << endl;
    cout << "True or False = " << (true || false) << endl;

    return 0;
}
7 ในภาษา C++ นั้นค่าของ
#include <iostream>

using namespace std;

int main()
{
    bool gender = false;
    if (gender)
    {
        cout << "This is a boy." << endl;
    }
    else
    {
        cout << "This is a girl." << endl;
    }

    bool is_playing = true;
    cout << "Playing = " << is_playing << endl;
    cout << "Playing invert = " << !is_playing << endl;

    cout << "1 and True = " << (1 && true) << endl;
    cout << "True and True = " << (true && true) << endl;
    cout << "True and False = " << (true && false) << endl;
    cout << "True or True = " << (true || true) << endl;
    cout << "True or False = " << (true || false) << endl;

    return 0;
}
7 จะแสดงเป็น
#include <iostream>

using namespace std;

int main()
{
    char ch = 'A';
    char ch2 = '1';
    char ch3 = ' ';
    char ch4 = 97;
    char ch5 = 97 + 10;

    cout << ch << " = " << (int) ch << endl;
    cout << ch2 << " = " << (int) ch2 << endl;
    cout << ch3 << " = " << (int) ch3 << endl;
    cout << ch4 << " = " << (int) ch4 << endl;
    cout << ch5 << " = " << (int) ch5 << endl;

    cout << "a < b = " << ('a' < 'b') << endl;

    return 0;
}
1 และค่าของ
#include <iostream>

using namespace std;

int main()
{
    bool gender = false;
    if (gender)
    {
        cout << "This is a boy." << endl;
    }
    else
    {
        cout << "This is a girl." << endl;
    }

    bool is_playing = true;
    cout << "Playing = " << is_playing << endl;
    cout << "Playing invert = " << !is_playing << endl;

    cout << "1 and True = " << (1 && true) << endl;
    cout << "True and True = " << (true && true) << endl;
    cout << "True and False = " << (true && false) << endl;
    cout << "True or True = " << (true || true) << endl;
    cout << "True or False = " << (true || false) << endl;

    return 0;
}
8 จะแสดงเป็น
#include <iostream>

using namespace std;

int main()
{
    char ch = 'A';
    char ch2 = '1';
    char ch3 = ' ';
    char ch4 = 97;
    char ch5 = 97 + 10;

    cout << ch << " = " << (int) ch << endl;
    cout << ch2 << " = " << (int) ch2 << endl;
    cout << ch3 << " = " << (int) ch3 << endl;
    cout << ch4 << " = " << (int) ch4 << endl;
    cout << ch5 << " = " << (int) ch5 << endl;

    cout << "a < b = " << ('a' < 'b') << endl;

    return 0;
}
3 นั่นหมายความว่าคุณสามารถใช้งานค่าเหล่านี้ทดแทนกันได้ในการเขียนโปรแกรม

ต่อมาเป็นการทดสอบค่าของ boolean ด้วยตัวดำเนินการในภาษา C++ ซึ่งถ้าหากผลลัพธ์เป็นจริงจะได้ค่าที่แสดงออกมาเป็น

#include <iostream>

using namespace std;

int main()
{
    char ch = 'A';
    char ch2 = '1';
    char ch3 = ' ';
    char ch4 = 97;
    char ch5 = 97 + 10;

    cout << ch << " = " << (int) ch << endl;
    cout << ch2 << " = " << (int) ch2 << endl;
    cout << ch3 << " = " << (int) ch3 << endl;
    cout << ch4 << " = " << (int) ch4 << endl;
    cout << ch5 << " = " << (int) ch5 << endl;

    cout << "a < b = " << ('a' < 'b') << endl;

    return 0;
}
1 และถ้าไม่เป็นจริงจะได้ค่าที่แสดงออกมาเป็น
#include <iostream>

using namespace std;

int main()
{
    char ch = 'A';
    char ch2 = '1';
    char ch3 = ' ';
    char ch4 = 97;
    char ch5 = 97 + 10;

    cout << ch << " = " << (int) ch << endl;
    cout << ch2 << " = " << (int) ch2 << endl;
    cout << ch3 << " = " << (int) ch3 << endl;
    cout << ch4 << " = " << (int) ch4 << endl;
    cout << ch5 << " = " << (int) ch5 << endl;

    cout << "a < b = " << ('a' < 'b') << endl;

    return 0;
}
3

This is a girl.
Playing = 1
Playing invert = 0
1 and True = 1
True and True = 1
True and False = 0
True or True = 1
True or False = 1

นี่เป็นผลลัพธ์การทำงานของโปรแกรมในการประกาศและใช้งานข้อมูลประเภท boolean ในภาษา C++

Char

Char คือประเภทข้อมูลที่เก็บและแสดงข้อมูลในรูปแบบตัวอักษร ASCII ซึ่งมีจำนวนทั้งสิ้น 256 ตัวในภาษา C++ ค่าของ char นั้นสามารถเป็นได้ตั้งแต่ 0 - 255 ใน integer และในการแสดงผลจะแสดงเป็นรหัสของตัวอักษร ซึ่งตัวอักษรทุกตัวมีรหัส ASCII มัน char นั้นมีหลายขนาด เช่น

#include <iostream>

using namespace std;

int main()
{
    char ch = 'A';
    char ch2 = '1';
    char ch3 = ' ';
    char ch4 = 97;
    char ch5 = 97 + 10;

    cout << ch << " = " << (int) ch << endl;
    cout << ch2 << " = " << (int) ch2 << endl;
    cout << ch3 << " = " << (int) ch3 << endl;
    cout << ch4 << " = " << (int) ch4 << endl;
    cout << ch5 << " = " << (int) ch5 << endl;

    cout << "a < b = " << ('a' < 'b') << endl;

    return 0;
}
6,
#include <iostream>

using namespace std;

int main()
{
    char ch = 'A';
    char ch2 = '1';
    char ch3 = ' ';
    char ch4 = 97;
    char ch5 = 97 + 10;

    cout << ch << " = " << (int) ch << endl;
    cout << ch2 << " = " << (int) ch2 << endl;
    cout << ch3 << " = " << (int) ch3 << endl;
    cout << ch4 << " = " << (int) ch4 << endl;
    cout << ch5 << " = " << (int) ch5 << endl;

    cout << "a < b = " << ('a' < 'b') << endl;

    return 0;
}
7 เป็นประเภทของ
#include <iostream>

using namespace std;

int main()
{
    char ch = 'A';
    char ch2 = '1';
    char ch3 = ' ';
    char ch4 = 97;
    char ch5 = 97 + 10;

    cout << ch << " = " << (int) ch << endl;
    cout << ch2 << " = " << (int) ch2 << endl;
    cout << ch3 << " = " << (int) ch3 << endl;
    cout << ch4 << " = " << (int) ch4 << endl;
    cout << ch5 << " = " << (int) ch5 << endl;

    cout << "a < b = " << ('a' < 'b') << endl;

    return 0;
}
8 ที่เก็บข้อมูลได้มากกว่า แต่โดยปกติแล้วเรามักจะใช้
#include <iostream>

using namespace std;

int main()
{
    char ch = 'A';
    char ch2 = '1';
    char ch3 = ' ';
    char ch4 = 97;
    char ch5 = 97 + 10;

    cout << ch << " = " << (int) ch << endl;
    cout << ch2 << " = " << (int) ch2 << endl;
    cout << ch3 << " = " << (int) ch3 << endl;
    cout << ch4 << " = " << (int) ch4 << endl;
    cout << ch5 << " = " << (int) ch5 << endl;

    cout << "a < b = " << ('a' < 'b') << endl;

    return 0;
}
8 ในการเขียนโปรแกรม นี่เป็นตัวอย่างการใช้งาน Char ในภาษา C++

#include <iostream>

using namespace std;

int main()
{
    char ch = 'A';
    char ch2 = '1';
    char ch3 = ' ';
    char ch4 = 97;
    char ch5 = 97 + 10;

    cout << ch << " = " << (int) ch << endl;
    cout << ch2 << " = " << (int) ch2 << endl;
    cout << ch3 << " = " << (int) ch3 << endl;
    cout << ch4 << " = " << (int) ch4 << endl;
    cout << ch5 << " = " << (int) ch5 << endl;

    cout << "a < b = " << ('a' < 'b') << endl;

    return 0;
}

ในตัวอย่าง เป็นการใช้งานตัวแปรประเภท

#include <iostream>

using namespace std;

int main()
{
    char ch = 'A';
    char ch2 = '1';
    char ch3 = ' ';
    char ch4 = 97;
    char ch5 = 97 + 10;

    cout << ch << " = " << (int) ch << endl;
    cout << ch2 << " = " << (int) ch2 << endl;
    cout << ch3 << " = " << (int) ch3 << endl;
    cout << ch4 << " = " << (int) ch4 << endl;
    cout << ch5 << " = " << (int) ch5 << endl;

    cout << "a < b = " << ('a' < 'b') << endl;

    return 0;
}
8 ซึ่งเราได้ประกาศค่าให้กับตัวแปรทั้งหมด โดยค่าของ
#include <iostream>

using namespace std;

int main()
{
    char ch = 'A';
    char ch2 = '1';
    char ch3 = ' ';
    char ch4 = 97;
    char ch5 = 97 + 10;

    cout << ch << " = " << (int) ch << endl;
    cout << ch2 << " = " << (int) ch2 << endl;
    cout << ch3 << " = " << (int) ch3 << endl;
    cout << ch4 << " = " << (int) ch4 << endl;
    cout << ch5 << " = " << (int) ch5 << endl;

    cout << "a < b = " << ('a' < 'b') << endl;

    return 0;
}
8 นั้นเป็นตัวอักษรในใดๆ ใน ASCII และจะต้องมีเครื่องหมาย single quote (
A = 65
1 = 49
  = 32
a = 97
k = 107
a < b = 1
2) ล้อมรอบอยู่ อย่างที่เราได้บอกว่า Char นั้นจะมีรหัสใน Integer ดังนั้นในสองตัวแปรสุดท้ายเราจึงกำหนดค่าแบบ Integer ให้กับตัวแปร

หลังจากนั้นเราได้แสดงผลค่าของตัวแปรทั้ง 5 ตัวและค่า Integer ASCII ของมันโดยการใช้วิธี Type casting จากข้อมูลประเภท Char เป็น Integer และในคำสั่งสุดท้ายเป็นการใช้ตัวดำเนินการเปรียบเทียบค่าของ Char

A = 65
1 = 49
  = 32
a = 97
k = 107
a < b = 1

นี่เป็นผลลัพธ์การทำงานของโปรแกรมในการใช้ประกาศและใช้งานข้อมูลประเภท char ในภาษา C++

Integer

Integer คือประเภทข้อมูลแบบจำนวนเต็ม มันมักจะถูกใช้สำหรับเก็บข้อมูลที่ไม่มีทศนิยม เช่น จำนวนของผลไม้ คะแนนของผู้เล่นเกม จำนวนลูกกระสุนถูกยิงออกไป เป็นต้น ในภาษา C++ นั้นข้อมูลประเภท Integer มีหลายประเภท เช่น

A = 65
1 = 49
  = 32
a = 97
k = 107
a < b = 1
3
#include <iostream>

using namespace std;

int main()
{
    bool gender = false;
    if (gender)
    {
        cout << "This is a boy." << endl;
    }
    else
    {
        cout << "This is a girl." << endl;
    }

    bool is_playing = true;
    cout << "Playing = " << is_playing << endl;
    cout << "Playing invert = " << !is_playing << endl;

    cout << "1 and True = " << (1 && true) << endl;
    cout << "True and True = " << (true && true) << endl;
    cout << "True and False = " << (true && false) << endl;
    cout << "True or True = " << (true || true) << endl;
    cout << "True or False = " << (true || false) << endl;

    return 0;
}
2 หรือ ซึ่งมีขนาดของการเก็บที่แตกต่างกันออกไป และคำสั่ง
A = 65
1 = 49
  = 32
a = 97
k = 107
a < b = 1
5 ใช้สำหรับประกาศตัวแปรที่เก็บเพียงจำนวนเต็มบวก นี่เป็นตัวอย่างของการใช้งานข้อมูลประเภท Integer ในภาษา C++

#include <iostream>

using namespace std;

int main()
{
    // using integer
    int apple = 3;
    int orange = 5;
    int total = apple + orange;
    cout << "Total fruit = " << total << endl;

    // using short, smaller size
    short day, month, year;
    day = 9;
    month = 3;
    year = 2017;
    cout << "Date: " << month << "/" << day << "/" << year << endl;

    // using unsigned, store only positive values
    unsigned int heght = 8848;
    cout << "The Mount Everest has " << heght << " height." << endl;

    // overflow
    int max_int  = 2147483647;
    cout << max_int << endl;
    max_int++;
    cout << max_int << endl;

    return 0;
}

ในตัวอย่าง เป็นการใช้งานข้อมูลประเภท Integer ในสถานการณ์ต่างๆ ในตอนแรกเราประกาศตัวแปรประเภท

#include <iostream>

using namespace std;

int main()
{
    bool gender = false;
    if (gender)
    {
        cout << "This is a boy." << endl;
    }
    else
    {
        cout << "This is a girl." << endl;
    }

    bool is_playing = true;
    cout << "Playing = " << is_playing << endl;
    cout << "Playing invert = " << !is_playing << endl;

    cout << "1 and True = " << (1 && true) << endl;
    cout << "True and True = " << (true && true) << endl;
    cout << "True and False = " << (true && false) << endl;
    cout << "True or True = " << (true || true) << endl;
    cout << "True or False = " << (true || false) << endl;

    return 0;
}
2 สำหรับเก็บจำนวนผลไม้ และนำมาบวกกันแล้วเก็บไว้ในตัวแปร
A = 65
1 = 49
  = 32
a = 97
k = 107
a < b = 1
7 และแสดงผลออกทางหน้าจอ หลังจากนั้นเป็นการใช้ตัวแปรประเภท
A = 65
1 = 49
  = 32
a = 97
k = 107
a < b = 1
3 ซึ่งเก็บข้อมูลได้น้อยกว่า
#include <iostream>

using namespace std;

int main()
{
    bool gender = false;
    if (gender)
    {
        cout << "This is a boy." << endl;
    }
    else
    {
        cout << "This is a girl." << endl;
    }

    bool is_playing = true;
    cout << "Playing = " << is_playing << endl;
    cout << "Playing invert = " << !is_playing << endl;

    cout << "1 and True = " << (1 && true) << endl;
    cout << "True and True = " << (true && true) << endl;
    cout << "True and False = " << (true && false) << endl;
    cout << "True or True = " << (true || true) << endl;
    cout << "True or False = " << (true || false) << endl;

    return 0;
}
2 เนื่องจากค่าที่เราต้องการเก็บนั้นเป็นค่าของวันเดือนปี ที่มีค่าไม่มากกว่าค่าของ
A = 65
1 = 49
  = 32
a = 97
k = 107
a < b = 1
3 อยู่แล้ว มันจึงสำคัญที่คุณจะตระหนักถึงในการเลือกใช้ประเภทข้อมูลที่ประหยัดหน่วยความจำมากที่สุด

// using unsigned, store only positive values
unsigned int heght = 8848;
cout << "The Mount Everest has " << heght << " height." << endl;

// overflow
int max_int  = 2147483647;
cout << max_int << endl;
max_int++;
cout << max_int << endl;

ต่อมาเป็นการใช้งานคำสั่ง

A = 65
1 = 49
  = 32
a = 97
k = 107
a < b = 1
5 สำหรับเก็บตัวเลขค่าบวกเท่านั้น และเราได้ใช้เก็บความสูงของภูเขาเพราะว่าเราทราบว่าความสูงนั้นไม่มีทางเป็นลบแน่นอน และสุดท้ายเป็นการทดสอบการเกิด Overflow ของตัวแปร เราได้กำหนดค่าสูงสุดที่ข้อมูลประเภท
#include <iostream>

using namespace std;

int main()
{
    bool gender = false;
    if (gender)
    {
        cout << "This is a boy." << endl;
    }
    else
    {
        cout << "This is a girl." << endl;
    }

    bool is_playing = true;
    cout << "Playing = " << is_playing << endl;
    cout << "Playing invert = " << !is_playing << endl;

    cout << "1 and True = " << (1 && true) << endl;
    cout << "True and True = " << (true && true) << endl;
    cout << "True and False = " << (true && false) << endl;
    cout << "True or True = " << (true || true) << endl;
    cout << "True or False = " << (true || false) << endl;

    return 0;
}
2 จะเก็บได้ลงในตัวแปร
#include <iostream>

using namespace std;

int main()
{
    // using integer
    int apple = 3;
    int orange = 5;
    int total = apple + orange;
    cout << "Total fruit = " << total << endl;

    // using short, smaller size
    short day, month, year;
    day = 9;
    month = 3;
    year = 2017;
    cout << "Date: " << month << "/" << day << "/" << year << endl;

    // using unsigned, store only positive values
    unsigned int heght = 8848;
    cout << "The Mount Everest has " << heght << " height." << endl;

    // overflow
    int max_int  = 2147483647;
    cout << max_int << endl;
    max_int++;
    cout << max_int << endl;

    return 0;
}
3 และพยายามเพิ่มค่าตัวแปรไปอีก 1 ผลลัพธ์ที่ได้คือค่าในตัวแปรจะเกิดการ Overflow ไปเป็นค่าต่ำสุดของมัน ดังนั้นคุณควรจะระวังในการเขียนโปรแกรม ถ้าหากข้อมูลของคุณมีขนาดใหญ่มากๆ คุณควรจะเลือกใช้ประเภทข้อมูลให้เพียงพอเป็นต้น ดังนั้นในตัวอย่างเราสามารถใช้ข้อมูลประเภท
#include <iostream>

using namespace std;

int main()
{
    // using integer
    int apple = 3;
    int orange = 5;
    int total = apple + orange;
    cout << "Total fruit = " << total << endl;

    // using short, smaller size
    short day, month, year;
    day = 9;
    month = 3;
    year = 2017;
    cout << "Date: " << month << "/" << day << "/" << year << endl;

    // using unsigned, store only positive values
    unsigned int heght = 8848;
    cout << "The Mount Everest has " << heght << " height." << endl;

    // overflow
    int max_int  = 2147483647;
    cout << max_int << endl;
    max_int++;
    cout << max_int << endl;

    return 0;
}
4 ที่สามารถเก็บข้อมูลได้มากกว่า
#include <iostream>

using namespace std;

int main()
{
    bool gender = false;
    if (gender)
    {
        cout << "This is a boy." << endl;
    }
    else
    {
        cout << "This is a girl." << endl;
    }

    bool is_playing = true;
    cout << "Playing = " << is_playing << endl;
    cout << "Playing invert = " << !is_playing << endl;

    cout << "1 and True = " << (1 && true) << endl;
    cout << "True and True = " << (true && true) << endl;
    cout << "True and False = " << (true && false) << endl;
    cout << "True or True = " << (true || true) << endl;
    cout << "True or False = " << (true || false) << endl;

    return 0;
}
2

Total fruit = 8
Date: 3/9/2017
The Mount Everest has 8848 height.
2147483647
-2147483648

นี่เป็นผลลัพธ์การทำงานของโปรแกรมของตัวอย่างการใช้ Integer ในภาษา C++

Floating-point number

Floating-point number ใช้สำหรับเก็บข้อมูลตัวเลขแบบจำนวนจริงหรือตัวเลขที่มีทศนิยม ในภาษา C++ นั้นจะมีสองปะเภทคือ

#include <iostream>

using namespace std;

int main()
{
    bool gender = false;
    if (gender)
    {
        cout << "This is a boy." << endl;
    }
    else
    {
        cout << "This is a girl." << endl;
    }

    bool is_playing = true;
    cout << "Playing = " << is_playing << endl;
    cout << "Playing invert = " << !is_playing << endl;

    cout << "1 and True = " << (1 && true) << endl;
    cout << "True and True = " << (true && true) << endl;
    cout << "True and False = " << (true && false) << endl;
    cout << "True or True = " << (true || true) << endl;
    cout << "True or False = " << (true || false) << endl;

    return 0;
}
4 และ
#include <iostream>

using namespace std;

int main()
{
    // using integer
    int apple = 3;
    int orange = 5;
    int total = apple + orange;
    cout << "Total fruit = " << total << endl;

    // using short, smaller size
    short day, month, year;
    day = 9;
    month = 3;
    year = 2017;
    cout << "Date: " << month << "/" << day << "/" << year << endl;

    // using unsigned, store only positive values
    unsigned int heght = 8848;
    cout << "The Mount Everest has " << heght << " height." << endl;

    // overflow
    int max_int  = 2147483647;
    cout << max_int << endl;
    max_int++;
    cout << max_int << endl;

    return 0;
}
7 ซึ่งข้อมูลแบบ
#include <iostream>

using namespace std;

int main()
{
    bool gender = false;
    if (gender)
    {
        cout << "This is a boy." << endl;
    }
    else
    {
        cout << "This is a girl." << endl;
    }

    bool is_playing = true;
    cout << "Playing = " << is_playing << endl;
    cout << "Playing invert = " << !is_playing << endl;

    cout << "1 and True = " << (1 && true) << endl;
    cout << "True and True = " << (true && true) << endl;
    cout << "True and False = " << (true && false) << endl;
    cout << "True or True = " << (true || true) << endl;
    cout << "True or False = " << (true || false) << endl;

    return 0;
}
4 นั้นมีขนาดเล็กกว่าและเก็บตัวเลขหลังจุดได้ 8 ตัว ในขณะที่
#include <iostream>

using namespace std;

int main()
{
    // using integer
    int apple = 3;
    int orange = 5;
    int total = apple + orange;
    cout << "Total fruit = " << total << endl;

    // using short, smaller size
    short day, month, year;
    day = 9;
    month = 3;
    year = 2017;
    cout << "Date: " << month << "/" << day << "/" << year << endl;

    // using unsigned, store only positive values
    unsigned int heght = 8848;
    cout << "The Mount Everest has " << heght << " height." << endl;

    // overflow
    int max_int  = 2147483647;
    cout << max_int << endl;
    max_int++;
    cout << max_int << endl;

    return 0;
}
7 นั้นสามารถเก็บได้ 16 ตัว ดังนั้นมันจึงถูกใช้กับข้อมูลที่มีความละเอียดของตัวเลขมาก เช่น ข้อมูลการคำนวณทางวิทยาศาสตร์ เป็นต้น มาดูตัวอย่างการใช้งานในภาษา C++

#include <iostream>

using namespace std;

int main()
{
    float a = 2.42f;
    float b = -3.8f;
    float c = 1.58e3f;
    float d = -4.934e-6f;
    cout << "a = " << a << endl;
    cout << "b = " << b << endl;
    cout << "c = " << c << endl;
    cout << "d = " << d << endl;

    double e = 102.34;
    double f = 10.0 / 2.0;
    cout << "d + e = " << (e + f) << endl;

    return 0;
}

ในตัวอย่าง เป็นการใช้งานตัวแปรประเภท Floating-point number ใน 4 ตัวแปรแรกเป็นการใช้งานตัวแปรประเภท

#include <iostream>

using namespace std;

int main()
{
    bool gender = false;
    if (gender)
    {
        cout << "This is a boy." << endl;
    }
    else
    {
        cout << "This is a girl." << endl;
    }

    bool is_playing = true;
    cout << "Playing = " << is_playing << endl;
    cout << "Playing invert = " << !is_playing << endl;

    cout << "1 and True = " << (1 && true) << endl;
    cout << "True and True = " << (true && true) << endl;
    cout << "True and False = " << (true && false) << endl;
    cout << "True or True = " << (true || true) << endl;
    cout << "True or False = " << (true || false) << endl;

    return 0;
}
4 ซึ่งในการกำหนดค่าให้กับตัวแปรนั้นต้องลงท้ายด้วย
// using unsigned, store only positive values
unsigned int heght = 8848;
cout << "The Mount Everest has " << heght << " height." << endl;

// overflow
int max_int  = 2147483647;
cout << max_int << endl;
max_int++;
cout << max_int << endl;
1 เสมอ ส่วนข้อมูลประเภท
#include <iostream>

using namespace std;

int main()
{
    // using integer
    int apple = 3;
    int orange = 5;
    int total = apple + orange;
    cout << "Total fruit = " << total << endl;

    // using short, smaller size
    short day, month, year;
    day = 9;
    month = 3;
    year = 2017;
    cout << "Date: " << month << "/" << day << "/" << year << endl;

    // using unsigned, store only positive values
    unsigned int heght = 8848;
    cout << "The Mount Everest has " << heght << " height." << endl;

    // overflow
    int max_int  = 2147483647;
    cout << max_int << endl;
    max_int++;
    cout << max_int << endl;

    return 0;
}
7 นั้นไม่ต้อง

int n;
float money;
bool t;
0

ในการกำหนดค่าให้กับตัวแปรนั้นสามารถกำหนดในรูปแบบย่อหรือสัญกรณ์วิทยาศาสตร์โดยการใช้เครื่องหมาย

// using unsigned, store only positive values
unsigned int heght = 8848;
cout << "The Mount Everest has " << heght << " height." << endl;

// overflow
int max_int  = 2147483647;
cout << max_int << endl;
max_int++;
cout << max_int << endl;
3 เช่น ในตัวแปร
// using unsigned, store only positive values
unsigned int heght = 8848;
cout << "The Mount Everest has " << heght << " height." << endl;

// overflow
int max_int  = 2147483647;
cout << max_int << endl;
max_int++;
cout << max_int << endl;
4 นั้นจะหมายถึง
// using unsigned, store only positive values
unsigned int heght = 8848;
cout << "The Mount Everest has " << heght << " height." << endl;

// overflow
int max_int  = 2147483647;
cout << max_int << endl;
max_int++;
cout << max_int << endl;
5 และในตัวแปร
// using unsigned, store only positive values
unsigned int heght = 8848;
cout << "The Mount Everest has " << heght << " height." << endl;

// overflow
int max_int  = 2147483647;
cout << max_int << endl;
max_int++;
cout << max_int << endl;
6 นั้นจะหมายถึง
// using unsigned, store only positive values
unsigned int heght = 8848;
cout << "The Mount Everest has " << heght << " height." << endl;

// overflow
int max_int  = 2147483647;
cout << max_int << endl;
max_int++;
cout << max_int << endl;
7 และถ้าหากตัวเลขนั้นมีค่าใหญ่มากหรือน้อยมาก ภาษา C++ จะแสดงในรูปแบบย่ออัตโนมัติ

int n;
float money;
bool t;
1

นี่เป็นผลลัพธ์การทำงานของโปรแกรม

Strings

String เป็นตัวอักษรที่เรียงต่อกันที่ถูกแสดงในรูปแบบอาเรย์ของตัวอักษร แต่ String ในภาษา C++ เป็นออบเจ็คของคลาสที่มากับ C++ ไลบรารี่ ในการที่จะใช้ String เราจำเป็นต้องนำเข้าไลบรารี่ของ String โดยใช้คำสั่ง

// using unsigned, store only positive values
unsigned int heght = 8848;
cout << "The Mount Everest has " << heght << " height." << endl;

// overflow
int max_int  = 2147483647;
cout << max_int << endl;
max_int++;
cout << max_int << endl;
8 ข้อมูลประเภทสตริงนั้นจะเก็บข้อมูลในรูปแบบของคำหรือประโยค ที่เป็นตัวอย่างเพื่อใช้ String ในภาษา C++

int n;
float money;
bool t;
2

ในบทนี้ คุณได้เรียนรู้เกี่ยวกับประเภทข้อมูลและตัวแปรในภาษา C++ เราได้พูดถึงความหมายของตัวแปร และแสดงตัวอย่างการประกาศตัวแปร และอธิบายถึงความแตกต่างของข้อมูลประเภทต่างๆ ในการใช้งานให้เหมาะสมในการเขียนโปรแกรม ซึ่งเป็นสิ่งสำคัญที่คุณควรจะตระหนักถึงในการเลือกใช้ประเภทของข้อมูลให้เหมาะสมในการเขียนโปรแกรมเพื่อลดการใช้งานหน่วยความจำ ซึ่งจะทำให้โปรแกรมของคุณมีประสิทธิภาพมากขึ้น