C 警告:格式';%ld';应为类型为';长整型';,但参数2的类型为';int';
生成日志: 警告:格式“%ld”要求参数类型为“long int”,但参数2的类型为“int” 节目:C 警告:格式';%ld';应为类型为';长整型';,但参数2的类型为';int';,c,C,生成日志: 警告:格式“%ld”要求参数类型为“long int”,但参数2的类型为“int” 节目: #include <stdio.h> #include <stdlib.h> int main() { printf("Hello world!\n");///for a new line printf("Hello world ");///simple printf("enter some values = %d %d %f",54432,5
#include <stdio.h>
#include <stdlib.h>
int main()
{
printf("Hello world!\n");///for a new line
printf("Hello world ");///simple
printf("enter some values = %d %d %f",54432,54,54.76474)
printf("%d \n",232);///integer in new line
printf("%f \n",21.322432);///decimal in new line
printf("%ld \n",3809);///large integer in new line
printf("%lf \n",432758575375735.24);///large float in new line
return 0;
}
#包括
#包括
int main()
{
printf(“Hello world!\n”);///用于新行
printf(“你好世界”);///简单
printf(“输入一些值=%d%d%f”,54432,54,54.76474)
printf(“%d\n”,232);///新行中的整数
printf(“%f\n”,21.322432);///新行小数
printf(“%ld\n”,3809);///新行中的大整数
printf(“%lf\n”,432758575375735.24);///新行中的大浮点
返回0;
}
这是一个。编译器向您发出警告,然后printf
尝试处理sizeof(long int)
字节,其中您提供了一个整数文本,其大小为sizeof(int)
字节
它需要sizeof(long int)
bytes,现在如果sizeof(int)==sizeof(long)
就可以了,否则就是未定义的行为
不要认为格式说明符与变量类似long
变量可以保存int
的值。这与格式说明符不符。
铸造可以解决这个问题
printf("%ld \n",(long)3809);
来自标准§7.21.6.1
(格式化输入/输出功能)
[7] 如果转换规范无效,则行为无效
未定义如果任何参数的类型不正确
对应的转换规范,行为未定义。
默认情况下,整数常量的类型为
int
。“%ld”
格式说明符需要一个长int
。使用错误的格式说明符会调用未定义的行为
您需要为整型文字添加L
后缀,使其类型long
:
printf("%ld \n",3809L);
整数文本(如
3809
)默认为int
。在大多数现代系统中,int
是有符号的32位类型。long
的类型也可以是有符号的32位,但它仍然是与int
不同的类型。也许你所需要的只是?请不要发表亵渎的言论。而且,这个错误是不言自明的。你自己尝试过解决这个问题吗?那么,你的问题是什么呢?顺便说一句:432758575375735.24
不是一个float
,而是一个double
类型的浮点常量。“你需要为整型文字添加L后缀以使其具有long类型”几乎是免费的1234567890123
在32位int
/64位长
上是一个长
,即使没有L
。当然,int
范围内的常量需要一个L
。虽然我更喜欢避免强制转换,但在printf(..)
中强制转换常量是一种防御性编程。@chux.:是的,有点像这样..我承认。