c中int型和浮点型的格式话输出

 

001、

[root@PC1 test]# ls
test.c
[root@PC1 test]# cat test.c                     // 测试程序
#include <stdio.h>

int main(void)
{
        int i = 10;

        printf("i1 = %d\n", i);
        printf("i2 = %f\n", i);

        return 0;
}
[root@PC1 test]# gcc test.c -o kkk             // 编译
[root@PC1 test]# ls
kkk  test.c
[root@PC1 test]# ./kkk           // %f不能转换int型数据
i1 = 10
i2 = 0.000000

 

002、

[root@PC1 test]# ls
test.c
[root@PC1 test]# cat test.c               // 测试程序
#include <stdio.h>

int main(void)
{
        int i = 8.888;                   // 把int型给予浮点型数据

        printf("i1 = %d\n", i);          // %d转换
        printf("i2 = %f\n", i);          // %f转换 

        return 0;
}
[root@PC1 test]# gcc test.c -o kkk         // 编译
[root@PC1 test]# ls
kkk  test.c
[root@PC1 test]# ./kkk                     ## int型无法用%f转换
i1 = 8
i2 = 0.000000

 。

 

003、

[root@PC1 test]# ls
test.c
[root@PC1 test]# cat test.c           // 测试程序
#include <stdio.h>

int main(void)
{
        double i = 9.888;

        printf("i1 = %f\n", i);
        printf("i2 = %d\n", i);      // 用%d转换doublereturn 0;
}
[root@PC1 test]# gcc test.c -o kkk
[root@PC1 test]# ls
kkk  test.c
[root@PC1 test]# ./kkk              ## 无法正常转换
i1 = 9.888000
i2 = 2147483634

 。

 

004、

[root@PC1 test]# ls
test.c
[root@PC1 test]# cat test.c                // 测试程序
#include <stdio.h>

int main(void)

{
        double i = 10;

        printf("i1 = %f\n", i);
        printf("i2 = %d\n", i);

        return 0;
}
[root@PC1 test]# gcc test.c -o kkk
[root@PC1 test]# ls
kkk  test.c
[root@PC1 test]# ./kkk              ## %d无法转换double型数据
i1 = 10.000000
i2 = 2147483633

 

 。

 

c语言中不能用%f输出int型数据; 其输出结果为0;

c语言中不能用%d输出double型数据,其输出无规则。 

 

posted @ 2024-07-27 13:37  小鲨鱼2018  阅读(4)  评论(0编辑  收藏  举报