1,402 views
1 votes
1 votes
#include <stdio.h>
main (){
unsigned x = -10;
int X = 20;
if (X > x)
      printf  ("Hello");
else{
      printf ("%d",x);
      printf ("Jello");
      }
}

1 Answer

Best answer
2 votes
2 votes

Yeah very good question and thanks for asking ...

In the above example, I am assuming the size of the integer is a 4 bytes (32 bit). Let us assume that the compiler represents signed negative integers number in 2’s complement notation. when casting the negative integer number than no bits will be changed the only compiler treat the stored bits as the unsigned integer.  

Question

Now -10 will be stored as 2's complement form and after unsigned the  value of the variable stored in 2's complement does not changed only type is changed.

if i take only 8 bit to represent -10 then 

2's complement of -10:= 11110110

Now after unsigned it is (11110110) in decimal 246.

x=246 

X=20

if(X>x) is now false then else block will be executed .

Because u have use

 printf("%d" ,x) which is used to get integer values . so if u replace it with %ud then u will get the point .

#include <stdio.h>
int main(void)
{
int i = -6;
unsigned int ui = (unsigned int)i;
printf("%d %ud\n",ui,ui);
return 0;
}

 

selected by

Related questions

0 votes
0 votes
2 answers
1
0 votes
0 votes
1 answer
3
Desert_Warrior asked May 16, 2016
509 views
#include<stdio.h int a = 10; int main() { fun(); fun(); return 0; } int fun() { static int a = 1; printf("%d ",a); a++; return 0; }
0 votes
0 votes
1 answer
4
Desert_Warrior asked May 16, 2016
591 views
#include<stdio.h #include<stdlib.h int main() { char s[] = "Opendays2012"; int i = 0; while(*(s++)) i++; printf("%d",i); return 0; }(a) Segmentation Fault (b) Compile Err...