如何用C ++编写short字面量?
在这里,我们将看到C++中的short字面量。在C或C++中,不同类型的数据具有不同的文字。这些在下面列出。
5
5U
5L
5LL
5.0f
5.0
'\5'
现在,有int,longfloat,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