gpt4 book ai didi

c - 警告 : format '%ld' expects argument of type 'long int' , 但参数 2 的类型为 'int'

转载 作者:行者123 更新时间:2023-11-30 18:35:11 26 4
gpt4 key购买 nike

构建日志:

warning: format '%ld' expects argument of type 'long int', but argument 2 has type '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,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;
}

最佳答案

这是undefined behavior 。编译器会向您发出警告,然后 printf 会尝试处理 sizeof(long int) 字节,其中您已为其提供了大小为 sizeof(int) 的整数文字) 字节。

它需要 sizeof(long int) 个字节,现在如果 sizeof(int)==sizeof(long) 就可以了,否则就是未定义的行为。

不要认为格式说明符与变量相似。 long 变量可以保存 int 的值。这与格式说明符不相符。

类型转换可以解决问题

printf("%ld \n",(long)3809);

来自标准§7.21.6.1(格式化输入/输出函数)

[7] If a conversion specification is invalid, the behavior is undefined. If any argument is not the correct type for the corresponding conversion specification, the behavior is undefined.

关于c - 警告 : format '%ld' expects argument of type 'long int' , 但参数 2 的类型为 'int',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47545131/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com