这个是越界导致的么?

lhyxiaolang 2016-01-26 01:45:47
vs2010,操作系统是64位,
为什么在计算的时候出现的结果不正确的情况
long long bindgold = ( 7 * 100000 + 150000) * 6000;
这个是什么原因?
...全文
255 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
jianglutian 2016-02-03
  • 打赏
  • 举报
回复
long long bindgold = ( 7 * 100000 + 150000) * 6000;
改为
long long bindgold = ( 7 * 100000LL + 150000) * 6000;
这样第一个用的常量就变成了LL类型,不会有溢出丢失了。
苏叔叔 2016-01-27
  • 打赏
  • 举报
回复

int main(void) {
	cout << typeid(1).name() << endl;
	cout << typeid(1L).name() << endl;
	cout << typeid(1LL).name() << endl;
	return 0;
}
//int
//long
//__int64
gaodlike 2016-01-26
  • 打赏
  • 举报
回复
赋值语句会产生临时变量(你右边的东西), 这个临时变量是int型的,因此这个临时变量溢出。 你把临时变量加一个l或者加个小数点就行了
paschen 版主 2016-01-26
  • 打赏
  • 举报
回复
虽然声明的是你这只是把已经溢出了的nt常量放到了long long中 如果要让你的常量是long long类型,在你的常量后面加上LL
fefe82 2016-01-26
  • 打赏
  • 举报
回复
( 7ll * 100000ll + 150000ll) * 6000ll;
赵4老师 2016-01-26
  • 打赏
  • 举报
回复
常量也有类型! Collapse AllExpand All Code: All Code: Multiple Code: Visual Basic Code: C# Code: Visual C++ Code: J# Code: JScript Visual Basic C# Visual C++ J# JScript Visual C++ Language Reference C++ Integer Constants See Also Send Feedback Integer constants are constant data elements that have no fractional parts or exponents. They always begin with a digit. You can specify integer constants in decimal, octal, or hexadecimal form. They can specify signed or unsigned types and long or short types. Grammar integer-constant: decimal-constant integer-suffixopt octal-constant integer-suffixopt hexadecimal-constant integer-suffixopt 'c-char-sequence' decimal-constant: nonzero-digit decimal-constant digit octal-constant: 0 octal-constant octal-digit hexadecimal-constant: 0xhexadecimal-digit 0Xhexadecimal-digit hexadecimal-constant hexadecimal-digit nonzero-digit: one of 1 2 3 4 5 6 7 8 9 octal-digit: one of 0 1 2 3 4 5 6 7 hexadecimal-digit: one of 0 1 2 3 4 5 6 7 8 9 a b c d e f A B C D E F integer-suffix: unsigned-suffix long-suffixopt long-suffix unsigned-suffixopt unsigned-suffix: one of u U long-suffix: one of l L 64-bit integer-suffix: i64 LL ll To specify integer constants using octal or hexadecimal notation, use a prefix that denotes the base. To specify an integer constant of a given integral type, use a suffix that denotes the type. To specify a decimal constant, begin the specification with a nonzero digit. For example: Copy Code int i = 157; // Decimal constant int j = 0198; // Not a decimal number; erroneous octal constant int k = 0365; // Leading zero specifies octal constant, not decimal To specify an octal constant, begin the specification with 0, followed by a sequence of digits in the range 0 through 7. The digits 8 and 9 are errors in specifying an octal constant. For example: Copy Code int i = 0377; // Octal constant int j = 0397; // Error: 9 is not an octal digit To specify a hexadecimal constant, begin the specification with 0x or 0X (the case of the "x" does not matter), followed by a sequence of digits in the range 0 through 9 and a (or A) through f (or F). Hexadecimal digits a (or A) through f (or F) represent values in the range 10 through 15. For example: Copy Code int i = 0x3fff; // Hexadecimal constant int j = 0X3FFF; // Equal to i To specify an unsigned type, use either the u or U suffix. To specify a long type, use either the l or L suffix. For example: Copy Code unsigned uVal = 328u; // Unsigned value long lVal = 0x7FFFFFL; // Long value specified // as hex constant unsigned long ulVal = 0776745ul; // Unsigned long value To specify a 64-bit integral type, use the LL, ll or i64 suffix. For example, Copy Code // 64bitsuffix.cpp #include <stdio.h> enum MyEnum { IntType, Int64Type }; MyEnum f1(int) { printf("in f1(int)\n"); return IntType; } MyEnum f1(__int64) { printf_s("in f1(__int64)\n"); return Int64Type; } int main() { MyEnum t1 = f1(0x1234), t2 = f1(0x1234i64); } Output in f1(int) in f1(__int64) See Also Concepts Literals (C++) Send feedback on this topic to Microsoft.

64,654

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧