![](/img/trans.png)
[英]scanf catches char type string with \0 and bunch of zeros after in C
[英]C Double type displays zeros after point
我有这个奇怪的问题。 当我写一个 double 类型的数字时,它被存储在
double data;
多变的。 然后,当我编译并运行代码时,当我编写3697.47595
3697.0000
这是代码:
#include <stdio.h>
const int n = 12;
int stack[n], top = -1;
double Read()
{
int x = 0;
if(top < 0)
printf("Empty stack!");
else
{
x = stack[top];
top--;
}
return x;
}
void Write(int x)
{
if(top == n-1)
printf("The stack is full");
else
{
top++;
stack[top] = x;
}
}
int main()
{
double data;
printf("Enter a double (0 for quit): ");
scanf("%lf", &data);
while(data > 0)
{
Write(data);
printf("Enter a double (0 for quit): ");
scanf("%lf", &data);
}
printf("---------------------------");
printf("\nThe stack contains:\n");
while(top >= 0 )
printf("%lf ", Read());
return 0;
}
该值被截断为int
,因为数组stack
的元素、 Write
的参数以及用于将值临时存储在Read
中的变量x
都是int
。 为他们使用double
来处理double
值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.