c语言中用整型格式化输出浮点型;还是用浮点型格式化输出整型都是不可行的

 

001、整型格式化输出浮点型

[root@PC1 test]# ls
test.c
[root@PC1 test]# cat test.c
#include <stdio.h>

int main(void)
{
        double i;                        //定义浮点型变量
        i = 5.8;

        printf("i = %d\n", i);          // 用整型格式化输出浮点return 0;
}
[root@PC1 test]# gcc test.c -o kkk
[root@PC1 test]# ls
kkk  test.c
[root@PC1 test]# ./kkk                  ## 返回值是一个无规则的值
i = -1554552856
[root@PC1 test]# ./kkk
i = -1307037784

 

002、浮点型格式化输出整型

[root@PC1 test]# ls
test.c
[root@PC1 test]# cat test.c
#include <stdio.h>

int main(void)
{
        int i;                               \\定义一个整型变量
        i = 10;

        printf("i = %f\n", i);               \\用浮点型格式化输出整return 0;
}
[root@PC1 test]# gcc test.c -o kkk
[root@PC1 test]# ls
kkk  test.c
[root@PC1 test]# ./kkk                       ## 返回值是浮点型0;
i = 0.000000
[root@PC1 test]# ./kkk
i = 0.000000

 。

 

posted @ 2024-08-14 10:58  小鲨鱼2018  阅读(1)  评论(0编辑  收藏  举报