Visit Jeremy's Blog.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 05-22-2006, 02:37 PM   #1
Registered: Jan 2006
Posts: 174

Rep: Reputation: 30
signed and unsigned

what's the difference with these three definitions?

signed char
unsigned char?

Generally, when and how to use them?

(Networking, scientific computing)????
Old 05-22-2006, 02:43 PM   #2
Mega Man X
LQ Guru
Registered: Apr 2003
Location: ~
Distribution: Ubuntu, FreeBSD, Solaris, DSL
Posts: 5,339

Rep: Reputation: 65
Well, a signed char can have either positive or negative values, while an unsigned char can only have positive values. Ex:

signed char myChar = 10;
signed char newChar = -3;
unsigned char yourChar = 100;
in the case you have only a char:

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, 02:47 PM   #3
Senior Member
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

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, 03:36 PM   #4
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.)

Last edited by ta0kira; 05-22-2006 at 03:40 PM.
Old 05-23-2006, 04:46 AM   #5
Wim Sturkenboom
Senior Member
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Ubuntu 12.04, Antix19.3
Posts: 3,794

Rep: Reputation: 282Reputation: 282Reputation: 282
You use 'unsigned char' when you want the compiler to treat it as unsigned.
An example
char a=127,b=1,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:
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).


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 01:16 PM
Finally signed up! nightowl0v0 Member Intro 1 01-13-2006 12:36 AM
convert unsigned char * to unsigned long int linux_lover2005 Programming 3 04-27-2005 12:38 AM
Why new kernels and aren't signed by Linus? Linux.tar.gz Linux - General 1 03-09-2005 06:47 PM
signed .... linuxanswer Programming 3 11-28-2003 10:20 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:38 AM.

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