Jellow
Jellow

Reputation: 101

C++ judgment conditional formatting question

#include <iostream>
using namespace std;
int foo(int x,int n);
int main() {
    string s = "hello";
    int k = 0;
    if((k - s.size()) < 0) {
        cout << "yes1";
    }
    int temp = k - s.size();
    if((temp) < 0) {
        cout << "yes2";
    }
}

Anyone can tell me why the output is yes2? Where is the yes1????

Upvotes: 2

Views: 153

Answers (1)

Shad Alam Joy
Shad Alam Joy

Reputation: 76

If you look at if((k-s.size())<0), there was no type casting datatype.

When you write int temp = k-s.size();, integer datatype convert result as integer value(-5). Then your result "yes2".

Change k-s.size() to int(k-s.size()), you will get "yes1" and also "yes2".

Upvotes: 2

Related Questions