类型转换会在给定代码中发生吗?
will type conversion take place in given code?
void main()
{
printf("%f",12/7.0);
getch();
}
类型转换是否会在此代码中发生?如果是,那么为什么会发生,如果否,为什么不会在此代码中发生,请解释一下?另外我认为 7.0 在这里是双数据类型(根据类型转换规则)和 12 是整数数据类型会给出数据类型 double 但是当我用 %lf 打印时屏幕上的输出又是 float 我不不懂请指正?
不能通过打印结果推断出表达式的类型。 printf
可能只显示部分值...
man printf
说 f
或 F
格式(强调是我的):
fF
The double argument is rounded and converted to decimal notation in the style [-]ddd.ddd
, where
the number of digits after the decimal-point character is equal to the precision specification.
If the precision is missing, it is taken as 6; if the precision is explicitly zero, no decimal-
point character appears. If a decimal point appears, at least one digit appears before it.
12 是 int
文字,7.0
是 double
文字。根据表达式评估规则,int
将被提升为 double
,结果将是一个 double
,根据格式打印(f
不适用于 float
但 double
).
如果要打印由 float
除法计算的值,您需要使用以下方法限制为 float
:
12/7.0f
结果将是 float
,您可能会问 为什么我要使用 double
说明符? 因为,在任何可变参数函数中,每个 float
将晋升为 double
。由于 printf
是可变函数...
这就是 printf
.
的格式字符串中没有 float
说明符的原因
void main()
{
printf("%f",12/7.0);
getch();
}
类型转换是否会在此代码中发生?如果是,那么为什么会发生,如果否,为什么不会在此代码中发生,请解释一下?另外我认为 7.0 在这里是双数据类型(根据类型转换规则)和 12 是整数数据类型会给出数据类型 double 但是当我用 %lf 打印时屏幕上的输出又是 float 我不不懂请指正?
不能通过打印结果推断出表达式的类型。 printf
可能只显示部分值...
man printf
说 f
或 F
格式(强调是我的):
fF
The double argument is rounded and converted to decimal notation in the style
[-]ddd.ddd
, where the number of digits after the decimal-point character is equal to the precision specification. If the precision is missing, it is taken as 6; if the precision is explicitly zero, no decimal- point character appears. If a decimal point appears, at least one digit appears before it.
12 是 int
文字,7.0
是 double
文字。根据表达式评估规则,int
将被提升为 double
,结果将是一个 double
,根据格式打印(f
不适用于 float
但 double
).
如果要打印由 float
除法计算的值,您需要使用以下方法限制为 float
:
12/7.0f
结果将是 float
,您可能会问 为什么我要使用 double
说明符? 因为,在任何可变参数函数中,每个 float
将晋升为 double
。由于 printf
是可变函数...
这就是 printf
.
float
说明符的原因