💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
**引言:** **单片机的编程中经常用到while(1)死循环来进行轮寻操作,但分析Linux内核源代码时却经常见到for(;;)作为死循环的条件。** **两者区别:** **区别一** **for(;;)死循环里的两个;;代表两个空语句,编译器一般会优化掉它们,直接进入循环体。** **while(1)死循环里的1被看成表达式,每循环一次都要判断常量1是不是等于零。** **区别二** **同样的代码,编译出的程序代码大小不同。** **示例分析:** **for.c源码:** ~~~ #include <stdio.h> int main(void) { for(;;){ printf("123\n"); } return 0; } ~~~ **while.c源码:** ~~~ #include <stdio.h> int main(void) { while(1){ printf("123\n"); } return 0; } ~~~ **汇编上面的两个程序发现它们的汇编源码完全相同:** ~~~ .file "while.c" .section .rodata .LC0: .string "123" .text .globl main .type main, @function main: pushl %ebp movl %esp, %ebp andl $-16, %esp subl $16, %esp .L2: movl $.LC0, (%esp) call puts jmp .L2 .size main, .-main .ident "GCC: (Ubuntu 4.4.1-4ubuntu8) 4.4.1" .section .note.GNU-stack,"",@progbits ~~~ **这样看来两者似乎没有任何区别,其实不然,编译生成程序的大小不同:** **-rwxr-xr-x 1 book book 8296 2014-06-12 22:32 for -rwxr-xr-x 1 book book 8298 2014-06-12 22:33 while**