Thread: unsigned and signed char

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    14

    unsigned and signed char

    It feels like I'm drowning in a glass of water...

    Am I missing something or according to the standard does this have undefined behaviour when working with non ASCII chars?
    Code:
    //...
    int i = fgetc(pf);
    char c = i;
    //...
    fgetc reads the next character in the the stream as an unsigned char... Hence the return value will be out of the char range, and I believe that for signed types this is undefined behaviour.

    Some clarification as to how the signed and unsigned representation for characters should be used together would be most welcomed .

  2. #2
    Registered User
    Join Date
    May 2003
    Posts
    148
    >>and I believe that for signed types this is undefined behaviour.
    No,implementation defined,thats different

    6.3.1.3 Signed and unsigned integers
    When a value with integer type is converted to another integer type other than _Bool, if
    the value can be represented by the new type, it is unchanged.
    2 Otherwise, if the new type is unsigned, the value is converted by repeatedly adding or
    subtracting one more than the maximum value that can be represented in the new type
    until the value is in the range of the new type.49)
    3 Otherwise, the new type is signed and the value cannot be represented in it; either the
    result is implementation-defined or an implementation-defined signal is raised.
    Btw char types are integer types.

  3. #3
    Registered User
    Join Date
    Jun 2003
    Posts
    14
    Thanks guys.
    Salem, I might be wrong, but I think what you are saying is true only for complement to 2 representation.

    "implementation defined" OK . Not very portable though.

    Can I expect the physical representation of a specific non-ASCII character to be the same as an unsigned char and a char? Suppose I have ISO characters in the source file, will this work as expected:

    Code:
    char buf[21];
    int j = 0;
    for(; j < 20; j++){
        int c = fgetc(pf);
        if(c==EOF) {
            break;
        }
        memset(&buf[j], c, 1); // c is the unsigned char value
    }
    buf[j] = '\0';
    Say I write buf back to the file, will I get the same text? I think I will with my compiler, and probably with any reasoneable one. But it doesn't seem that the standard assures it, neither does it give any other way to do the unsigned char to char convertion while preserving the character that is being represented. I must have missed something here.
    This is all for non ASCII (ISO-latin-1 in my case) characters of course
    Last edited by P.Phant; 07-11-2003 at 07:38 AM.

  4. #4
    Registered User
    Join Date
    Jun 2003
    Posts
    14
    Thanks Salem.
    This just obscures the problem - I doubt it will change much. The bulk of the C standard library is itself written in C.
    I realise that, that was just an exemple, albeit not a very good one .

    I feel a bit stupid to insist about this thing, since I have actually never encountered a problem. What can I say? It is bothering me.

    My problem with the unsigned char approach is that at some point I am going to pass those string to function wich take char* parametters, for string manipulation I doubt that is a problem, but for output...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Signed Char Overflow
    By coder8137 in forum C Programming
    Replies: 5
    Last Post: 11-17-2006, 08:25 AM
  2. Please STICKY this- vital to MSVC 6 dev - BASETSD.h
    By VirtualAce in forum Game Programming
    Replies: 11
    Last Post: 03-15-2005, 09:22 AM
  3. build errors migrated from dx9b to dx9c sdk
    By reanimated in forum Game Programming
    Replies: 4
    Last Post: 12-17-2004, 07:35 AM
  4. unsigned char & signed char
    By studentc in forum C Programming
    Replies: 20
    Last Post: 05-31-2004, 06:30 AM
  5. unsigned char vs signed char and range of values
    By Silvercord in forum C++ Programming
    Replies: 5
    Last Post: 01-22-2003, 01:30 PM