-2
#include <bits/stdc++.h>
using namespace std;


int main() {
    vector<int> nums(4, -1);
    
    cout << nums.size() << endl;
    
    cout << (-1 < nums.size()) << endl;

    return 0;
}

In the above code the output is

4
0

I was expecting the output as

4
1

How can the boolean evaluate to 0 as 4 is clearly greater than -1;

  • 3
    turn on your compiler warnings – 463035818_is_not_an_ai Aug 04 '23 at 14:15
  • 3
    Hint 1: what type is `nums.size()`? Hint 2: it's not `int`. Hint 3: [`-Wall`](https://godbolt.org/z/cEjPdKKbW) – Brian61354270 Aug 04 '23 at 14:16
  • 4
    You may also be interested in reading [Why should I not #include ?](https://stackoverflow.com/q/31816095/11082165) and [Why is "using namespace std;" considered bad practice?](https://stackoverflow.com/q/1452721/11082165) and [Why should I always enable compiler warnings?](https://stackoverflow.com/q/57842756/11082165) – Brian61354270 Aug 04 '23 at 14:17
  • 1
    You are mixing unsigned values `nums.size()` with signed values `-1`. Surprising things can happen if you do that. In this case the signed value is converted to the equivalent unsigned value and that value is definitely bigger than 4. – john Aug 04 '23 at 14:46

0 Answers0