Written by Administrator
|
Thursday, 04 October 2012 08:07 |
ชนิดของข้อมูล(Data Type) ข้อมูลใน C++ แบ่งชนิดข้อมูลออกเป็น 2 ประเภท คือ 1. Simple data type เป็นชนิดข้อมูลที่ใช้แสดงค่าของสิ่งใดสิ่งหนึ่งเพียงรายการเดียว เช่น ค่า ความสูง นํ้ าหนัก จำนวนนักเรียน อุณหภูมิ ระดับคะแนน เป็นต้น 2. Structure เป็นข้อมูลชนิดใช้แสดงค่าของสิ่งใดสิ่งหนึ่งหลายรายการ เช่น ความสูงของนัก เรียนใน ชั้น ม. 6, อุณหภูมิของแต่ละวันในเดือนตุลาคม, รายชื่อนักเรียนใน 1 กลุ่ม ต้องกำหนดเป็นข้อมูล ชนิดโครงสร้างแบบ อาร์เรย์ (array) แบบโครงสร้าง(structure) หรือแบบยูเนียน(union) เป็นต้น ข้อมูล Simple data type รายละเอียดชนิดของมูลและช่วงของข้อมูลประเภท Simple data type แสดงได้ดังตารางต่อไปนี้
 หมายเหตุ ชื่อชนิดของข้อมูลได้แก่ char, unsigned char , int , unsigned int, short in, long , unsigned long , float , double , long double เป็น keyword ที่นำไปกำหนดประเภทข้อมูลที่จะใช้ใน โปรแกรม
• ตัวอย่างโปรแกรม size_mem.cpp เป็นตัวอย่างโปรแกรมแสดงขนาดของหน่วยความจำที่ข้อ มูลแต่ละชนิดใช้พื้นที่หน่วยความจำ โดยใช้ คีย์เวิร์ด sizeof ซึ่งให้ค่าขนาดหน่วยความจำที่ข้อ มูลชนิดนั้นใช้ มีหน่วยเป็น byte /*Program : size_mem.cpp Process : Display size of memory for each simple data type */ #include <iostream> #include <conio.h> int main() using namespace std; { clrscr(); //clear screen in conio.h cout<< "Size of char = "<<sizeof(char)<< " bytes"<<endl; cout<< "Size of unsigned char = "<<sizeof(unsigned char)<< " bytes"<<endl; cout<< "Size of int = "<<sizeof(int)<< " bytes"<<endl; cout<< "Size of unsigned int = "<<sizeof(unsigned int)<< " bytes"<<endl; cout<< "Size of short int = "<<sizeof(short int)<< " bytes"<<endl; cout<< "Size of long = "<<sizeof(long)<< " bytes"<<endl; cout<< "Size of unsigned long = "<<sizeof(unsigned long)<< " bytes"<<endl; cout<< "Size of float = "<<sizeof(float)<< " bytes"<<endl; cout<< "Size of double = "<<sizeof(double)<< " bytes"<<endl; cout<< "Size of long double = "<<sizeof(long double)<< " bytes"<<endl; getch(); return 0; //wait for press any key }
|