difference between unsigned char and char in c

R

ravinderthakur

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
 
S

Sherm Pendley

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--
 
P

peter.koch.larsen

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
 
K

Karl Heinz Buchegger

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.
 
P

peter.koch.larsen

Karl Heinz Buchegger skrev:
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.

/Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top