Tricky C Question

This is a discussion on Tricky C Question within the C Programming forums, part of the General Programming Boards category; Code: What is printed by the following program: main() { char x = 0x7F; int y; unsigned int z; y ...

  1. #1
    C is Sea. I know a drop! ganesh bala's Avatar
    Join Date
    Jan 2009
    Location
    Bangalore
    Posts
    58

    Lightbulb Tricky C Question

    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??

  2. #2
    Registered User carrotcake1029's Avatar
    Join Date
    Apr 2008
    Posts
    398
    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?

  3. #3
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    It's undefined as y is signed.

    Stop getting questions from that terrible quiz.

  4. #4
    C is Sea. I know a drop! ganesh bala's Avatar
    Join Date
    Jan 2009
    Location
    Bangalore
    Posts
    58
    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;
    }

  5. #5
    C is Sea. I know a drop! ganesh bala's Avatar
    Join Date
    Jan 2009
    Location
    Bangalore
    Posts
    58

    Thumbs up

    Hi Zacs,

    I am sure Its not undefined behaviour......

  6. #6
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    > 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 :-)

  7. #7
    C is Sea. I know a drop! ganesh bala's Avatar
    Join Date
    Jan 2009
    Location
    Bangalore
    Posts
    58

    Talking

    This question is not from terrible Quiz !!...

  8. #8
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Well techincally none of them are right... not even option d.

  9. #9
    C is Sea. I know a drop! ganesh bala's Avatar
    Join Date
    Jan 2009
    Location
    Bangalore
    Posts
    58
    It seems nothing is wrong with code..

    Can you be little more specific........?

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by ganesh bala View Post
    It seems nothing is wrong with code..

    Can you be little more specific........?
    Except that printf with "%x" is, strictly speaking, undefined if the value is signed.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #11
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    > 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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  12. #12
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  2. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  3. question about reading in strings from a file :>
    By bball887 in forum C Programming
    Replies: 8
    Last Post: 04-13-2004, 06:24 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 12:47 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21