C程序将一个数字转换为字符串

PHPz
PHPz 转载
2023-08-25 18:17:13 414浏览

C程序将一个数字转换为字符串

Input: User will put some numeric value say 42.26 Output: This program will return the string equivalent result of that number like "42.26"

算法

Step 1: Take a number from the user
Step 2: Create an empty string buffer to store result
Step 3: Use sprintf() to convert number to string
Step 4: End

示例代码

实时演示

#include<stdio.h>
main() {
   char str[20];    //create an empty string to store number
   float number;
   printf("Enter a number: ");
   scanf("%f", &number);
   sprintf(str, "%f", number);   //make the number into string using sprintf function
   printf("

You have entered: %s", str); }

输出:

Enter a number: 46.3258
You have entered: 46.325802

以上就是C程序将一个数字转换为字符串的详细内容,更多请关注php中文网其它相关文章!

声明:本文转载于:tutorialspoint,如有侵犯,请联系admin@php.cn删除