Does while(1){} do the same as while(1);

佐手、 提交于 2019-12-10 18:28:53

问题


I just want to be sure that this C code:

while(flag==true)
{
} 
foo();

does the same as this:

while(flag==true);

foo();

回答1:


; alone is a null statement in C.

In your case, {} or ; are syntactically needed, but they do the same: nothing

Related: Use of null statement in C




回答2:


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:


Yes, having an empty body of the loop is equivaled to just while(<some condition>);




回答4:


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.




回答5:


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. 




回答6:


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)

#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 :


.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


来源:https://stackoverflow.com/questions/42534523/does-while1-do-the-same-as-while1

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!