By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,786 Members | 1,143 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,786 IT Pros & Developers. It's quick & easy.

difference between unsigned char and char in c

P: n/a
hi all experts,
can anybody explain me the difference between the
unsigned char and char in c/c++ langugage.

specifically how does this affects the c library fucntion such as
strcat,strtok etc and
their implementation.the way compiler treats them and the scenarios
where one
can be preffered rather than other.
thanks
rt

Sep 8 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
ra************@gmail.com writes:
can anybody explain me the difference between the
unsigned char and char in c/c++ langugage.
Unsigned has a range of at least 0..255, signed of at least -127..127.
Those are minimum ranges - chars are guaranteed to be at least 8 bits
wide, but they're allowed to be wider.
specifically how does this affects the c library fucntion such as
strcat,strtok etc and their implementation.


You really should ask about C library functions in a group where C is the main
focus though. You'll get better answers there.

sherm--

--
Cocoa programming in Perl: http://camelbones.sourceforge.net
Hire me! My resume: http://www.dot-app.org
Sep 8 '05 #2

P: n/a

ra************@gmail.com wrote:
hi all experts,
can anybody explain me the difference between the
unsigned char and char in c/c++ langugage. There are three char types in C++: char, signed char and unsigned char.
They all occupy one byte - e.g. size() is 1. They are required to hold
at least eight bits.
specifically how does this affects the c library fucntion such as
strcat,strtok etc and
their implementation.the way compiler treats them and the scenarios
where one
can be preffered rather than other.
These all use char. So if you have an unsigned char or a signed char,
you need to cast.


thanks
rt


/Peter

Sep 8 '05 #3

P: n/a
ra************@gmail.com wrote:

hi all experts,

can anybody explain me the difference between the
unsigned char and char in c/c++ langugage.
First of all:
a char is nothing else then a small integer in C and C++.
Only during input/output they are treated differently then
all other data types describing numbers.

Having said that:
As with all other integer types, they come in 2 flavours:
signed and unsigned.

A plain char (without the prefix 'signed' or 'unsigned') is either
the one or the other. The compiler chooses for you and it most likely
bases that choice on the way both of them can be implemented on your
hardware. The compiler will choose the way that gives the least troubles
based on the assumption that you will use a char for character manipulation
only. (Remember: characters are just small integers. So yes, one can do
arithmetic with them!)

specifically how does this affects the c library fucntion such as
strcat,strtok etc and
their implementation.the way compiler treats them and the scenarios
where one
can be preffered rather than other.


If you just do character manipulation, use plain vanilla char.
If you need to do some arithmetics: choose one, signed char or unsigned
char, based on the intended usage.
If you need to manipulate 'raw bytes', as it is usually done when interfacing
to external hardware or manipulating files on a byte level, use
unsigned char.

--
Karl Heinz Buchegger
kb******@gascad.at
Sep 8 '05 #4

P: n/a

Karl Heinz Buchegger skrev:
ra************@gmail.com wrote:

hi all experts,

can anybody explain me the difference between the
unsigned char and char in c/c++ langugage.
First of all:
a char is nothing else then a small integer in C and C++.
Only during input/output they are treated differently then
all other data types describing numbers.

Having said that:
As with all other integer types, they come in 2 flavours:
signed and unsigned.

I believe there are three types:

void f(char);
void f(signed char);
void f(unsigned char);

int main()
{
char c;
signed char sc;
unsigned char uc;
f(c);
f(sc);
f(uc);
}

is a legal C++ program.

[snip]
If you just do character manipulation, use plain vanilla char.
If you need to do some arithmetics: choose one, signed char or unsigned
char, based on the intended usage.
If you need to manipulate 'raw bytes', as it is usually done when interfacing
to external hardware or manipulating files on a byte level, use
unsigned char.
I agree with you on this part. --
Karl Heinz Buchegger
kb******@gascad.at


/Peter

Sep 8 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.