开发者

code crashes when int arr = 1 && arr; but not int arr = 0 && arr;

I wanted to know why the following code crashes.

int main( ) 
{  
    int arr = 1 && arr;
    return 0; 
}

BUT not with the below code

int main( ) 
{  
    int arr = 0 && arr;
   开发者_运维百科 return 0; 
}

Thanks in advance


0 && arr
The above expression is false because of 0, so arr is not checked unlike 1 && arr where arr has to be checked to evaluate the value for expression.


You should try:

int main(){
  int a = 0 && printf("a"); //printf returns number of characters printed, 1
  int b = 1 && printf("b");
  return 0;
} 


Because of short circuit evaluation of boolean expressions. In the first example the left side of the && operator evaluates to true so the right is evaluated. In the second case the left is false so the right is never evaluated.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜