unsigned char

This is a discussion on unsigned char within the C Programming forums, part of the General Programming Boards category; Iv'e seen this many times (or something similar). Code: #define BYTE unsigned char But isn't a char unsigned by default?...

  1. #1
    ---
    Join Date
    May 2004
    Posts
    1,379

    unsigned char

    Iv'e seen this many times (or something similar).
    Code:
    #define BYTE unsigned char
    But isn't a char unsigned by default?

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    6.2.5.15 http://www.open-std.org/jtc1/sc22/wg...69/n869.pdf.gz
    The three types char, signed char, and unsigned char are collectively called
    the character types. The implementation shall define char to have the same range,
    representation, and behavior as either signed char or unsigned char.32)

    32) CHAR_MIN, defined in <limits.h>, will have one of the values 0 or SCHAR_MIN, and this can be
    used to distinguish the two options. Irrespective of the choice made, char is a separate type from the
    other two and is not compatible with either.
    Edit: Wow 4000 posts. And to think Janurary of 2004 I barely had 1000.

  3. #3
    C Programmer Stack Overflow's Avatar
    Join Date
    Apr 2004
    Posts
    477
    Hello,

    Code:
    Definition Type Value
    SCHAR_MIN	signed char	-127
    SCHAR_MAX	signed char	+127
    
    UCHAR_MAX	unsigned char	255
    
    CHAR_MIN	char		"see below"
    CHAR_MAX	char		"see below"
    
    If the value of an object of type char is treated as a signed integer when used in an expression, the value of CHAR_MIN shall be the same as that of SCHAR_MIN and the value of CHAR_MAX shall be the same as that of SCHAR_MAX. Otherwise, the value of CHAR_MIN shall be 0 and the value of CHAR_MAX shall be the same as that of UCHAR_MAX.

    Edit:


    - Stack Overflow
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,548
    > But isn't a char unsigned by default?
    Nope - an unqualified char may be either signed or unsigned.

    And using #define to create a new type is pointless when typedef is both more capable, and safer.
    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.

  5. #5
    ---
    Join Date
    May 2004
    Posts
    1,379
    Thanks. It couldn't be any clearer.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Actually it could be clearer. The values Stack Overflow gave you were the "minimums". The standard defines that the character data types have those minimum values. That is to say, it's perferctly legal as far as ANSI goes, to have a char type whose minimum value is ... wait a minute! Holy Deja Vu Batman!

    Quote Originally Posted by quzah
    Not exactly. The standard defines "minimum values" for each, not fixed numbers.

    A char has the following "minimum values":
    a) The low end is CHAR_MIN, which is either SCHAR_MIN or 0, depending on if it is signed by default or not.
    b) The high end is CHAR_MAX, which is either SCHAR_MAX, or UCHAR_MAX, again depending if it is signed or not.
    A signed char has the following "minimum values":
    a) The low end is SCHAR_MIN, which is -127.
    b) The high end is SCHAR_MAX, which is 127.
    An unsigned char has the following "minimum values":
    a) The low end is 0, because there is no UCHAR_MIN macro.
    b) The high end is UCHAR_MAX which must be ((2^CHAR_BIT)-1).
    And finally, CHAR_BIT must be a minimum of 8.

    Again, these are minimums, so it need not be some set value, just so long as it meets the minimum definitions.


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

  7. #7
    ---
    Join Date
    May 2004
    Posts
    1,379
    Thanks Quzah.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 06-16-2006, 09:23 PM
  2. Obtaining source & destination IP,details of ICMP Header & each of field of it ???
    By cromologic in forum Networking/Device Communication
    Replies: 1
    Last Post: 04-29-2006, 02:49 PM
  3. problem with reading and writing
    By yahn in forum C++ Programming
    Replies: 2
    Last Post: 01-03-2006, 03:38 PM
  4. Swap a bit
    By mr_nice! in forum C Programming
    Replies: 7
    Last Post: 03-01-2004, 02:15 AM
  5. String sorthing, file opening and saving.
    By j0hnb in forum C Programming
    Replies: 9
    Last Post: 01-23-2003, 12:18 AM

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