c - if (id==1) printf ("%i", id) 打印一些随机值

标签 c printf

好的。这一定是我遇到的最奇怪的错误之一。我的代码中有这两行

 int id=i*2000*512+512*row+column;
 if (id==1){printf("This is output %f %f %f %i \n",entire_red[id],entire_green[id],entire_blue[id],id);}

它给了我输出

This is the output 0.000000 0.000000 0.000000 109456488.

我不知道发生了什么!

最佳答案

%f 格式说明符需要一个 double(float 隐式转换为 double 用于可变参数函数) .如果 variable_red 等不是 floatdouble 变量,则 printf 将错误地处理参数,并且可以解释为什么 id 整数打印不正确。

试试这个:

if (id==1)
    printf("This is output %f %f %f %i\n", (double)entire_red[id],(double)entire_green[id],(double)entire_blue[id],id);

或者,确保为 entire_redentire_greenentire_blue 变量使用正确的格式说明符。

关于c - if (id==1) printf ("%i", id) 打印一些随机值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5213537/

相关文章:

C修改printf()输出到文件

c - printf 在程序退出时刷新

c++ - fprintf、字符串和 vector

c - C中总节点的错误结果

c - 分发用 C 编写的库

c - 使用 malloc 时如何正确使用 free() 来释放内存?

c - 使用 fprintf() 在 C 中写入文本文件

c - openmp理论与实践效率?

c++ - 使用 C++ 或 C 处理图像

c - 询问用户的姓氏和名字,并以电子邮件帐户作为输出