I just want to be sure that this C code:
我只是想确定这个C代码:
while(flag==true)
{
}
foo();
does the same as this:
做同样的事情:
while(flag==true);
foo();
6 个解决方案
#1
9
;
alone is a null statement in C.
;单独是C中的空语句。
In your case, {}
or ;
are syntactically needed, but they do the same: nothing
在你的情况下,{};在句法上是需要的,但它们做的是一样的:什么都不做
Related: Use of null statement in C
相关:在C中使用空语句。
#2
3
In addition to the other answers: It's the same thing.
除了其他答案:这是一样的。
But I prefer this:
但我更喜欢这个:
while (condition)
{
}
foo();
over this:
在这:
while (condition);
foo();
because if you forget the semicolon after the while, your code will compile fine but it won't do what you expect:
因为如果您在之后忘记了分号,那么您的代码可以很好地编译,但是它不会做您期望的事情:
while(condition) // ; forgotten here
foo();
will actually be equivalent of:
实际上等于:
while(condition)
{
foo();
}
#3
2
Yes, having an empty body of the loop is equivaled to just while(<some condition>);
是的,循环的空体等于just while( <某个条件> );
#4
2
Yes. A ;
following a control structure (e.g., while
, for
, etc.) that can be followed with a block is treated as if it was followed by an empty block.
是的。一个;可以使用块跟随的控制结构(例如while、for等)被视为后面跟着一个空块。
#5
2
Yes, because when put semicolon after while loop statement that indicate empty body and when the condition becomes false then it goes to the immediate next statement after that loop.
是的,因为当在while循环语句后面加上分号表示空体时,当条件变为false时,它就会转到循环之后的下一个语句。
#6
2
Yes, they are same.
是的,它们是一样的。
You Can Generate The assembly of the code and see for yourself that they produce the same assembly. (Using gcc filename.c -S -masm=intel -o ouputfilename
)
您可以生成代码的程序集,并亲自查看它们是否生成相同的程序集。(使用gcc文件名。c -S -masm=intel -o ouputfilename)
#include<stdio.h>
int foo(void);
int main(){
int flag;
scanf("%d" , &flag);
while(flag==1);
foo();
}
int foo(void){
int x = 2;
return x*x;
}
.LC0:
.ascii "%d\0"
.text
.globl main
.def main; .scl 2; .type 32; .endef
.seh_proc main
main:
push rbp
.seh_pushreg rbp
mov rbp, rsp
.seh_setframe rbp, 0
sub rsp, 48
.seh_stackalloc 48
.seh_endprologue
call __main
lea rax, -4[rbp]
mov rdx, rax
lea rcx, .LC0[rip]
call scanf
nop
.L2:
mov eax, DWORD PTR -4[rbp]
cmp eax, 1
je .L2
call foo
mov eax, 0
add rsp, 48
pop rbp
ret
.seh_endproc
.globl foo
.def foo; .scl 2; .type 32; .endef
.seh_proc foo
foo:
push rbp
.seh_pushreg rbp
mov rbp, rsp
.seh_setframe rbp, 0
sub rsp, 16
.seh_stackalloc 16
.seh_endprologue
mov DWORD PTR -4[rbp], 2
mov eax, DWORD PTR -4[rbp]
imul eax, DWORD PTR -4[rbp]
add rsp, 16
pop rbp
ret
.seh_endproc
.ident "GCC: (x86_64-posix-seh-rev1, Built by MinGW-W64 project) 6.3.0"
.def scanf; .scl 2; .type 32; .endef
And When I Changed while(flag == 1);
to while(flag==1){}
Assembly Code Generated is :
当我改变时(flag = 1)到while(flag==1){}汇编代码生成:
.LC0:
.ascii "%d\0"
.text
.globl main
.def main; .scl 2; .type 32; .endef
.seh_proc main
main:
push rbp
.seh_pushreg rbp
mov rbp, rsp
.seh_setframe rbp, 0
sub rsp, 48
.seh_stackalloc 48
.seh_endprologue
call __main
lea rax, -4[rbp]
mov rdx, rax
lea rcx, .LC0[rip]
call scanf
nop
.L2:
mov eax, DWORD PTR -4[rbp]
cmp eax, 1
je .L2
call foo
mov eax, 0
add rsp, 48
pop rbp
ret
.seh_endproc
.globl foo
.def foo; .scl 2; .type 32; .endef
.seh_proc foo
foo:
push rbp
.seh_pushreg rbp
mov rbp, rsp
.seh_setframe rbp, 0
sub rsp, 16
.seh_stackalloc 16
.seh_endprologue
mov DWORD PTR -4[rbp], 2
mov eax, DWORD PTR -4[rbp]
imul eax, DWORD PTR -4[rbp]
add rsp, 16
pop rbp
ret
.seh_endproc
.ident "GCC: (x86_64-posix-seh-rev1, Built by MinGW-W64 project) 6.3.0"
.def scanf; .scl 2; .type 32; .endef
You can see that the relevant portion is same in both cases.
您可以看到,在这两种情况下,相关的部分是相同的。
//Below Portion is same in both cases.
.L2:
mov eax, DWORD PTR -4[rbp]
cmp eax, 1
je .L2
call foo
mov eax, 0
add rsp, 48
pop rbp
ret
.seh_endproc
.globl foo
.def foo; .scl 2; .type 32; .endef
.seh_proc foo
#1
9
;
alone is a null statement in C.
;单独是C中的空语句。
In your case, {}
or ;
are syntactically needed, but they do the same: nothing
在你的情况下,{};在句法上是需要的,但它们做的是一样的:什么都不做
Related: Use of null statement in C
相关:在C中使用空语句。
#2
3
In addition to the other answers: It's the same thing.
除了其他答案:这是一样的。
But I prefer this:
但我更喜欢这个:
while (condition)
{
}
foo();
over this:
在这:
while (condition);
foo();
because if you forget the semicolon after the while, your code will compile fine but it won't do what you expect:
因为如果您在之后忘记了分号,那么您的代码可以很好地编译,但是它不会做您期望的事情:
while(condition) // ; forgotten here
foo();
will actually be equivalent of:
实际上等于:
while(condition)
{
foo();
}
#3
2
Yes, having an empty body of the loop is equivaled to just while(<some condition>);
是的,循环的空体等于just while( <某个条件> );
#4
2
Yes. A ;
following a control structure (e.g., while
, for
, etc.) that can be followed with a block is treated as if it was followed by an empty block.
是的。一个;可以使用块跟随的控制结构(例如while、for等)被视为后面跟着一个空块。
#5
2
Yes, because when put semicolon after while loop statement that indicate empty body and when the condition becomes false then it goes to the immediate next statement after that loop.
是的,因为当在while循环语句后面加上分号表示空体时,当条件变为false时,它就会转到循环之后的下一个语句。
#6
2
Yes, they are same.
是的,它们是一样的。
You Can Generate The assembly of the code and see for yourself that they produce the same assembly. (Using gcc filename.c -S -masm=intel -o ouputfilename
)
您可以生成代码的程序集,并亲自查看它们是否生成相同的程序集。(使用gcc文件名。c -S -masm=intel -o ouputfilename)
#include<stdio.h>
int foo(void);
int main(){
int flag;
scanf("%d" , &flag);
while(flag==1);
foo();
}
int foo(void){
int x = 2;
return x*x;
}
.LC0:
.ascii "%d\0"
.text
.globl main
.def main; .scl 2; .type 32; .endef
.seh_proc main
main:
push rbp
.seh_pushreg rbp
mov rbp, rsp
.seh_setframe rbp, 0
sub rsp, 48
.seh_stackalloc 48
.seh_endprologue
call __main
lea rax, -4[rbp]
mov rdx, rax
lea rcx, .LC0[rip]
call scanf
nop
.L2:
mov eax, DWORD PTR -4[rbp]
cmp eax, 1
je .L2
call foo
mov eax, 0
add rsp, 48
pop rbp
ret
.seh_endproc
.globl foo
.def foo; .scl 2; .type 32; .endef
.seh_proc foo
foo:
push rbp
.seh_pushreg rbp
mov rbp, rsp
.seh_setframe rbp, 0
sub rsp, 16
.seh_stackalloc 16
.seh_endprologue
mov DWORD PTR -4[rbp], 2
mov eax, DWORD PTR -4[rbp]
imul eax, DWORD PTR -4[rbp]
add rsp, 16
pop rbp
ret
.seh_endproc
.ident "GCC: (x86_64-posix-seh-rev1, Built by MinGW-W64 project) 6.3.0"
.def scanf; .scl 2; .type 32; .endef
And When I Changed while(flag == 1);
to while(flag==1){}
Assembly Code Generated is :
当我改变时(flag = 1)到while(flag==1){}汇编代码生成:
.LC0:
.ascii "%d\0"
.text
.globl main
.def main; .scl 2; .type 32; .endef
.seh_proc main
main:
push rbp
.seh_pushreg rbp
mov rbp, rsp
.seh_setframe rbp, 0
sub rsp, 48
.seh_stackalloc 48
.seh_endprologue
call __main
lea rax, -4[rbp]
mov rdx, rax
lea rcx, .LC0[rip]
call scanf
nop
.L2:
mov eax, DWORD PTR -4[rbp]
cmp eax, 1
je .L2
call foo
mov eax, 0
add rsp, 48
pop rbp
ret
.seh_endproc
.globl foo
.def foo; .scl 2; .type 32; .endef
.seh_proc foo
foo:
push rbp
.seh_pushreg rbp
mov rbp, rsp
.seh_setframe rbp, 0
sub rsp, 16
.seh_stackalloc 16
.seh_endprologue
mov DWORD PTR -4[rbp], 2
mov eax, DWORD PTR -4[rbp]
imul eax, DWORD PTR -4[rbp]
add rsp, 16
pop rbp
ret
.seh_endproc
.ident "GCC: (x86_64-posix-seh-rev1, Built by MinGW-W64 project) 6.3.0"
.def scanf; .scl 2; .type 32; .endef
You can see that the relevant portion is same in both cases.
您可以看到,在这两种情况下,相关的部分是相同的。
//Below Portion is same in both cases.
.L2:
mov eax, DWORD PTR -4[rbp]
cmp eax, 1
je .L2
call foo
mov eax, 0
add rsp, 48
pop rbp
ret
.seh_endproc
.globl foo
.def foo; .scl 2; .type 32; .endef
.seh_proc foo