强制类型转换问题,带代码,顶帖就给分,求解答,麻烦了,谢谢

lzbang 2013-02-27 10:49:36

main(){
int a,b;
float x;
double y;
a=355.5;
printf("a=%d\n",a);
x=57;
printf("x=%f\n",x);
y=57;
printf("y=%lf\n",y);
b=a+x;
printf("b=%f\n",b);
system("pause");
}

书上这样写,执行b=a+x时,a,x都转换成double型相加,结果为412.000000000000 (小数点后12个0),再换为b的类型int,得412,最后将412赋给b
问题:
1、a为int,x为float,相加的话应该是为什么执行b=a+x时,a,x都转换成double型相加,而不是转成float型相加?
2、为什么最后运行的结果,用dev-c++是57.000000,使用turbo c2.0和3.0的结果却是0.000000,而不是412?
谢谢
...全文
322 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
bookc-man 2013-03-05
  • 打赏
  • 举报
回复
这样也带?????
赵4老师 2013-03-04
  • 打赏
  • 举报
回复
常量也有类型: C++ Integer Constants Integer constants are constant data elements that have no fractional parts or exponents. They always begin with a digit. You can specify integer constants in decimal, octal, or hexadecimal form. They can specify signed or unsigned types and long or short types. Syntax integer-constant : decimal-constant integer-suffixopt octal-constant integer-suffixopt hexadecimal-constant integer-suffixopt 'c-char-sequence' decimal-constant : nonzero-digit decimal-constant digit octal-constant : 0 octal-constant octal-digit hexadecimal-constant : 0x hexadecimal-digit 0X hexadecimal-digit hexadecimal-constant hexadecimal-digit nonzero-digit : one of 1 2 3 4 5 6 7 8 9 octal-digit : one of 0 1 2 3 4 5 6 7 hexadecimal-digit : one of 0 1 2 3 4 5 6 7 8 9 a b c d e f A B C D E F integer-suffix : unsigned-suffix long-suffixopt long-suffix unsigned-suffixopt unsigned-suffix : one of u U long-suffix : one of l L 64-bit integer-suffix : i64 To specify integer constants using octal or hexadecimal notation, use a prefix that denotes the base. To specify an integer constant of a given integral type, use a suffix that denotes the type. To specify a decimal constant, begin the specification with a nonzero digit. For example: int i = 157; // Decimal constant int j = 0198; // Not a decimal number; erroneous octal constant int k = 0365; // Leading zero specifies octal constant, not decimal To specify an octal constant, begin the specification with 0, followed by a sequence of digits in the range 0 through 7. The digits 8 and 9 are errors in specifying an octal constant. For example: int i = 0377; // Octal constant int j = 0397; // Error: 9 is not an octal digit To specify a hexadecimal constant, begin the specification with 0x or 0X (the case of the “x” does not matter), followed by a sequence of digits in the range 0 through 9 and a (or A) through f (or F). Hexadecimal digits a (or A) through f (or F) represent values in the range 10 through 15. For example: int i = 0x3fff; // Hexadecimal constant int j = 0X3FFF; // Equal to i To specify an unsigned type, use either the u or U suffix. To specify a long type, use either the l or L suffix. For example: unsigned uVal = 328u; // Unsigned value long lVal = 0x7FFFFFL; // Long value specified // as hex constant unsigned long ulVal = 0776745ul; // Unsigned long value C++ Floating-Point Constants Floating-point constants specify values that must have a fractional part. These values contain decimal points (.) and can contain exponents. Syntax floating-constant : fractional-constant exponent-partopt floating-suffixopt digit-sequence exponent-part floating-suffixopt fractional-constant : digit-sequenceopt . digit-sequence digit-sequence . exponent-part : e signopt digit-sequence E signopt digit-sequence sign : one of + – digit-sequence : digit digit-sequence digit floating-suffix :one of f l F L Floating-point constants have a “mantissa,” which specifies the value of the number, an “exponent,” which specifies the magnitude of the number, and an optional suffix that specifies the constant’s type. The mantissa is specified as a sequence of digits followed by a period, followed by an optional sequence of digits representing the fractional part of the number. For example: 18.46 38. The exponent, if present, specifies the magnitude of the number as a power of 10, as shown in the following example: 18.46e0 // 18.46 18.46e1 // 184.6 If an exponent is present, the trailing decimal point is unnecessary in whole numbers such as 18E0. Floating-point constants default to type double. By using the suffixes f or l (or F or L — the suffix is not case sensitive), the constant can be specified as float or long double, respectively. Although long double and double have the same representation, they are not the same type. For example, you can have overloaded functions like void func( double ); and void func( long double );
FenDouOsaka 2013-03-04
  • 打赏
  • 举报
回复
C语言本身根据编译,运行的环境不同,对数据类型的定义本身也会不同,自然执行出来的结果也不一样 比如int型,有的环境下是2byte,有的环境下是4byte
zhao4zhong2 2013-03-04
  • 打赏
  • 举报
回复
不要写连自己也预测不了结果的代码
25K纯帅 2013-03-04
  • 打赏
  • 举报
回复
1.在运算时,不同类型要转换成同一类型,自动转换的规则为: double ←float ↑ long ↑ unsigned ↑ int ←char,short 2.这个不太清楚,个人觉得是编译器的原因
东北熊孩子 2013-03-03
  • 打赏
  • 举报
回复
那也应该是有值的啊,不是吗? 我在window下试验的时候是没有值,但是我要是在LINUX下是有值的。 但是值也不是正确的。
东北熊孩子 2013-03-02
  • 打赏
  • 举报
回复
追问 但是问什么执行b=a+x; printf("b=%f\n",b); 运行结果: a=355 x=57.000000 y=57.000000 b=0.000000-------------->这里不应该是0.000000吧。 请按任意键继续. . . 我是在VS2010编译环境运行的结果啊。 求解释啊。
AnYidan 2013-03-02
  • 打赏
  • 举报
回复
引用 7 楼 songchao_2011 的回复:
追问 但是问什么执行b=a+x; printf("b=%f\n",b); 运行结果: a=355 x=57.000000 y=57.000000 b=0.000000-------------->这里不应该是0.000000吧。 请按任意键继续. . . 我是在VS2010编译环境运行的结果啊。 求解释啊。
你的 b 是 int, 在 printf 中按 float 输出
sx666777888 2013-02-28
  • 打赏
  • 举报
回复
顶个,坐等解答
AnYidan 2013-02-28
  • 打赏
  • 举报
回复
First, if either operand is long double, the other is converted to long double. Otherwise, if either operand is double, the other is converted to double. Otherwise, if either operand is float, the other is converted to float. Otherwise, the integral promotions are performed on both operands; then, if either operand is unsigned long int, the other is converted to unsigned long int. Otherwise, if one operand is long int and the other is unsigned int, the effect depends on whether a long int can represent all values of an unsigned int; if so, the unsigned int operand is converted to long int; if not, both are converted to unsigned long int. Otherwise, if one operand is long int, the other is converted to long int. Otherwise, if either operand is unsigned int, the other is converted to unsigned int. Otherwise, both operands have type int.
赵4老师 2013-02-28
  • 打赏
  • 举报
回复
对学习编程者的忠告: 眼过千遍不如手过一遍! 书看千行不如手敲一行! 手敲千行不如单步一行! 单步源代码千行不如单步对应汇编一行! VC调试时按Alt+8、Alt+7、Alt+6和Alt+5,打开汇编窗口、堆栈窗口、内存窗口和寄存器窗口看每句C对应的汇编、单步执行并观察相应堆栈、内存和寄存器变化,这样过一遍不就啥都明白了吗。 对VC来说,所谓‘调试时’就是编译连接通过以后,按F10或F11键单步执行一步以后的时候,或者在某行按F9设了断点后按F5执行停在该断点处的时候。 (Turbo C或Borland C用Turbo Debugger调试,Linux或Unix下用GDB调试时,看每句C对应的汇编并单步执行观察相应内存和寄存器变化。) 提醒: “学习用汇编语言写程序” 和 “VC调试(TC或BC用TD调试)时按Alt+8、Alt+7、Alt+6和Alt+5,打开汇编窗口、堆栈窗口、内存窗口和寄存器窗口看每句C对应的汇编、单步执行并观察相应堆栈、内存和寄存器变化,这样过一遍不就啥都明白了吗。 (Linux或Unix下可以在用GDB调试时,看每句C对应的汇编并单步执行观察相应内存和寄存器变化。)” 不是一回事! 不要迷信书、考题、老师、回帖; 要迷信CPU、编译器、调试器、运行结果。 并请结合“盲人摸太阳”和“驾船出海时一定只带一个指南针。”加以理解。 任何理论、权威、传说、真理、标准、解释、想象、知识……都比不上摆在眼前的事实! 有人说一套做一套,你相信他说的还是相信他做的? 其实严格来说这个世界上古往今来所有人都是说一套做一套,不是吗? 不要写连自己也预测不了结果的代码! 电脑内存只是一个一维二进制字节数组及其对应的二进制地址; 人脑才将电脑内存中的这个一维二进制字节数组及其对应的二进制地址的某些部分看成是整数、有符号数/无符号数、浮点数、复数、英文字母、阿拉伯数字、中文/韩文/法文……字符/字符串、函数、函数参数、堆、栈、数组、指针、数组指针、指针数组、数组的数组、指针的指针、二维数组、字符点阵、字符笔画的坐标、黑白二值图片、灰度图片、彩色图片、录音、视频、指纹信息、身份证信息……
lwouyang 2013-02-28
  • 打赏
  • 举报
回复
引用 1 楼 ForestDB 的回复:
所有与浮点相关的,最后都会转成double进行运算,float+float也是如此。 第二个问题涉及到浮点数和整数的内存表示了,感兴趣可以看 1、补码(整数的内存表示); 2、IEEE 754(浮点数的内存表示); 3、函数调用时,压参方式; 4、可变参数的实现原理。
赞同。UP!UP! 补充一点,C语言由于历史原因,float类型在参与任何运算或参数传递时都自动转为double类型。这也说明了为什么printf没有%f和%lf之分。 参考C语言 printf为什么酱紫?
ForestDB 2013-02-28
  • 打赏
  • 举报
回复
所有与浮点相关的,最后都会转成double进行运算,float+float也是如此。 第二个问题涉及到浮点数和整数的内存表示了,感兴趣可以看 1、补码(整数的内存表示); 2、IEEE 754(浮点数的内存表示); 3、函数调用时,压参方式; 4、可变参数的实现原理。
q422013 2013-02-28
  • 打赏
  • 举报
回复
pirntf("%f",(float)a);这样?

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧