in Programming in C edited by
1,986 views
2 votes
2 votes
#include <stdio.h>
int main() {
	unsigned char a = 5;
	a |= (1<<((sizeof(char)<<3)-1));
	char b = a;
	printf("%d %d\n",b,a);
	printf("%u %u\n",b,a);
}

If the size of a char datatype is 1 Byte, then what will be the output?

[Edited]

in Programming in C edited by
by
2.0k views

2 Comments

QS: edited
0
0

If the size of a character is 1 Byte?

1
1

4 Answers

8 votes
8 votes
Best answer
#include <stdio.h>
int main() {
	unsigned char a = 5;
	a |= (1<<((sizeof(char)<<3)-1));
	char b = a;
	printf("%d %d\n",b,a);
	printf("%u\n",a);
}


  • a |= (1<<((sizeof(char)<<3)-1));
    

$a |= (1<<(1<<3)-1) = 133$ $\rightarrow$ $a =133$

  • Since, $b$ is a signed char and equal to $a$, hence value of $b\rightarrow 133 \mod\ 256 \rightarrow -123$.
  • Printf here is printing signed int values and unsigned, hence

printf("%d %d\n",b,a); // b= -123, a=133
printf("%u %u\n",b,a); // b=4294967173,a=133
selected by
by

4 Comments

@Debashish

what is meaning of it of stack overflow

" If the function is defined with a type that includes a prototype, and either the prototype ends with an ellipsis (, ...) or the types of the arguments after promotion are not compatible with the types of the parameters, the behavior is undefined. "

0
0

@Kapil

cannot understand

" in hexa :- 0000007B and now take its 2's complement, you will get the result as unsigned "

0
0
@Srestha,

I meant $24$ zeros and then $123$ in $8$ bit binary as size of integer on your system presumably = $32$ bits.

Then, for -ve values, we take $2$'s complement right ? It is a much simpler way and others are also there .
–1
–1
1 vote
1 vote
-123,-123

133

 

a=a|=(1<<((sizeof(char)<<3)-1));

a|=(1<<((1<<3)-1));

a|=(1<<(8-1));

a|=(1<<7);

a|=128;

a=a|128;//both are in unsigned

a=133;

now,

b=a;//b=-123 it is in signed

print a,b; // a=-123(bz printing the number with signed format specifier) b=-123

print a;//a=133;

2 Comments

no padding involvement ?
0
0
Sir ,

i am wrong it will print

-123,133

133

i am unable to get the reason of printing 133 in 1st line
0
0
0 votes
0 votes
int main() {
	unsigned char a = 5;
	a |= (1<<((sizeof(char)<<3)-1));
	char b = a;
	printf("%d %d\n",b,a);
	printf("%u %u\n",b,a);
}

carefully ! Here unsigned is used so range will get modified from 32/64 machine to machine.

(1<<((sizeof(char)<<3)-1))

          (1<<((1<<3)-1))  ==> (1<<((8)-1)) ==> (1<<(8-1)) ==>(1<<7) so,shifting 00000001 become 10000000(128)

now, a |= (1<<((sizeof(char)<<3)-1)); //here  | sign means OR so a value is 5  a= a | 128

        a= 00000101 | 10000000 

       a=133

      now, we know 

Type Storage size Value range
char 1 byte -128 to 127 or 0 to 255
unsigned char 1 byte 0 to 255

       so, b(SIGNED INT)=a(UNSIGNED INT);

      so b=133%256; ==> b = -123;

printf("%d %d\n",b,a); //OUTPUT:- -123 133

printf("%u %u\n",b,a); //NOTE %u for unsigned use as value at address whereas for signed address so

                                          //OUTPUT:- ANY MEMORY ADDRESS  133

for more see http://code.geeksforgeeks.org/pDibZO

edited by
0 votes
0 votes
print :5,-123

print : 5,133

Related questions