LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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


Reply
  Search this Thread
Old 05-22-2006, 01:37 PM   #1
ArthurHuang
Member
 
Registered: Jan 2006
Posts: 174

Rep: Reputation: 30
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)????
 
Old 05-22-2006, 01:43 PM   #2
Mega Man X
LQ Guru
 
Registered: Apr 2003
Location: ~
Distribution: Ubuntu, FreeBSD, Solaris, DSL
Posts: 5,339

Rep: Reputation: 64
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.
 
Old 05-22-2006, 01:47 PM   #3
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,014

Rep: Reputation: 115Reputation: 115
This feels like a HW question. Read K&R 2.2, 2.9 if you've got it around.
 
Old 05-22-2006, 02:36 PM   #4
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
'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.
 
Old 05-23-2006, 03:46 AM   #5
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Slackware 10.1/10.2/12, Ubuntu 12.04, Crunchbang Statler
Posts: 3,786

Rep: Reputation: 282Reputation: 282Reputation: 282
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).
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
rpm not signed? smgregory Fedora 1 03-28-2006 12:16 PM
Finally signed up! nightowl0v0 LinuxQuestions.org Member Intro 1 01-12-2006 11:36 PM
convert unsigned char * to unsigned long int linux_lover2005 Programming 3 04-26-2005 11:38 PM
Why new kernels 2.6.11.1 and 2.6.11.2 aren't signed by Linus? Linux.tar.gz Linux - General 1 03-09-2005 05:47 PM
signed .... linuxanswer Programming 3 11-28-2003 09:20 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:15 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration