C&C++   发布时间:2022-04-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了c – 打印unsigned long long int值类型返回奇怪的结果大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
@H_607_1@当使用printf函数打印unsigned long long int类型的值时,我有一个问题

@R_240_6618@了我正在使用Dev-Cpp 4.9.9.2和Visual studio 2010 Professional(我知道它不是C编译器,但无论如何,想要尝试)在Windows 7专业版64位.为了显示,我使用%llu修饰符(根据How do you printf an unsigned long long int(the format specifier for unsigned long long int)?),但我也尝试I64d没有影响…

首先,我只想打印无符号long long int的最小值和最大值(从limits.h使用ULONG_MAX)

printf("unsigned long long int: \n%llu to %llu \n\n",ULONG_MAX);

返回:

然后我尝试使用printf打印两个零

printf("unsigned long long int: \n%llu to %llu \n\n",0);

返回:

还尝试了两个ULONG_MAX值

printf("unsigned long long int: \n%llu to %llu \n\n",ULONG_MAX,ULONG_MAX);

返回:

为什么这样做呢?你可以向我解释一下吗

解决方法

这是错误的:
printf("unsigned long long int: \n%llu to %llu \n\n",ULONG_MAX);

您使用无符号长长格式说明符,但您传递int和无符号长整型值.促销规则意味着您可以对于大小或更小的一切都是马虎,这不适用于长时间.

使用演员:

printf("unsigned long long int: \n%llu to %llu \n\n",0ULL,(unsigned long long) ULONG_MAX);

说明:将参数传递给printf时,可以将适用于int的任何类型升级为int,然后将可以匹配无符号int的任何类型提升为unsigned int.只要可以使用格式说明符指定的类型表示传递的值,也可以将无符号类型传递给带格式的格式说明符,反之亦然.

所以你一定要小心长久,但是你可以用int,short和char来形容词.

大多数编译器都有设置让他们警告你这种类型的错误,因为它可以很容易地在编译时检测到; GCC和Clang有-Wformat,其结果如下警告:

test.c:5: warning: format ‘%llu’ expects type ‘long long unsigned int’,but argument 2 has type ‘int’
test.c:5: warning: format ‘%llu’ expects type ‘long long unsigned int’,but argument 3 has type ‘long unsigned int’

大佬总结

以上是大佬教程为你收集整理的c – 打印unsigned long long int值类型返回奇怪的结果全部内容,希望文章能够帮你解决c – 打印unsigned long long int值类型返回奇怪的结果所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。