在本节中,我们将了解如何将数字(整数或浮点或任何其他数字类型数据)转换为字符串。
逻辑非常简单。这里我们将使用 sprintf() 函数。此函数用于将某些值或行打印到字符串中,但不在控制台中。这是 printf() 和 sprintf() 之间的唯一区别。这里第一个参数是字符串缓冲区。我们想要保存数据的地方。
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("</p><p>You have entered: %s", str); }
Enter a number: 46.3258 You have entered: 46.325802
以上就是C程序将一个数字转换为字符串的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号