一个小问题 不懂

jdwq33 2012-08-14 02:42:58
#include <stdio.h>

int main()
{
double x, y, z;

x = 1;
z = 3/2;
y = x + z;

printf("%f\n", z);
printf("%f\n", y);

return 0;
}

//在这里的Z为什么等于1,而不是1.5呢???大师能帮忙解决一下吗??摆脱了
...全文
115 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
jdwq33 2012-08-14
  • 打赏
  • 举报
回复
C真是博大精深啊
yehailin11 2012-08-14
  • 打赏
  • 举报
回复
这个的确是很小得问题
manxiSafe 2012-08-14
  • 打赏
  • 举报
回复
在C语言中3/2=1,不会等于1.5的
你要 这样 c=3.0/2 就会转换成浮点型的结果
xiaoguailong3 2012-08-14
  • 打赏
  • 举报
回复
你可以用:z = 3.0/2.0
xiaoguailong3 2012-08-14
  • 打赏
  • 举报
回复
你可以用:z = 3.0/2.0
赵4老师 2012-08-14
  • 打赏
  • 举报
回复
x = 1.0;
z = 3.0/2.0;
常量也有类型:
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 );


jdwq33 2012-08-14
  • 打赏
  • 举报
回复
对的 谢谢大师帮忙解惑啊 呵呵
Corner 2012-08-14
  • 打赏
  • 举报
回复
因为3和2都是int型的,计算结果也是int型,然后转换成double赋值给z,所以结果是1。如下修改后就是1.5了

double x, y, z;

x = 1;
z = (double)3/2;
y = x + z;

printf("%f\n", z);
printf("%f\n", y);

69,369

社区成员

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

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