![](/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.