【发布时间】:2017-06-06 21:47:05
【问题描述】:
我刚刚开始学习如何编码(就像 3 天前一样)而且我有一些问题......首先,我的“int”、“long int”和“unsigned int”看起来都是一样的大小。
#include <iostream>
#include <limits.h>
using namespace std;
int main() {
int value = 2147483647;
cout << value << endl;
cout << "Max int value: "<< INT_MAX << endl;
cout << "Min int value: "<< INT_MIN << endl;
long lvalue = 568534534;
cout << lvalue << endl;
short svalue = 22344;
cout << svalue << endl;
cout << "Size of int " << sizeof(int) << endl;
cout << "Size of short int: " << sizeof(short) << endl;
cout << "Size of long int: " << sizeof(long) << endl;
cout << "Size of unsigned int: " << sizeof(unsigned int) << endl;
unsigned int uvalue = 5345554;
cout << uvalue << endl;
return 0;
}
当我运行它时,我得到了这个:
568534534
22344
Size of int 4
Size of short int: 2
Size of long int: 4
Size of unsigned int: 4
5345554
如您所见。长且无符号的 int = int。
这不是我唯一的问题。使用“long double”,无论数字有多大,它总是输出负数。 代码如下:
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
float fvalue = 123.456789;
cout << sizeof(float) << endl;
cout << setprecision(20) << fixed << fvalue << endl;
double dvalue = 234.5554;
cout << setprecision(20) << fixed << dvalue << endl;
long double lvalue = 123.4;
cout << setprecision(20) << fixed << lvalue << endl;
return 0;
}
结果如下:
4
123.45678710937500000000
234.55539999999999000000
-18137553330312606000000000000000000000000000000000000
(顺便说一句,删除了大部分零)
所以,正如您所见,有些东西存在问题。 我使用 eclipse 作为我的 IDLE 和 我使用 MinGW32 虽然我在 64 位系统上,但我尝试为我的系统安装 MinGW-w64,但找不到如何...
【问题讨论】:
-
C++ 将整数类型的大小留给了实现者Give this a read。请注意,C++ 标准仅指定整数类型至少为 X 位。您所有的整数类型都可以是 64 位。
-
你真的应该一次问一个问题。
-
请注意,在 C++ 中,您可以使用
std::numeric_limits代替像INT_MAX这样的 C 宏。