2. ข้อมูล ชนิดของข้อมูลในภาษา C++ แบ่งเป็น 2 ประเภท คือ Simple Data type ( เรียนวันนี้ ) แสดงค่าของสิ่งใดสิ่งหนึ่งเพียงรายการเดียว เช่น ความกว้าง , ความยาว , ความสูง , ฐาน เป็นต้น Structure Data type ( เรียนภายหลัง ) แสดงค่าของสิ่งใดสิ่งหนึ่งหลายรายการ เช่น ปริมาณน้ำฝนในแต่ละวันของเดือนมกราคม ปี 51 , ความสูงของต้นไม้ในระยะ 1 เดือน เป็นต้น
3. Simple Data Type The values shown above are those found on most 32-bit systems unsigned: 0 to 4294967295 signed: -2147483648 to 2147483647 4 bytes Long integer. long int unsigned: 0 to 4294967295 signed: -2147483648 to 2147483647 4 bytes Integer. int unsigned: 0 to 65535 signed: -32768 to 32767 2 bytes Short Integer. short int unsigned: 0 to 255 signed: -128 to 127 1 byte Character or small integer. char Range* Size* Description Name
4. Simple Data Type The values shown above are those found on most 32-bit systems 1.7 x 10 -308 to 1.7 x 10 +308 8 bytes Long double precision floating point number. long double 1.7 x 10 -308 to 1.7 x 10 +308 8 bytes Double precision floating point number. double 3.4 x 10 -38 to 3.4 x 10 +38 4 bytes Floating point number. float true or false 1 byte Boolean value. It can take one of two values: true or false. bool Range* Size* Description Name
5. วิธีเลือกชนิดตัวแปรให้เหมาะกับ data type ตัวแปร เก็บ อักขระ 1 ตัวอักษร ใช้ char ตัวแปร เก็บ ตัวเลขไม่มีจุดทศนิยม เลือกใช้ short int, int, long int ตัวแปร เก็บ ตัวเลขแบบมีจุดทศนิยม เลือกใช้ float, double, long double ตัวแปร เก็บ อักขระมากกว่า 1 ตัวอักษร ใช้ string โดยต้องเพิ่ม header file ชื่อ string เข้าไปในโปรแกรม
6. ตัวอย่างการเลือก Data Type ให้เหมาะกับตัวแปร โจทย์การการหาพื้นที่ผิวทรงกลมที่มีรัศมี r ตัวแปรสำหรับเก็บค่าคงที่ เก็บค่า 3.14 แสดงว่า เก็บเลขทศนิยม จึงควรใช้ตัวแปรแบบ float ตัวแปรเก็บค่ารัศมี รัศมี เป็นได้ทั้งจำนวนเต็มและทศนิยม ดังนั้นควรใช้ตัวแปร float ตัวแปรเก็บผลลัพธ์ ผลลัพธ์เป็นได้ทั้งจำนวนเต็มและทศนิยม ดังนั้นควรใช้ตัวแปร float