signed and unsigned data types
Primitive Data Types
ประเภทตัวแปรพื้นฐานในภาษา C++ มีดังนี้
Integer (int)ใช้สำหรับเก็บข้อมูลประเภทตัวเลขจำนวนเต็ม
Character (char) ใช้สำหรับเก็บข้อมูลประเภทตัวอักษร
Boolean (bool) ใช้สำหรับเก็บข้อมูลเพียงสองค่าคือ true และ false
Floating point (float)ใช้สำหรับเก็บข้อมูลประเภทตัวเลขทศนิยม
Double Floating Point ใช้สำหรับเก็บข้อมูลประเภทตัวเลขทศนิยม โดยมีพื้นที่การเก็บมากกว่าตัวแปร float สองเท่า
Valueless (Void) ใช้สำหรับค่าว่างเปล่า
Wide Character ช้สำหรับเก็บข้อมูลประเภทตัวอักษร unicode
Data type modifiers
เราสามารถใช้คำสั่งด้านล่างนี้ในการกำหนดเพิ่มลดขนาด(Size)และกำหนดช่วงข้อมูล(Range) ในพื้นที่หน่วยความจำและการรับข้อมูลสำหรับเก็บค่าตัวแปร
Signed ใช้สำหรับกำหนดช่วงข้อมูลของตัวแปรให้สามารถรับค่าช่วงข้อมูลตัวเลขที่เป็นจำนวนลบได้
Unsigned ใช้สำหรับกำหนดช่วงข้อมูลของตัวแปรให้สามารถรับค่าช่วงข้อมูลที่ 0 ขึ้นไป จะไม่สามารถรับข้อมูลตัวเลขที่เป็นจำนวนลบได้
Short ใช้สำหรับกำหนดขนาดตัวแปร int ให้ใช้พื้นที่ลดลงเหลือ 4 bytesเป็น 2 bytes เหมาะกับข้อมูลตัวเลขที่ไม่เยอะมาก
long ใช้สำหรับกำหนดขนาดตัวแปร int ให้ใช้พื้นที่เพิ่มขึ้นจาก 4 bytes เป็น 8 bytes เหมาะกับข้อมูลตัวเลขที่ไม่เยอะมาก
Data Type
Size (in bytes)
Range
short int
2
-32,768 to 32,767
unsigned short int
2
0 to 65,535
unsigned int
4
0 to 4,294,967,295
int
4
-2,147,483,648 to 2,147,483,647
long int
8
-9.223372e+18 to 9.223372e+18
unsigned long int
8
0 to 18,446,744,073,709,551,615
long long int
8
-9.223372e+18 to 9.223372e+18
unsigned long long int
8
0 to 18,446,744,073,709,551,615
signed char
1
-128 to 127
unsigned char
1
0 to 255
float
4
1.2E-38 to 3.4E+38
double
8
2.3E-308 to 1.7E+308
void
-
-
wchar_t
2 or 4
1 wide character
สร้าง file.cpp ชื่อว่า sizeData.cpp แล้วคัดลอก code ด้านล่างลงไฟล์ที่สร้าง
// C++ program to sizes of data types
#include<iostream>
using namespace std;
int main()
{
cout << "Size of char : " << sizeof(char)
<< " byte" << endl;
cout << "Size of int : " << sizeof(int)
<< " bytes" << endl;
cout << "Size of short int : " << sizeof(short int)
<< " bytes" << endl;
cout << "Size of long int : " << sizeof(long int)
<< " bytes" << endl;
cout << "Size of signed long int : " << sizeof(signed long int)
<< " bytes" << endl;
cout << "Size of unsigned long int : " << sizeof(unsigned long int)
<< " bytes" << endl;
cout << "Size of float : " << sizeof(float)
<< " bytes" <<endl;
cout << "Size of double : " << sizeof(double)
<< " bytes" << endl;
cout << "Size of wchar_t : " << sizeof(wchar_t)
<< " bytes" <<endl;
return 0;
}
ทดลอง complier และ run โปรแกรม
Output :
Size of char : 1 byte
Size of int : 4 bytes
Size of short int : 2 bytes
Size of long int : 8 bytes
Size of signed long int : 8 bytes
Size of unsigned long int : 8 bytes
Size of float : 4 bytes
Size of double : 8 bytes
Size of wchar_t : 4 bytes
ตัวอย่าง ความแตกต่างของข้อมูล unsigned int and int
#include <iostream>
using namespace std;
int main()
{
int A = -1;
unsigned int AA = 1;
cout<< " Size of char A :" << sizeof(A)<< "byte" << endl;
cout<< " Size of char A :" << sizeof(AA)<< "byte" << endl;
cout<< "A = "<<A<< endl;
cout<< "AA = "<<AA<< endl;
cout<< "A-A = "<<A-A<< endl;
cout<< "A-AA = " << A-AA<< endl;
return 0;
}
ทดลอง complier และ run โปรแกรม
Size of char A :4byte
Size of char A :4byte
A = -1
AA = 1
A-A = 0
A-AA = 4294967294 // max range of unsign 4 byte is 4294967295
สร้าง file.cpp ชื่อว่า rangeData.cpp แล้วคัดลอก code ด้านล่างลงไฟล์ที่สร้าง
#include <iostream>
#include <limits>
using namespace std;
int main()
{
cout<<"Limits of Data types:\n";
cout<<"unsigned char : "<<static_cast<int>(numeric_limits<unsigned char>::min())<<"to "<<static_cast<int>(numeric_limits<unsigned char>::max())<<endl;
cout<<"char : "<<static_cast<int>(numeric_limits<char>::min())<<"to "<<static_cast<int>(numeric_limits<char>::max())<<endl;
cout<<"short : "<<numeric_limits<short>::min()<<"to "<<numeric_limits<short>::max()<<endl;
return 0;
}
ทดลอง complier และ run โปรแกรม
Output:
Limits of Data types:
unsigned char : 0to 255
char : -128to 127
short : -32768to 32767
แหล่งอ้างอิง
Last updated
Was this helpful?