Like Tree1Likes
  • 1 Post By pheininger

char & unsigned char

This is a discussion on char & unsigned char within the C Programming forums, part of the General Programming Boards category; hi, We use %d to print int values and %c to print char values..like this how can we print unsigned ...

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    5

    Lightbulb char & unsigned char

    hi,
    We use %d to print int values and %c to print char values..like this how can we print unsigned char values???

    Thanx.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    5,884
    %c is also used for unsigned char's.

    If you want to print the numeric value (eg print 65 for 'A' in the typical ASCII character set) promote to int or unsigned and use %d or %u respectively.
    Right 98% of the time, and don't care about the other 3%.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    5
    ok..

    If we declare a variable as char then its range would be -128 to 127..
    but if we assign value >127 the variable accepts it and prints the corresponding character as per ASCII value..

    So i'm confused that it won't defer whether the variable is declared as "char" or "unsigned char"

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by fenilshah03 View Post
    ok..

    If we declare a variable as char then its range would be -128 to 127..
    but if we assign value >127 the variable accepts it and prints the corresponding character as per ASCII value..

    So i'm confused that it won't defer whether the variable is declared as "char" or "unsigned char"
    That only happens because many of the library functions internally cast char to unsigned.

  5. #5
    Registered User
    Join Date
    Jun 2010
    Location
    Michigan, USA
    Posts
    138
    Quote Originally Posted by fenilshah03 View Post
    If we declare a variable as char then its range would be -128 to 127..
    This statement may be true on your implementation. But there are several ways in which that statement is "implentation defined".

    First, in C, there are three char types: char, signed char, unsigned char. Whether char is signed or unsigned is implementation defined. Your statement assumes an implementation where char is signed. This is different than the other integer types where, for example, signed int and int are the same type.

    Second, in C, the number of bits in a char, signed char or unsigned char is implementation defined. The macro CHAR_BIT in <limits.h> gives the number of bits. It must be at least 8. Your statement assumes an implementation where CHAR_BIT is 8.

    Third, in C99, signed values have one of three implementation defined representations: two's complement, one's complement, and sign-magnitude. Your statement that the range is -128 to 127 assumes a two's complement implementation for signed char values with CHAR_BIT 8. By the way, there are macros in <limits.h> that give the range of values for signed char, unsigned char, and char: SCHAR_MIN, SCHAR_MAX, UCHAR_MAX, CHAR_MIN, and CHAR_MAX. Using these macros, provides some degree of portability.

    Forth, in C99, there is a "basic execution set" of characters that are required to be positive. It is implementation defined what are the values of other characters stored in char.

    This discussion probably does not help you with your confusion. However, you may want to confirm if any of these implementation defined assumptions is not what your implementation has selected. The most likely one to be different is that the implementation you are using has selected char is to be unsigned.

    You may want to find one of the draft standards or buy a copy of the standard to help you understand your issue. I am always impressed that the volunteers on the standards committee have done such a good job dealing with all the various implementations that pre-dated the ANSI 1989 standard.

    Quote Originally Posted by fenilshah03 View Post
    but if we assign value >127 the variable accepts it and prints the corresponding character as per ASCII value..
    What happens for signed value overflow is "undefined behavior". However, I have found that for two's complement implementations, there is fairly predicable behavior. The language standard does not guaranty this behavior.
    Last edited by pheininger; 03-20-2011 at 02:44 PM.
    Adak likes this.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory Allocation Problems
    By amac in forum C Programming
    Replies: 9
    Last Post: 12-10-2009, 05:49 AM
  2. Replies: 7
    Last Post: 06-16-2006, 09:23 PM
  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. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM
  5. ANY BODY WILLING TO HELP ME WITH Microsoft Visual C++
    By BiG pImPiN fOoL in forum C++ Programming
    Replies: 12
    Last Post: 11-04-2001, 05:03 PM

Tags for this Thread


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