rather a stupid question

This is a discussion on rather a stupid question within the C Programming forums, part of the General Programming Boards category; Halo all, I know it might look a simple and stupid question to you all but please help me out ...

  1. #1
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804

    rather a stupid question

    Halo all,
    I know it might look a simple and stupid question to you all but please help me out of this.
    I wonder how the output of the code below can be known in advance
    Code:
    #include <stdio.h>
    int main(void)
    {
    	char ch=-74;
    	printf("%d %c",ch,ch);
    }
    The output is coming out to be
    -74 ||
    I know that ch ranges from -128 to 127 and it's printing -74 correctly but the %c is making me confuse. I found the 2's complement of -74 and matched it with ASCII value but that also didn't match. Isn't my PC storing -74 in its 2's complement form?
    Thanks
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by BEN10 View Post
    I found the 2's complement of -74 and matched it with ASCII value but that also didn't match. Isn't my PC storing -74 in its 2's complement form?
    Thanks
    Negative signed datatypes are stored in two's complement, yes. It does not really make sense, however, to be be looking at the two's complement form of -74 (which is a binary number) and expecting that to add any new meaning. It is still -74 for all intents and purposes.

    You may have noticed -74 is not an ascii value. Char values less than zero are used in multibyte characters, which are not part of ascii. Since a multibyte character is several sequential values (not just a single char), the %c represented by -74 is not very meaningful either...
    Last edited by MK27; 06-25-2009 at 11:38 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,263
    Quote Originally Posted by BEN10 View Post
    I found the 2's complement of -74 and matched it with ASCII value but that also didn't match.
    You couldn't have possibly done that, the unsigned char with the same bit pattern as -74 is equal to 182, and 182 is not an ASCII code.

    The character set your computer uses is not ASCII. It simply happens to be the same as ASCII in the range 0-127.

    Isn't my PC storing -74 in its 2's complement form?
    Yes.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  4. #4
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Now I saw that char corresponding to ASCII of 182 is similar to the one which got printed. But how did you get 182?
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by BEN10 View Post
    Now I saw that char corresponding to ASCII of 182 is similar to the one which got printed. But how did you get 182?
    182 is the two's complement of 74.
    Code:
     74 = 01001010
    flip  10110101
    add 1 10110110 = 182
    Note though that there is no ASCII code for 182.

  6. #6
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by tabstop View Post
    182 is the two's complement of 74.
    Code:
     74 = 01001010
    flip  10110101
    add 1 10110110 = 182
    Note though that there is no ASCII code for 182.
    Thanks. Actually I was taking only 7 bits for 74(stupid me).
    The character corresponding to 182 will be printed in my code, which is getting printed.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  7. #7
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by tabstop View Post
    182 is the two's complement of 74.
    Code:
     74 = 01001010
    flip  10110101
    add 1 10110110 = 182
    Note though that there is no ASCII code for 182.
    what about just doing: 256 - 74 = 182?
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  8. #8
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by cpjust View Post
    what about just doing: 256 - 74 = 182?
    By which logic are you doing this? Is this logic applicable for any negative number? Or it's just a fluke for -74?
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by BEN10 View Post
    By which logic are you doing this? Is this logic applicable for any negative number? Or it's just a fluke for -74?
    If you take any (binary) number, and then flip all the 0's and 1's around, and then add those two numbers, you will always get all 1's. (For a char, that's 255, for a 2-byte int 32767, for a 4-byte int 4billion and change.)

  10. #10
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by BEN10 View Post
    By which logic are you doing this? Is this logic applicable for any negative number? Or it's just a fluke for -74?
    Try writing a program to do this for all possible char numbers and see if it works.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Stupid Question Probably
    By Kyrin in forum C Programming
    Replies: 2
    Last Post: 05-07-2006, 01:51 AM
  2. Replies: 7
    Last Post: 11-04-2005, 12:17 AM
  3. Stupid Question
    By digdug4life in forum C++ Programming
    Replies: 22
    Last Post: 05-17-2005, 12:43 PM
  4. stupid, stupid question
    By xelitex in forum C++ Programming
    Replies: 5
    Last Post: 12-22-2004, 08:22 PM
  5. Stupid question: What does Debugger do?
    By napkin111 in forum C++ Programming
    Replies: 6
    Last Post: 05-02-2002, 11:00 PM

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