小编JB0*_*2D1的帖子

C动态printf double,不损失精度,没有尾随零

我是C的新手,从书本上学习/在互联网上学习.我正在尝试编写一个我可以传递给任何函数的函数,double并返回一个intprintf("%.*lf" ...语句中使用的函数,这样返回的函数int既不会降低精度也不会产生尾随零.

我有一个工作函数,但它很大,因为它是为了可读性而编写的,所有注释都是如此.

为了总结这个函数,我计算得到该double范围所需的除以10的除法10 > d >= 0,只取小数部分并将其放入string带有n个小数位的位置n = 15 - number_of_digits_left_of_decimal(我读取该类型double只能跟踪15位数),检查string从右到左的尾随零并保持计数,最后返回一个int代表小数点右边非零数字的数字.

有没有更简单的方法?谢谢.

int get_number_of_digits_after_decimal(double d)
{
  int i = 0;      /* sometimes you need an int */
  int pl = 0;     /* precision left = 15 - sigfigs */
  int sigfigs = 1; /* the number of digits in d */
  char line[20];  /* used to find …
Run Code Online (Sandbox Code Playgroud)

c floating-point printf

6
推荐指数
2
解决办法
7895
查看次数

strfry(char*__ string)返回int?

所以我是C的新手,当我遇到https://www.gnu.org/software/libc/manual/html_node/strfry.html#strfry时,我正在玩GNU C库中的函数.

好奇,我写了一个小测试程序:

1 #include <stdio.h>
2 #include <string.h>
3 
4 main ()
5 {
6     char *str = "test123abc";
7     char *other;
8 
9     other = strfry(str);
10    printf("%s\n", other);
11     return 0;
12 }
Run Code Online (Sandbox Code Playgroud)

gcc test.c 输出 test.c:9: warning: assignment makes pointer from integer without a cast

为什么?

/usr/include/string.h 有以下条目:

extern char *strfry (char *__string) __THROW __nonnull ((1));

如何char *function(...)回归int

谢谢

c

4
推荐指数
1
解决办法
1406
查看次数

标签 统计

c ×2

floating-point ×1

printf ×1