C Programming - Input / Output - Discussion

Discussion Forum : Input / Output - Find Output of Program (Q.No. 6)
6.
What will be the output of the program ?
#include<stdio.h>

int main()
{
    printf("%c\n", ~('C'*-1));
    return 0;
}
A
B
C
D
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
32 comments Page 1 of 4.

Digvijay said:   3 years ago
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).
(7)

Mohd pv vanimal said:   7 years ago
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.
(2)

Tarun said:   1 decade ago
~n=-(n+1)
'c'*(-1)=67*(-1)=-67
~(-67)=-(-67+1)=66
66='B'
(1)

Prasad said:   1 decade ago
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.

Jitendra said:   1 decade ago
@Tarun is absolutly right.

Chandan kumar said:   1 decade ago
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:   1 decade ago
Why to use 1's compliment? Any one please.

Anu said:   1 decade ago
How 01000010 is 66? Can someone explain please?

Venkat said:   1 decade ago
Here 01000010 = 66 can be like 2^1+2^6 = 2+64 = 66.

Sowmiya said:   9 years ago
How ~67 comes to 66? Can someone explain please.


Post your comments here:

Your comments will be displayed after verification.