Simultaneous execution of both if and else blocks

后端 未结 13 2307
难免孤独
难免孤独 2020-12-10 17:45

In C or C++

if ( x )
    statement1;
else
    statement2;

For what value of x will both statements be executed?

I know

13条回答
  •  南方客
    南方客 (楼主)
    2020-12-10 18:03

    If it is a trick question, you could answer with

    if( ({ statement2; 1; }) ) 
      statement1;
    else
      statement2;
    

    Using GCC statement expressions :) For expression statements, there is the comma operator

    if(expr2, 1) 
      expr1;
    else
      expr2;
    

    This is a quite popular question.

提交回复
热议问题