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 @   小鲨鱼2018  阅读(28)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
历史上的今天:
2022-07-27 R语言中对数据框进行排序
2022-07-27 R语言中inner_join、left_join、right_join、full_join函数
2022-07-27 R语言中如何删除重复行
2022-07-27 R语言中如何读取带有#注释的数据
2022-07-27 R语言读取数据如何跳过文件的前几行
2022-07-27 R语言中如何读取列数不一致的数据
2022-07-27 GWAS分析 一般线性模型GLM + 协变量中meta、se、T、p值的计算
点击右上角即可分享
微信分享提示