题目:从键盘输入一个数,求其绝对值
错误代码:
#include <iostream>
using namespace std;
int main()
{
while (true)
{
float t;
printf("please enter a number \n");
scanf("%f",&t);
if(t>0)
t=t;
else
t=-t;
printf("this is the number is ",t);
}
return 0;
}
正确代码:
#include <iostream>
using namespace std;
int main()
{
while (true)
{
float t;
printf("please enter a number \n");
scanf("%f",&t);
if(t>0)
t=t;
else
t=-t;
printf("this is the number is %f ",t);
}
return 0;
}
printf的用法(格式控制,输出列表),
我自己的理解是。计算机在识别语句的时候,需要一个标识符,就是%f,识别之后,将变量赋值,实现输出。
详细内容可参照 http://blog.sina.com.cn/s/blog_56e8acf40100kaer.html。
本文通过一个实例分析了在C语言中使用printf函数时的一个常见错误,即输出浮点数缺少格式控制符。错误代码在输出时漏掉了%f,导致编译错误。正确的做法是在输出浮点数t时使用`printf("this is the number is %f ", t);`。printf函数的格式控制对于理解和编写正确代码至关重要。"
112085710,10539468,Excel数据跟踪表制作指南:轻松管理采购订单,"['Excel', '数据管理', '日期处理', '办公效率']

被折叠的 条评论
为什么被折叠?



