0

I have:

#include <iostream>
int main()
{
    static int i, arr[10];
    cout<<(i==0) && (arr[i]<0);
} 

Which means that both i and all of the elements oft are automatically initialized with 0. Why does this expression (i==0) && (t[i]<0) returns true? Even this returns true:

#include <iostream>
int main()
{
    static int i;
    cout<<(i==0) && (i==1);
}

I got confused when I red this question which supposedly has the correct answer a:

  1. Given the declarations:

static int i, t[10];

and assuming that neither i nor t are explicitly initialized, the value of the expression (i==0) && (t[i]<0)

(a) is 1

(b) is 0

(c) depends on the context

Alexandru Cimpanu
  • 1,029
  • 2
  • 14
  • 38

1 Answers1

8

Your problem is with operator precedence. The && operator is evaluated after <<. Thus your print expression becomes: (cout<<(i==0)) && (i==1);. Correct the precedence and it prints 0 as expected: cout<<((i==0) && (i==1));

Dark Falcon
  • 43,592
  • 5
  • 83
  • 98