![](/img/trans.png)
[英]Why does this code work? The input for the function is "string s" but the actual input we are giving is "int name". C language
[英]C program: Wrote code to convert int to float, but it's giving me 0.0000 and it's changing my input to some strange number
我有以下代码,也可以在此链接上找到 。
#include <stdio.h>
int main()
{
int n;
printf("\nPlease input an integer to convert to float: %d", n );
scanf("%d", &n);
printf("\nYour float is: %.5f", n);
return(0);
}
它所做的是不转换浮点数,它只给我0.0000,它也接受我的输入并使它成为古怪的数字。 我不确定自己在做什么错,即使在不同的站点四处浏览也是如此。 我也不能使用任何库,所以atol等都没有
#include <stdio.h>
int main() {
int n;
printf("\nPlease input an integer to convert to float: ");
scanf("%d", &n);
printf("\nYour float is: %.5f", (float)n);
return(0);
}
如果您希望以这种方式将整数转换为浮点数,则必须显式编写转换。 如果不这样做,则会导致未定义的行为。 另一种方法是声明一个float变量并为其分配n。
顺便说一句,您编写的代码在没有GCC警告的情况下也不应编译。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.