我發現了一些代碼,我對這個循環如何退出以及它如何工作感到困惑。程序是否產生確定性輸出?這個循環如何結束並且結果是確定性的?
我百思不得其解的原因是:
1. `someArray` is of size 2, but clearly, the loop goes till size 3,
2. The value is deterministic and it always exits `someNumber` reaches 4
能有人請解釋這是怎麼回事?
當我將尖括號<>
放在include的庫名稱周圍時,代碼打印不正確。
#include <stdlib.h>
#include <time.h>
#include <stdio.h>
int main() {
int someNumber = 97;
int someArray[2] = {0,1};
int findTheValue;
for (findTheValue=0; (someNumber -= someArray[findTheValue]) >0; findTheValue++) {
}
printf("The crazy value is %d", findTheValue);
return EXIT_SUCCESS;
}
真的,我會猜測findTheValue會= 0作爲輸出...因爲,someNumber - = someArray [findTheValue]當findTheValue == 0肯定是> 0 ... – Dair 2011-04-16 06:27:11
@anon:循環繼續,直到條件是錯誤的 – Brad 2011-04-16 06:40:33
它可能在您的計算機上每次都有效,但不是確定性的。僅僅因爲它在你運行時工作並不意味着代碼是好的。 – 2011-04-16 09:02:56