What is the output of the following code?
int main(void) { float k = 1; while (k != k + 1) { k = k + 1; } printf(“%g”, k); // %g means output a floating point variable in decimal }
If the program runs indefinitely but produces no output, write INFINITE LOOP as the answer to the question. All of the programs compile and run. They may or may not contain serious errors, however. You should assume that int is four bytes. You should assume that float has the equivalent of six decimal digits of precision. You may round your answer off to the nearest power of 10 (e.g., you can say 1,000 instead of 210 (i.e., 1024)).
我不明白为什么循环会终止。
while(1) { float j = k + 1; if (j == k) break;
才能使其终止。使用优化时,我还需要加上-ffloat-store选项,尽管即使没有-ffloat-store,在-O2或-O3下它也会在2^65左右的某个地方最终终止。在我的机器上运行时间为0.2秒,因此除非marionmaiden的计算机非常老旧,否则我怀疑这不是因为缺乏耐心而导致的 :-) - Steve Jessop