为什么这个程序会无限循环?
Why this program going to infinite loop?
当我在下面的代码中将 i<0,5
放在 for 循环的条件部分时
#include<stdio.h>
int main()
{
int i;
for(i = 0;i<0,5;i++)
printf("%d\n",i);
return 0;
}
答案是 5
总是 true
。
请参考以下从您的代码中反汇编的代码。
条件部分仅指 5
.
move eax, 5
正在将 5 保存到 eax
寄存器。
test eax, eax
正在比较 eax
和 eax
。
它必须始终相同。所以,它永远是真的。
009318FA mov eax,5
009318FF test eax,eax
00931901 je main+56h (0931916h)
而且是完整代码:
int i;
for (i = 0; i < 0, 5; i++)
009318E8 mov dword ptr [i],0
009318EF jmp main+3Ah (09318FAh)
009318F1 mov eax,dword ptr [i]
009318F4 add eax,1
009318F7 mov dword ptr [i],eax
009318FA mov eax,5
int i;
for (i = 0; i < 0, 5; i++)
009318FF test eax,eax
00931901 je main+56h (0931916h)
printf("%d\n", i);
00931903 mov eax,dword ptr [i]
00931906 push eax
00931907 push offset string "%d\n" (0937B30h)
0093190C call _printf (093104Bh)
00931911 add esp,8
00931914 jmp main+31h (09318F1h)
如果您希望循环在 5 次迭代后停止,您必须编写
for(i = 0;i<5;i++)
printf("%d\n",i);
根据melpomene, the comma operation is explained here
的建议
当我在下面的代码中将 i<0,5
放在 for 循环的条件部分时
#include<stdio.h>
int main()
{
int i;
for(i = 0;i<0,5;i++)
printf("%d\n",i);
return 0;
}
答案是 5
总是 true
。
请参考以下从您的代码中反汇编的代码。
条件部分仅指 5
.
move eax, 5
正在将 5 保存到 eax
寄存器。
test eax, eax
正在比较 eax
和 eax
。
它必须始终相同。所以,它永远是真的。
009318FA mov eax,5
009318FF test eax,eax
00931901 je main+56h (0931916h)
而且是完整代码:
int i;
for (i = 0; i < 0, 5; i++)
009318E8 mov dword ptr [i],0
009318EF jmp main+3Ah (09318FAh)
009318F1 mov eax,dword ptr [i]
009318F4 add eax,1
009318F7 mov dword ptr [i],eax
009318FA mov eax,5
int i;
for (i = 0; i < 0, 5; i++)
009318FF test eax,eax
00931901 je main+56h (0931916h)
printf("%d\n", i);
00931903 mov eax,dword ptr [i]
00931906 push eax
00931907 push offset string "%d\n" (0937B30h)
0093190C call _printf (093104Bh)
00931911 add esp,8
00931914 jmp main+31h (09318F1h)
如果您希望循环在 5 次迭代后停止,您必须编写
for(i = 0;i<5;i++)
printf("%d\n",i);
根据melpomene, the comma operation is explained here
的建议