sizeof(int) > -1 | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
9th Aug 2019, 10:06 PM
Mouadh Ben Abdallah
Mouadh Ben Abdallah - avatar
4 Answers
+ 11
The sizeof operator returns a type of size_t. size_t is an unsigned data type. When -1 is converted to an unsigned data type, it will become the largest maximim value for that datatype. For a 32-bit build, that -1 will become 4,294,967,295 which is bigger than 4 (size of an int). In general: don't compare size_t to an int, and especially don't compare size_t to a negative int.
9th Aug 2019, 10:49 PM
King
King - avatar
+ 5
The return type of sizeof operator is size_t data type, the definition of size_t depends on its implementation, but it usually an unsigned type, in Sololearn, I think it's unsigned long long int. Because the return type is an unsigned type, the number -1 will be evaluated as positive. The format of -1 itself is (in Sololearn size_t type) 0xFFFFFFFFFFFFFFFF, which is practically the biggest number it can get. So, 4 is definitely not bigger than that
9th Aug 2019, 10:40 PM
Agent_I
Agent_I - avatar
+ 4
https://code.sololearn.com/cG495gBCZ332/?ref=app this is a solution to prevent the compiler from the logical error if u need more help to understand why you are welcome
12th Aug 2019, 12:58 AM
Kyrillos Akram
Kyrillos Akram - avatar
+ 3
Yes, compare with zero if you like but not a negative number.
10th Aug 2019, 12:42 AM
Sonic
Sonic - avatar