Can I compare an int and a size_t variable like this:
int i = 1;
size_t y = 2;
if (i == y) {
// Do something
}
Or do I have to type cast one of them?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
It’s safe provided the
intis zero or positive. If it’s negative, andsize_tis of equal or higher rank thanint, then theintwill be converted tosize_tand so its negative value will instead become a positive value. This new positive value is then compared to thesize_tvalue, which may (in a staggeringly unlikely coincidence) give a false positive. To be truly safe (and perhaps overcautious) check that theintis nonnegative first:and to suppress compiler warnings: