I've seen a lot of "unsigned char" in the past, but is it already unsigned by default or is it implementation specific?
Printable View
I've seen a lot of "unsigned char" in the past, but is it already unsigned by default or is it implementation specific?
It is implementation specific.
Usually I think it is signed by default, however, which is why you will see "unsigned char" used in a lot of code.
It is implementation specific as previously mentioned.
Quzah.
I think that the real reason 'why you will see "unsigned char" used in a lot of code' is that the code involved requires the use of unsigned integers for correctness, e.g., bit shifting. Thus, it makes sense to specify unsigned char rather than assuming that the implementation will have char as unsigned.Quote:
Originally Posted by MK27
Ah, so I guess the moral of the story is specify it anyway if it's important - even if it's just being clear to other developers that you intend this to be unsigned.
char is always unsigned. for example if u give char a=-257 it wont bother about '-'. it will simply print value of corresponding to char 1, some special symbol.
Sorry, but the 1999 edition of the C standard disagrees with you, so you must be wrong:Quote:
Originally Posted by magestrium
Quote:
Originally Posted by C99 Section 6.2.5 Paragraph 15 and note 35
Your example has to do with how the value is printed, not with whether char is signed or unsigned.Quote:
Originally Posted by magestrium
Wotcher,
I needed to know, so I wrote a quick program and tested it on our various porting platforms. The following platforms all have char == signed char:
Windoze, Solaris(sparc), Solaris(intel), HP(risc), HP(intel), Linux(intel).
IBM, however, buck the tend. All these platforms have char == unsigned char:
Aix, Linux(s390), Linux(PowerPC).
Hope that helps,
Grelly
Actually it's configurable in Visual Studio IIRC.
Microsoft compilers have a flag to change it
/J (Default char Type Is unsigned) (C++)
GCC compilers have a flag to change it
Options Controlling C Dialect
If you really care about the signed-ness of your chars, then just say "signed char" or "unsigned char" and forget about whether your current compiler has a flag to fix it for you.
I can't help feeling that to use compiler options to change a default behaviour is a bad idea. It won't be obvious to the next developer that the sign-ness has been reversed.
My recommendation is to get into the habit of using the signed/unsigned qualifiers the whole time.