Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
05-22-2006, 01:37 PM
|
#1
|
Member
Registered: Jan 2006
Posts: 174
Rep:
|
signed and unsigned
what's the difference with these three definitions?
char
signed char
unsigned char?
Generally, when and how to use them?
(Networking, scientific computing)????
|
|
|
05-22-2006, 01:43 PM
|
#2
|
LQ Guru
Registered: Apr 2003
Location: ~
Distribution: Ubuntu, FreeBSD, Solaris, DSL
Posts: 5,339
Rep:
|
Well, a signed char can have either positive or negative values, while an unsigned char can only have positive values. Ex:
Code:
signed char myChar = 10;
signed char newChar = -3;
unsigned char yourChar = 100;
in the case you have only a char:
Code:
char anotherChar = 20;
it's up to the compiler to define if it will be either signed or unsigned by default.
|
|
|
05-22-2006, 01:47 PM
|
#3
|
Senior Member
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012
Rep:
|
This feels like a HW question. Read K&R 2.2, 2.9 if you've got it around.
|
|
|
05-22-2006, 02:36 PM
|
#4
|
Senior Member
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078
Rep:
|
'char' is just a number, like 'short' and 'long'. Since it has only 256 values, we choose to generally represent letters using 'char' and binary using 'unsigned char', though the computer sees them both as 1 byte numbers. The usual default is 'signed', but you always have the option of making it explicit. The C++ standard says that any pointer can be cast to 'unsigned char*' for binary representation without "undefined behavior", which is why it's mostly used for working with single bytes of raw data. Really, the only thing different between the two is how arithmetic other than + and - affect them, and how std::cout (in C++) would choose to display them (and, of course, the pointer types don't implicitly convert.)
ta0kira
Last edited by ta0kira; 05-22-2006 at 02:40 PM.
|
|
|
05-23-2006, 03:46 AM
|
#5
|
Senior Member
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Ubuntu 12.04, Antix19.3
Posts: 3,797
|
You use 'unsigned char' when you want the compiler to treat it as unsigned.
An example
Code:
char a=127,b=1,c;
c=a+b;
printf("a+b=%d+%d=%d\n",a,b,c);
a=0x01; // 1 signed, 1 unsigned
b=0xff; // -1 signed, 255 unsigned
if(a>b) printf("a greater than b\n");
else printf("a smaller than (or equal to) b\n");
The result:[code]a+b=127+1=-128
a greater than b[/code[
Change the char in the code to unsigned char and this will be the result:
Code:
a+b=127+1=128
a smaller than b
If char is used as a character, it does not matter to much as characters are usually 7-bit (ascii table).
If you're however programming in a dos/windows environment, you will have the extensions (like à,ç etc which are the values 128 to 255).
When comparing the characters (a<b or a>=b), the sign is important (see above example).
|
|
|
All times are GMT -5. The time now is 09:11 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|