C Programming - Input / Output - Discussion

6. 

What will be the output of the program ?

#include<stdio.h>

int main()
{
    printf("%c\n", ~('C'*-1));
    return 0;
}

[A]. A
[B]. B
[C]. C
[D]. D

Answer: Option B

Explanation:

No answer description available for this question.

Hariraj said: (Aug 9, 2010)  
ASCII Value of 'C' is 67.

67*-1=-67

applying bitwise not(~) to -67 will result in 66, which is the ASCII value of 'B'.

Nik said: (Nov 3, 2010)  
How come bitwise not (~) to -67 is 66 , why not 67 itself?

Satya said: (Nov 6, 2010)  
@Nik because c language follows the one's complement signed number representation

Mah said: (Dec 15, 2010)  
Thank you hariraj.

Chaudhary Paresh said: (Dec 19, 2010)  
('C'*-1)--> ASCII Value of C equal to 67 so statement become(67*-1)=-67
~ indicate one's complement in c
~(-67)=66 so 66 ASCII value of character 'B'

Nita said: (Feb 15, 2011)  
Hariraj is right.

Dhan said: (Jul 1, 2011)  
For -67 we can calculate the binary value by:

First find binary value for 67
67=01000011
take 1's complement:
that is make 0's complement and add 1
10111100+1=10111101

Vivek said: (Jul 21, 2011)  
For using ~
first we will add 1 to the number and then we will change the sign.
for ex:
-67
-67+1=-66
and now we will change the sign that is equal to 66
how we use this sign ~.

Sindhu said: (Jul 25, 2011)  
Is ~ of all negative numbers result in positive numbers whose value is one less than its negative value?

Rhana said: (Sep 3, 2011)  
Hariraj is right.

Balaji said: (Sep 10, 2011)  
-67 is stored in 2's complement form in computer.

Anil said: (Oct 24, 2011)  
Hariraj is right.

Vallabh said: (Dec 14, 2011)  
ASCII value of 'C'=67 (01000011)
67*-1=-67
in C negative values are treated with 2's compliment
so its 1's compliment is 10111100
and its 2's compliment is 10111101

Now apply ~ to its 2's compliment and it becomes 01000010 that is nothing but the ASCII value of B(66).

Diya said: (Dec 22, 2011)  
What does this * in 67*-1 mean?

Tarun said: (Mar 13, 2012)  
~n=-(n+1)
'c'*(-1)=67*(-1)=-67
~(-67)=-(-67+1)=66
66='B'

Swati said: (May 4, 2012)  
Vallabh is right.

Jitendra said: (Sep 1, 2012)  
@Tarun is absolutly right.

Prasad said: (Oct 2, 2012)  
Hey just to avoid confusion.

(<number>*-1)= multiplying <number> by -1 = 2's compliment.

~(number)= 1's compliment.(just inverting the binary digit)

Hope this helps.

Chandan Kumar said: (Aug 9, 2013)  
Hope it will help you.

67*-1 = -67;

Binary representation of 67: 01000011.

-67 can be represented as 2's complement of 67.

So we will compute 2's complement of 67.

1's complement of 01000011 is 10111100.

After adding 1 in the result we get: 10111101. (2's complement of 67 i.e -67)
~ (10111101) = 01000010(66).

66 is ASCII value of B so on execution of the program B is printed.

Prashanth said: (Nov 11, 2014)  
Why to use 1's compliment? Any one please.

Anu said: (Mar 30, 2015)  
How 01000010 is 66? Can someone explain please?

Venkat said: (May 21, 2015)  
Here 01000010 = 66 can be like 2^1+2^6 = 2+64 = 66.

Sowmiya said: (Jul 19, 2016)  
How ~67 comes to 66? Can someone explain please.

Alok said: (Sep 10, 2016)  
@Vallabh is correct.

Rahul said: (Nov 26, 2016)  
Thank you all for such a wonderful help. It is very helpful.

Jayant said: (Jun 24, 2017)  
You are right @Vallabh.

Aminul said: (Aug 20, 2017)  
Yes, Right @Venkat.

Pallavi said: (Nov 23, 2017)  
Agree @Venkat.

Mayank said: (Dec 3, 2017)  
Very nice explanation @Vallabh.

Mohd Pv Vanimal said: (May 11, 2018)  
include<stdio.h>
int main()
{
printf("%d\n", 'C'); //67
printf("%d\n", 'C' * -1); //-67
printf("%d\n", ~('C' * - 1)); //66
printf("%c\n",~('C' * -1)); //B
return 0;
}

In 2's complement, the value of ~(-67) is 66.

Noureen said: (Oct 16, 2018)  
Very well explained, Thanks @Mohd Pv Vanimal.

Digvijay said: (Jun 4, 2022)  
ASCII value of 67 is (01000011).

67*-1= -67.


In C negative values are treated with 2's complement so its 1's complement is 10111100 + 1.
Its 2's complement is 10111101.

Now apply ~ to its 2's complement.

Therefore it becomes- 01000010.
It's the ASCII value of B(66).

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.