I've seen a lot of "unsigned char" in the past, but is it already unsigned by default or is it implementation specific?
I've seen a lot of "unsigned char" in the past, but is it already unsigned by default or is it 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.
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
It is implementation specific as previously mentioned.
Quzah.
Hope is the first step on the road to disappointment.
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.Originally Posted by MK27
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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:Originally Posted by magestrium
Originally Posted by C99 Section 6.2.5 Paragraph 15 and note 35Your example has to do with how the value is printed, not with whether char is signed or unsigned.Originally Posted by magestrium
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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.
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 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.