Thread: unsigned char 128

  1. #1
    Registered User
    Join Date
    Jun 2009
    Posts
    2

    unsigned char 128

    Hello,

    I am having troubles with char data type I am using 128 to intiallize it to my variable --> char x = 128; and printing it as an unsigned type (%u) I understand that when printing it as a %u it is printed as unsigned decimal integer. I have tried printf("%u",(unsigned char)x); and it does print 128 but I want to be able to print it without casting in the printf statement. Any help/feedback/thoughts would be very much appreciated.

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    If it's defined as a char, it will be passed to printf() as a char. There is no way to avoid the cast in this case. The problem is that you're trying to store 128 into a signed char, when a signed char is not capable of holding such a value.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  3. #3
    Registered User
    Join Date
    Jun 2009
    Posts
    2
    Thanks brewbuck I knew that signed char had a range -128 to 127, was trying to see if there is workaround out there. But thank you very much for the reply.

  4. #4
    Banned ಠ_ಠ's Avatar
    Join Date
    Mar 2009
    Posts
    687
    whats the point of a signed char anyway?
    ╔╗╔══╦╗
    ║║║╔╗║║
    ║╚╣╚╝║╚╗
    ╚═╩══╩═╝

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by ಠ_ಠ View Post
    whats the point of a signed char anyway?
    What's the point of a signed int anyway?


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Banned ಠ_ಠ's Avatar
    Join Date
    Mar 2009
    Posts
    687
    Quote Originally Posted by quzah View Post
    What's the point of a signed int anyway?


    Quzah.
    oh, they are visible characters... I didn't know that
    ╔╗╔══╦╗
    ║║║╔╗║║
    ║╚╣╚╝║╚╗
    ╚═╩══╩═╝

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You are aware that characters are just tiny ints, right? You aren't forced to interpret them as displayable characters. For example, this is perfectly valid:
    Code:
    char x;
    for( x = 0; x < 10; x++ )
        printf( "Yay!\n" );
    You are confusing a data type, with a character set.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 08-11-2008, 11:02 PM
  2. C problem with legacy code
    By andy_baptiste in forum C Programming
    Replies: 4
    Last Post: 05-19-2008, 06:14 AM
  3. How do i un-SHA1 hash something..
    By willc0de4food in forum C Programming
    Replies: 4
    Last Post: 09-14-2005, 05:59 AM
  4. Swap a bit
    By mr_nice! in forum C Programming
    Replies: 7
    Last Post: 03-01-2004, 03:15 AM
  5. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM