看代码,看到了for(;;),然后想到为什么不写成while(1),于是做了下面的测试。网上有解释,因为while需要做判断,理论上执行时间会更长,for(;;)只是执行了两条空语句,执行起来会更快for.c#includeintmain(){for(;;)printf("Thisaloop\n");return0;}while.c#includeintmain(){while(1)printf("Thisaloop\n");return0;}goto.c#includeintmain(){start:printf("Thisisaloop\n");gotostart;return0;}执行gcc-Sxxx.c得到三个文件for.s.file"for.c".c".text.section.rodata.LC0:.string"Thisisaloop".text.globlmain.typemain,@functionmain:.LFB0:.cfi_startprocpushq%rbp.cfi_def_cfa_offset16.cfi_offset6,-16movq??%rsp,%rbp.cfi_def_cfa_register6.L2:leaq.LC0(%rip),%rdcallputs@PLTjmp.L2.cfi_endproc.LFE0:.sizemain,.-main.ident"GCC:(Ubuntu7.5.0-3ubuntu1~18.04)7.5.0".section.note.GNU-堆栈,"",@progbitswhile.s.file"while.c".text.section.rodata.LC0:.string"Thisisaloop".text.globlmain.typemain,@functionmain:.LFB0:.cfi_startprocpushq%rbp.cfi_def_cfa_offset16.cfi_offset6,-16movq??%rsp,%rbp.cfi_def_cfa_register6.L2:leaq.LC0(%rip),%rdcallputs@PLTjmp.L2.cfi_endproc.LFE0:.sizemain,.-main.ident"GCC:(Ubuntu7.5.0-3ubuntu1~18.04)7.5.0"部分.note.GNU-stack,"",@progbitsgoto.s.file"goto.c".text.section.rodata.LC0:.string"Thisaloop".text.globlmain.typemain,@functionmain:.LFB0:.cfi_startprocpushq%rbp.cfi_def_cfa_offset16.cfi_offset6,-16movq??%rsp,%rbp.cfi_def_cfa_register6.L2:leaq.LC0(%rip),%rdcallputs@PLTjmp.L2.cfi_endproc.LFE0:.sizemain,.-main.ident"GCC:(Ubuntu7.5.0-3ubuntu1~18.04)7.5.0".section.note.GNU-stack,"",@progbitsgccversiongcc(Ubuntu7.5.0-3ubuntu1~18.04)7.5.0Copyright(C)2017FreeSoftwareFoundation,Inc.Thisisfreesoftware;查看复制条件的来源。没有任何保证;注意即使是适销性或特定用途的适用性。经过上面的测试,我打开我的keil软件,发现生成的两个机器码是一样的。所以,如果你在项目中遇到这样的写法,不要觉得奇怪,都没事。只是for(;;)看起来更优雅。还有一种情况,while(1)中的1是常量。在某些编译器中,如果设置的检查规则比较高,会提示警告,但是for(;;)不会有这个问题,因为没有变量,也没有常量。