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

I wanted to know why the following code would work.

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

BUT not with the code below

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

Thanks in advance

+3
source share
2 answers

0 && arr
The above expression is false due to 0, therefore, it is arrnot checked, unlike 1 && arrwhere arrit is necessary to check in order to evaluate the value for the expression.


You must try:

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

Due to short circuit 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 value is false, so the right is never evaluated.

+4
source

Source: https://habr.com/ru/post/1740583/


All Articles