如何在 C++ 中编写一个短字面值?
接下来,我们将了解如何在 C++ 中编写短字面值。在 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。
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
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP