如果输出的最后一行没有以换行符结尾,则 标准不保证您的程序会输出任何内容。 但是要回答你的问题,你需要对 浮点数的实现进行一些基础研究。在我的系统上输出以下程序 应该给你一个线索... %类型float.c #include< stdio.h> int main(无效) { 浮动x; for(x = 0; x printf("%0.9f\\\",x); 返回0; } %gcc -ansi -pedantic float.c -o float %浮动 0.000000000 0.100000001 0.200000003 0.300000012 0.400000006 0.500000000 0.600000024 0.700000048 0.800000072 0.900000095 1.000000119 % 浮点计算通常不准确。 - Peter can anybody explain me plz, why this code doesn''t print 1.0??#include <stdio.h>void main(){float x;for (x=0;x<=1.0;x+=0.1)printf("%0.1f ",x);} 解决方案 http://www.eskimo.com/~scs/C-faq/s14.htmlHTH,-ArthurThis seems ok:#include <stdio.h>#include <float.h>int main(void){float x;for(x=0.0f; x <= 1.0f + FLT_EPSILON; x += 0.1f)printf("%1.1f ", x);printf("\n");return 0;}If the last line of output isn''t terminated with a newline character, thestandard offers no guarantee that your program will output anything.But to answer your question, you need to do some basic research on howfloating point numbers are implemented. The output of the following programon my system should give you a clue...% type float.c#include <stdio.h>int main(void){float x;for (x = 0; x <= 1.1; x += 0.1)printf("%0.9f\n",x);return 0;}% gcc -ansi -pedantic float.c -o float% float0.0000000000.1000000010.2000000030.3000000120.4000000060.5000000000.6000000240.7000000480.8000000720.9000000951.000000119%Floating point computation is generally not exact.--Peter 这篇关于为什么不打印1.0?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-15 04:36