c语言中给int类型变量赋值double型数值; 给double型变量赋值int型数据

 

 

001、 给int型变量赋值double型数据

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

int main(void)
{
        int i;

        i = 8.583;

        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 = 8

 。

 

02、double型变量赋值int值;

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

int main(void)
{
        double i;

        i = 5;                   // double型变量赋值int数值

        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           ## 其结果不受影响
i = 5.000000

 

posted @ 2024-07-28 20:29  小鲨鱼2018  阅读(2)  评论(0编辑  收藏  举报