#include <stdio.h> float main() { float num; num =12345678.56789; printf("%6.1f",num); return 0; }为什么输出12345679
时间: 2024-05-19 16:12:16 浏览: 17
这是因为浮点数在计算机内部是以二进制表示的,而二进制无法精确表示十进制数。在存储浮点数时,计算机会对数字进行四舍五入,导致输出结果与预期结果不同。
在这个例子中,num的值为12345678.56789,但是由于计算机内部的舍入误差,它被存储为12345679。printf("%6.1f",num)指定了输出格式,%6.1f表示输出浮点数占6位,其中小数点后1位,因此输出结果为12345679.0。
如果需要精确表示小数,可以使用更高精度的数据类型,如double或long double。另外,也可以使用字符串表示数字,然后进行字符串处理。
相关问题
#include <stdio.h> float main() { num=1234.56789; printf("%6.1f",num); return 0; }这个哪里出错了?
这段代码中,有几个问题需要解决:
1. 在 C 语言中,需要先声明变量的类型,才能使用该变量。因此,需要在代码中添加 `float` 类型的变量 `num` 的声明语句。
2. 在 `printf` 函数中,需要使用格式化字符串 `%f` 来输出浮点数变量的值,而不是 `%6.1f`。
下面是修改后的代码:
```c
#include <stdio.h>
int main() {
float num = 1234.56789;
printf("%f", num);
return 0;
}
```
这样就可以输出浮点数变量 `num` 的值了。
#include <stdio.h> #include <stdlib.h> struct student { int num; char name[10]; int c,oop,db; float avg; }; void main() { int i; struct student s,s1,s2; for(i=1;i<3;i++) { printf("请输入第%d个学生的信息:\n",i); scanf("%d%s%d%d%d",&s.num,s.name,&s.c,&s.oop,&s.db); s.avg = (float)(s.c+s.oop+s.db)/3; if(i=1) s1=s; else s2=s; } if(s1.avg<s2.avg) { s=s1; s1=s2; s2=s; } printf("\n学号 姓名 c语言 OOP 数据库 平均成绩\n"); printf("%-10d%-10s%-8d%-8d%-8d%-6.1f\n",s1.num,s1.name,s1.c,s1.oop,s1.db,s1.avg); printf("%-10d%-10s%-6d%-6d%-6d%-6.1f\n",s2.num,s2.name,s2.c,s2.oop,s2.db,s2.avg); }哪里错了
在 if 语句中,判断 s1 和 s2 平均成绩大小的时候,应该使用比较运算符“<”,而不是赋值运算符“=”,所以程序会出现逻辑错误。正确的代码如下:
```c
#include <stdio.h>
#include <stdlib.h>
struct student {
int num;
char name[10];
int c,oop,db;
float avg;
};
void main() {
int i;
struct student s,s1,s2;
for(i=1;i<3;i++)
{
printf("请输入第%d个学生的信息:\n",i);
scanf("%d%s%d%d%d",&s.num,s.name,&s.c,&s.oop,&s.db);
s.avg = (float)(s.c+s.oop+s.db)/3;
if(i==1)
s1=s;
else
s2=s;
}
if(s1.avg<s2.avg)
{
s=s1;
s1=s2;
s2=s;
}
printf("\n学号 姓名 c语言 OOP 数据库 平均成绩\n");
printf("%-10d%-10s%-8d%-8d%-8d%-6.1f\n",s1.num,s1.name,s1.c,s1.oop,s1.db,s1.avg);
printf("%-10d%-10s%-6d%-6d%-6d%-6.1f\n",s2.num,s2.name,s2.c,s2.oop,s2.db,s2.avg);
}
```
相关推荐
![text/x-c](https://img-home.csdnimg.cn/images/20210720083646.png)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![exe](https://img-home.csdnimg.cn/images/20210720083343.png)
![image/gif](https://img-home.csdnimg.cn/images/20210720083352.png)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![application/pdf](https://img-home.csdnimg.cn/images/20210720083512.png)