Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

signed and unsigned char

Status
Not open for further replies.

WebDrake

Programmer
Sep 29, 2005
106
PL
Hello all,

I've heard that it's good practice to always declare char variables as signed or unsigned.

Could anyone comment? Is it worth it?

In addition, under what circumstances should one declare one or the other?

I note that when I tried to compile a program under Linux with all unsigned chars, I got errors when using printf: "warning: pointer targets in passing argument 1 of ‘sprintf’ differ in signedness".

Do printf etc. always require signed chars to be entered or is there a way of telling it a given char (or string of chars) is unsigned?

Many thanks,

-- Joe
 
In fact there are three different types in C: char, signed char and unsigned char. Only char type is true portable one. Of course, char is signed or unsigned in the specific implementation, but pointer to char and pointer to signed char are different types even if char is signed in the implementation. So you have problems with library function signed/unsigned char arguments (because of all lib functions want char as is).

So it's most likely bad practice to always declare char variables as signed or unsigned.
 
Mm. Further to the above good advice, I was advised on the usenet C list that I should use just "char" for things I mean to print, but for other stuff that it might be worth specifying signed or unsigned in other cases. e.g. if I have a 0 or 1 check variable, declaring it as "unsigned char" is worthwhile... ;-)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top