从整型,浮点,CHAR类型转换,双 [英] Typecasting from int,float,char,double

查看:193
本文介绍了从整型,浮点,CHAR类型转换,双的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是想出来做的几个例子,并且不强制类型转换的公司。我不明白为什么以下code段未能输出正确的结果。

  / * INT浮动* /
#包括LT&;&stdio.h中GT;
诠释主(){
        INT I = 37;
        浮动F = *(*浮动)及我;
        的printf(\\ n%F \\ N,F);
        返回0;
}

这版画 0.000000

  / *浮动做空* /
#包括LT&;&stdio.h中GT;
诠释主(){
        浮动F = 7.0;
        短S = *(*浮动)及F;
        的printf(\\ n s键数:%d \\ n,S);
        返回0;
}

这版画 7

  / *从双为char * /
#包括LT&;&stdio.h中GT;
诠释主(){
        双D = 3.14;
        焦炭CH = *(字符*)和D;
        的printf(\\ n CH:%C \\ N,CH);
        返回0;
}

此打印不需要

  / *从短期翻番* /
#包括LT&;&stdio.h中GT;
诠释主(){
        短S = 45;
        双D = *(*双)及S;
        的printf(\\ N D:%F \\ N,D);
        返回0;
}

这版画 0.000000

为什么从浮动 INT 投给正确的结果和所有其它转换给出错误的结果时,类型是明确投?

我无法清楚地了解为什么这种类型转换的(浮点*)需要,而不是浮动

  INT I = 10;
浮动F =(浮点)我; //提供了正确的操作为:10.000

但是,

  INT I = 10;
浮动F = *(*浮动)及我; //给人以0.0000

什么是上述两种类型之间的区别蒙上?

为什么不能我们使用:

 浮动F =(浮动**)及我;
浮动F = *(*浮动)及我;


解决方案

在这个例子:

字符CH = *(字符*)和D;

您不是从铸造双字符。您正在从铸造双* 的char * ;也就是说,你从一个双重的指针的转换为一个字符的指针的。

按c转换浮点型铸造值时整数类型,但因为你是投三分球,而不是那些值,没有完成转换。你得到的垃圾,因为浮点数从固定点编号的存储非常不同。

I was trying out few examples on do's and dont's of typecasting. I could not understand why the following code snippets failed to output the correct result.

/* int to float */
#include<stdio.h>
int main(){
        int i = 37;
        float f = *(float*)&i;
        printf("\n %f \n",f);
        return 0;
}

This prints 0.000000

/* float to short */
#include<stdio.h>
int main(){
        float f = 7.0;
        short s = *(float*)&f;
        printf("\n s: %d \n",s);
        return 0;
}

This prints 7

/* From double to char */
#include<stdio.h>
int main(){
        double d = 3.14;
        char ch = *(char*)&d;
        printf("\n ch : %c \n",ch);
        return 0;
}

This prints garbage

/* From short to double */
#include<stdio.h>
int main(){
        short s = 45;
        double d = *(double*)&s;
        printf("\n d : %f \n",d);
        return 0;
}

This prints 0.000000

Why does the cast from float to int give the correct result and all the other conversions give wrong results when type is cast explicitly?

I couldn't clearly understand why this typecasting of (float*) is needed instead of float

int i = 10;
float f = (float) i; // gives the correct op as : 10.000

But,

int i = 10;
float f = *(float*)&i; // gives a 0.0000

What is the difference between the above two type casts?

Why cant we use:

float f = (float**)&i;
float f = *(float*)&i;

解决方案

In this example:

char ch = *(char*)&d;

You are not casting from double to a char. You are casting from a double* to a char*; that is, you are casting from a double pointer to a char pointer.

C will convert floating point types to integer types when casting the values, but since you are casting pointers to those values instead, there is no conversion done. You get garbage because floating point numbers are stored very differently from fixed point numbers.

这篇关于从整型,浮点,CHAR类型转换,双的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆