在这里,我们将看到C ++中的 short 字面量。在C或C ++中,不同类型的数据具有不同的文字。这些在下面列出。
| 序号 | 数据类型和字面量 |
|---|---|
| 1 | int 5 |
| 2 | unsigned int 5U |
| 3 | Long 5L |
| 4 | long long 5LL |
| 5 | float 5.0f |
| 6 | double 5.0 |
| 7 | char '\5' |
现在,有int,long float,double等,但是不存在short。因此,我们不能对short类型数据使用任何字面量。但是我们可以通过显式类型转换解决此问题。
如果我们使用如下所示的行,那么它将被转换为short。
int x; x = (short) 5; //转换成短类型数据。
#include <iostream>
using namespace std;
main() {
int x;
x = 65700;
cout << "x is (as integer):" << x << endl;
x = (short)65700; //将在2字节后四舍五入
cout << "x is (as short):" << x << endl;
}输出结果
x is (as integer):65700 x is (as short):164