jeudi 6 juin 2019

what is actual value of variable t of type size_t when it is assigned negative value?(can be unsigned int instead of size_t)

Assigning -1 to variable t of type size_t and checking it's equality with both -1 and 4294967295 (FFFFFFFF, 2's compliment of -1; my system is 64 bit;value may vary with system), then in both the cases, it returns 1 i.e. true.

Code is

int main(){
    unsigned int t = 10;
    cout<<"t = -1: "<<(t==-1);  //checks if t is -1
    cout<<"\nt = 4294967295: "<<(t==4294967295);   //checks if t is 4294967295
    cout<<"\nt: "<<t;    //printing actual value of t
    int p = t;    //typecasting it to int
    cout<<"\np: "<<p;    //printing value of p
}

Actual output is

t = -1: 1
t = 4294967295: 1
t: 4294967295
p: -1

returning 1 for both checks (t==-1) and (t==4294697295) but outputs t = 4294697295 and outputs p = -1. Is it that the variable t is holding two values i.e. -1 and 4294697295. Which is definitely not the case.

Help needed.

Aucun commentaire:

Enregistrer un commentaire