Click here to Skip to main content
15,069,020 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
#include <iostream>
using namespace std;

int main()
    if (sizeof(int) > -1)
        cout << "Yes";
        cout << "No";
    return 0;

What I have tried:

according to me the answer should be yes.
ut the correct answer is no.
how is it possibble?
Updated 8-Dec-18 22:21pm

1 solution

Because the sizeof operator returns an unsigned value. So the compiler then treats -1 also as an unsigned value. The hexadecimal value of -1 is 0xFFFFFFFF or unsigned decimal 4294967295, which of course is much greater than the size of an integer (which is 4 (not 8)). One of the pitfalls of mixing signed and unsigned values in software.
Prateek Krishna 7-Dec-18 23:35pm
Thank you Sir !
but size of int is 4 ..
KarstenK 9-Dec-18 4:22am
assign -1 to an uint and see. :-O
Richard MacCutchan 9-Dec-18 7:55am
You are correct but it is just the same.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900