VisualC++ 6.0 带有 unsigned long long 和 sprintf
我想在 Visual C++ 6.0(普通 C)中 sprintf() 一个无符号 long long 值。
char buf[1000]; //bad coding
unsigned __int64 l = 12345678;
char t1[6] = "test1";
char t2[6] = "test2";
sprintf(buf, "%lli, %s, %s", l, t1, t2);
给出结果
12345678, (null), test1
(注意 test2
未打印)
和 l = 123456789012345
它给出了异常处理
任何建议?
I want to sprintf() an unsigned long long value in visual C++ 6.0 (plain C).
char buf[1000]; //bad coding
unsigned __int64 l = 12345678;
char t1[6] = "test1";
char t2[6] = "test2";
sprintf(buf, "%lli, %s, %s", l, t1, t2);
gives the result
12345678, (null), test1
(watch that test2
is not printed)
and l = 123456789012345
it gives an exception handle
any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
要在 Visual C++ 6.0 中打印
unsigned __int64
值,应使用%I64u
,而不是%lli
(请参阅 MSDN 上的此页面)。%lli
仅在 Visual Studio 2005 及更高版本中受支持。所以,你的代码应该是:
To print an
unsigned __int64
value in Visual C++ 6.0 you should use%I64u
, not%lli
(refer to this page on MSDN).%lli
is only supported in Visual Studio 2005 and later versions.So, your code should be:
printf 使用省略号来传递变量参数列表。 您看到的 (null) 是 long long 的第二部分,恰好全是 0 位。 将其设置为 1<<60+1<<30 ,您会遇到崩溃,因为 1<<60 部分被解释为 char*。
正确的解决方案是将数字分解为 10 位数字的三部分,“verylongvalue % 10000000000”“(verylongvalue/10000000000) % 10000000000”“verylongvalue/100000000000000000000”。
printf uses the ellipsis to pass a variable argument list. The (null) you see is the second part of your long long, which happen to be all 0 bits. Set it to 1<<60+1<<30 and you'll get a crash as the 1<<60 part is interpreted as a char*.
The correct solution would be to break down the number in three parts of 10 digits, "verylongvalue % 10000000000" "(verylongvalue/10000000000) % 10000000000" "verylongvalue/100000000000000000000".
显然,您没有将
additionaltext
分配给必要的char *
(字符串)。 请注意,long int
已被处理,逗号已被复制,并且仅%s
生成了(null)
。Apparently, you did not assign
additionaltext
to the necessarychar *
(string). Note that thelong int
was processed, the comma was copied and only the%s
generated(null)
.