long long出错问题

helun20111185023 2012-08-21 11:05:28
#include<stdio.h>
int main(void)
{
unsigned long long p;

p=15466587565525;

printf("%llu",p);


// putchar('\n');
return 0;
}
怎么P这个数输出来有错误啊?输出结果为410332629,明明没有超过P的范围啊,为什么会得到这个结果呢?求助啊!!!!
...全文
336 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
自由建客 2012-08-22
  • 打赏
  • 举报
回复
据说 Windows 平台要用 %l64u
赵4老师 2012-08-22
  • 打赏
  • 举报
回复
常量也有类型!
C++ Integer Constants
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.

Syntax

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 :

0x hexadecimal-digit
0X hexadecimal-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

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:

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:

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:

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:

unsigned uVal = 328u; // Unsigned value
long lVal = 0x7FFFFFL; // Long value specified
// as hex constant
unsigned long ulVal = 0776745ul; // Unsigned long value

helun20111185023 2012-08-22
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 的回复:]
如果你在Windows环境下的话,那么建议用MinGW编译看看,确定是否是编译器的处理问题。


引用 3 楼 的回复:

引用 1 楼 的回复:
用GCC编译没有问题,不过最好加LL主动说明p = 15466587565525LL是long long类型的数据。
还是不行啊!!!
[/Quote]应该不是编译器的问题,若将P的数据类型换成double,则输出结果是正确的,这是否说明发生了数据溢出呢?但long long型是八个字节,应该不会发生溢出啊。就是这点让人郁闷了
helun20111185023 2012-08-22
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 的回复:]
linux 下GCC 测试没有问题

LZ用sizeof(unsigned long long)输出看看是否是八个字节!

上面 的那个错误答案是p mod 2的32次幂的结果!

printf("%llu", p);
GCC 没有报错,输出是也正确的
[/Quote]应该是编译器的问题,非常感谢!!
图灵狗 2012-08-22
  • 打赏
  • 举报
回复
如果你在Windows环境下的话,那么建议用MinGW编译看看,确定是否是编译器的处理问题。

[Quote=引用 3 楼 的回复:]

引用 1 楼 的回复:
用GCC编译没有问题,不过最好加LL主动说明p = 15466587565525LL是long long类型的数据。
还是不行啊!!!
[/Quote]
helun20111185023 2012-08-22
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]
用GCC编译没有问题,不过最好加LL主动说明p = 15466587565525LL是long long类型的数据。
[/Quote]还是不行啊!!!
smsgreenlife 2012-08-22
  • 打赏
  • 举报
回复
将long long 替换成__int64就行了(注意是两个下划线)。
惊天霸戈 2012-08-22
  • 打赏
  • 举报
回复
linux 下GCC 测试没有问题

LZ用sizeof(unsigned long long)输出看看是否是八个字节!

上面 的那个错误答案是p mod 2的32次幂的结果!

printf("%llu", p);
GCC 没有报错,输出是也正确的
helun20111185023 2012-08-22
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 的回复:]
据说 Windows 平台要用 %l64u
[/Quote]不行啊
图灵狗 2012-08-21
  • 打赏
  • 举报
回复
用GCC编译没有问题,不过最好加LL主动说明p = 15466587565525LL是long long类型的数据。

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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