ที่นี่เราจะมาดูกันว่าตัวอักษรสั้น ๆ ใน C ++ จะเป็นอย่างไร ใน C หรือ C++ ข้อมูลประเภทต่างๆ มีตัวอักษรต่างกัน ดังต่อไปนี้
| Sr.No | ประเภทข้อมูลและตัวอักษร |
|---|---|
| 1 | int 5 |
| 2 | unsigned int 5U |
| 3 | ยาว 5L |
| 4 | ยาว 5LL |
| 5 | ลอย 5.0f |
| 6 | สองเท่า 5.0 |
| 7 | ถ่าน '\5' |
ขณะนี้มี int, long float, double ฯลฯ แต่ไม่มี short อยู่ ดังนั้นเราจึงไม่สามารถใช้ตัวอักษรใดๆ สำหรับข้อมูลประเภทสั้นได้ แต่เราสามารถแก้ปัญหานี้ได้ด้วยการพิมพ์แบบชัดแจ้ง
หากเราใช้เส้นตามด้านล่างนี้ ก็จะถูกแปลงเป็น short.
int x; x = (short) 5; //converted into short type data.
ตัวอย่าง
#include <iostream>
using namespace std;
main() {
int x;
x = 65700;
cout << "x is (as integer):" << x << endl;
x = (short)65700; //will be rounded after 2-bytes
cout << "x is (as short):" << x << endl;
} ผลลัพธ์
x is (as integer):65700 x is (as short):164