Explain how??Code:What is printed by the following program: main() { char x = 0x7F; int y; unsigned int z; y = x; z = x; printf("%x %x\n", y, z); } a)ffffff7f ffffff7f b)7f 7f c)ffffff7f 7f d)None of the above
Explain how??Code:What is printed by the following program: main() { char x = 0x7F; int y; unsigned int z; y = x; z = x; printf("%x %x\n", y, z); } a)ffffff7f ffffff7f b)7f 7f c)ffffff7f 7f d)None of the above
Why don't you compile it yourself? Really all it seems to be asking is how is the output formatted. What specifically is your question?
It's undefined as y is signed.
Stop getting questions from that terrible quiz.
value of signed char variable assigned to unsigned int
and to signed int.....
[ it seems just printing the value copied.. 7f 7f]...
Try this question...
output wont be FF ..... It will be different...
Code:#include<stdio.h> int main() { char x = 0xFF; unsigned int y; int z; y = x; z = x; printf("%x %x\n", y, z); getchar(); return 0; }
Hi Zacs,
I am sure Its not undefined behaviour......
> I am sure Its not undefined behaviour...
Look at the standard, especially about signed data types and what "%x" means.
It is well defined that this is undefined :-)
This question is not from terrible Quiz !!...
Well techincally none of them are right... not even option d.
It seems nothing is wrong with code..
Can you be little more specific........?
> value of signed char variable assigned to unsigned int
Well it's implementation specific as to whether a 'char' by itself is naturally signed or unsigned.
0x7F wouldn't matter either way, but 0xFF would be a whole new story.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
In the assignment y = x; y is a signed int and x being a char it is promoted to a signed int throught sign bit extension ie the leftmost bit of the data size code. So as Salem pointed out 0x7F won't matter but it would if x lies between 0x80 and 0xFF inclusive.