473,396 Members | 1,748 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

difference between unsigned char and char in c

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
4 17058
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

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

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Eric J.Hu | last post by:
Hi, Such as I have following bitmap structure and assigned value: struct bm { char high2bits:2; char mid4bits:4; char low2bits:2; }; bm.high2bits = 3;
3
by: ling | last post by:
Hi, I'm trying to do fscanf from a file with integers. I've tried using both %d and %i hoping that I would figure out their difference. When I checked the man pages, the only difference I...
6
by: nick | last post by:
I have tried finding an answer to this, but most people just explain classes as a more modular way to program. It seems to me that (forgetting OO programming which I don't quite understand) the...
9
by: dam_fool_2003 | last post by:
For int data type the default range starts from signed to unsigned. If we don't want negative value we can force an unsigned value. The same goes for long also. But I don't understand why we have...
10
by: tinesan | last post by:
Hello fellow C programmers, I'm just learning to program with C, and I'm wondering what the difference between signed and unsigned char is. To me there seems to be no difference, and the...
11
by: Jason Curl | last post by:
Dear C people, C90 doesn't specify if 'char' is either 'signed char' or 'unsigned char' leaving it to the implementation of the compiler. Has this changed for C99, or is it still the same? ...
3
by: Eric J.Hu | last post by:
Hi, Such as I have following bitmap structure and assigned value: struct bm { char high2bits:2; char mid4bits:4; char low2bits:2; }; bm.high2bits = 3;
4
by: ravinderthakur | last post by:
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...
20
by: Hanzac Chen | last post by:
Hi, I don't understand why this could happen? The Code 1 will output `fff9' and the Code 2 will output `1' How could the `mod 8' not have effect? /* Code 1 */ #include <stdio.h> #include...
13
by: In a little while | last post by:
thanks
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.