LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
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 03-19-2005, 01:53 PM   #1
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: Gentoo, LFS, Debian,Ubuntu
Posts: 1,537

Rep: Reputation: 87
unsigned char in C language


Ok i created this form question because i asked this in another post witch was wrong to do.



So I ask all of the programming guru out there what is a unsigned char and why would you need to use this over a int ???




as far as i I know char cannot be signed or unsigned in C programming.


Please correct me if im wrong on this one?
 
Old 03-19-2005, 02:50 PM   #2
alred
Member
 
Registered: Mar 2005
Location: singapore
Distribution: puppy and Ubuntu and ... erh ... redhat(sort of) :( ... + the venerable bsd and solaris ^_^
Posts: 658
Blog Entries: 8

Rep: Reputation: 31
unsigned char range = 0 to 255
char range = -128 to 127
int range = -32768 to 32767
unsigned int range = 0 to 65535


as for why ,it might be to conserve memory
 
Old 03-19-2005, 03:54 PM   #3
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: Gentoo, LFS, Debian,Ubuntu
Posts: 1,537

Original Poster
Rep: Reputation: 87
Hmmm strange.


i can have a char past 128 with just a char tho so why have unsigned.


what charecters in the ascii set are negtive numbers


this is whats annoying to me. a char is a chaecter like 'A' numbers are not chars they are int
why would you use a char or for that instance a negtive char.
this seams stupid to me.



char testvar = 12



this code will complile but it will print the ascii set charecter for 12 not the number 12


char testvar2 = -12;

this code will comple without errors but the value when printed prints garbage.




unsinged char testvar3 = 12

has the same reslut as the first.


so in retro spect and after lookiing around char are always unsigned they are never negitive because there is no ascii charecter thats negitive. means declareing a char unsigned is extra unneeded code it does not make char go to 255 it alredy does you are making code harder to read and understand.

Looking around online there are refernces to a signed char. Hmmm ok so its possible to assign a negitive to a char varable but why would you do this for what situation would you need to use it.


Im not trying to preach that I know more about programming and you all are wrong im just in reality learning c programming and would like to know why you would have a negitive char varible what use is it.

gcc does not create a warning doing this even with -Wall so its got to be a leagal C syntax but does anyone know what its use is.

Last edited by exvor; 03-19-2005 at 04:16 PM.
 
Old 03-19-2005, 04:36 PM   #4
alred
Member
 
Registered: Mar 2005
Location: singapore
Distribution: puppy and Ubuntu and ... erh ... redhat(sort of) :( ... + the venerable bsd and solaris ^_^
Posts: 658
Blog Entries: 8

Rep: Reputation: 31
a value of 0 to 31 are control characters eg.tabs or carriage return
a value of 32 to 127 are a,b,c,d....1,2,3,4,5..... and punctuation
a value of 128 to 255 are for foreign or special characters

according to " C " talk those upper and lower case english leeters,10 digits and around 29 puntuation are all GUARANTEED (until now i still don't understand this)to be positive.
those negative ones are the so-called extended characters eg ISO####Latin## character sets or MSDOS extended or MAC extended,etc
and as if not for the worst,those characters that can't fit well in char are all dumped as wchar_t (wide char)with a jumbo size of 65536(without english character). eg multinational characters like sanskrit or Cyrillic or chinese.

note:i might not be totally correct with all these,so somebody please help double check.
 
Old 03-19-2005, 04:51 PM   #5
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: Gentoo, LFS, Debian,Ubuntu
Posts: 1,537

Original Poster
Rep: Reputation: 87
This is starting to sound like another Microsoft influnced screw up.


But no really i understand that char values above 127 are not considered standard ascii charecters.



Looking online just getting more confusing seams to be a debate on this one with char

char the abused!!!



one says using unsigned char when not nessisary is dagerous.

mabye i should look at char as memory more then a value of something i might understand signed char and unsinged char better.


whatcha all think?
 
Old 03-19-2005, 05:19 PM   #6
Dave Kelly
Member
 
Registered: Aug 2004
Location: Todd Mission Texas
Distribution: Linspire
Posts: 215

Rep: Reputation: 31
Remember in all this discussion that C is a very old language. It was old as far back as when I started fiddling with it and that would be the early 80s.

K&Rs ansi C book has a copy right date of 1978. So this standard was probably developed to fit in with some of the early 4 bit 8 bit architecture of the CPUs.

Would be a good read to research the history of this.

Dave
 
Old 03-20-2005, 01:33 AM   #7
CrazyPilot
Member
 
Registered: Jan 2004
Location: Somewhere...
Distribution: Fedora Core 4, Slackware 10.1
Posts: 61

Rep: Reputation: 15
Re: unsigned char in C language

Quote:
Originally posted by exvor
why would you need to use this over a int ???
you wouldn't... did you acctually see this used somewhere? where?
 
Old 03-20-2005, 01:40 AM   #8
alred
Member
 
Registered: Mar 2005
Location: singapore
Distribution: puppy and Ubuntu and ... erh ... redhat(sort of) :( ... + the venerable bsd and solaris ^_^
Posts: 658
Blog Entries: 8

Rep: Reputation: 31
in those days,someone might be labeled as a marketing genius if he predicted that there will be a global market for 6 computers...............

problems shared :

http://www.cim.mcgill.ca/~franco/OpS...es/node51.html

Arithmetic and Data Types - Mixed Data Type Arithmetic

agitation :

http://www.joelonsoftware.com/articles/Unicode.html


for those cool minded :

ftp://kermit.columbia.edu/kermit/charsets/

Last edited by alred; 03-20-2005 at 03:22 AM.
 
Old 03-20-2005, 09:21 AM   #9
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: Gentoo, LFS, Debian,Ubuntu
Posts: 1,537

Original Poster
Rep: Reputation: 87
Too all my madness here there is a point



what im trying to make is why would anyone ever declare char to be unsigned when it alredy is unsigned.

char just declared as char can alredy go to 255


however i have never yet seen char declared with a negitive like

char testparm = -15;


this creates a char with garbage but why doesnt it generate a error in gcc. Because it doesent generate an error there must be some use for it. Can anyone give me an example for why you would use a negitive char value??
 
Old 03-20-2005, 09:54 AM   #10
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Quote:
what im trying to make is why would anyone ever declare char to be unsigned when it alredy is unsigned.
You're mistaken, a char variable is signed. and represent either a character for which the sign doesn't mean a lot or a 8 bit binary value, with 255 possible values from -128 (0x80) to +127 (0x7F), where -1 is corresponding to 0xFF.

An unsigned char represent, when used numerically, 256 values from 0 (0x00) to 255 (0xFF).
 
Old 03-20-2005, 10:48 AM   #11
Dave Kelly
Member
 
Registered: Aug 2004
Location: Todd Mission Texas
Distribution: Linspire
Posts: 215

Rep: Reputation: 31
Quote:
Originally posted by exvor
Too all my madness here there is a point
what im trying to make is why would anyone ever declare char to be unsigned when it alredy is unsigned.
I can not stress the importance of getting a good book to sit and read through.
Let me quote from K & R - chapter 8.2:
Quote:
.........For example, here is a version of getchar that does unbuffered input, by reading the standard input one character at a time.
Code:
 #include "systacalls.h"
 int getchar ( void )
    {
         char c;
         return ( read(0,&c,1) ==1) ? ( unsigned char ) c : EOF;
     }
c must be a char, because read needs a character pointer. Casting c to unsigned char in the return statement eliminates any problem of sign extension.
Question for you MilkMan.
Describe the 3 flavors of the variable c in the code snippet above.
 
Old 03-20-2005, 11:52 AM   #12
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
Quote:
Originally posted by exvor
char testparm = -15;


this creates a char with garbage but why doesnt it generate a error in gcc.
gcc doesn't complain because of the way negative values are handled internally. Do a search on "two's complement". It's the scheme used to represent integer data as positive or negative. Say you have a positive char value of 4 (which is represented in binary as 0000_0100), and you want it to be negative. Two's complement says, "invert the bits, and add 1". So, doing so would create: 1111_1011 + 1 = 1111_1100. Thus, 1111_1100 in two's complement binary is -4. If the value is unsigned then the same binary sequence is interpreted as 252.

So, in the statement you gave above, the -15 is treated as an int. Assuming a 32-bit int, that means the int (in two's complement) is: 11111111_11111111_11111111_11110001. When it gets assigned to the char (assuming 8-bit), you lose everything other than the least significant 8 bits (i.e. 11110001). If the char is treated as unsigned, then that value is 241. If it's treated as signed, then it's still -15.

Alluding to what Dave Kelly posted, and you assign that value to an int, you get different results. For instance, assume:
Code:
unsigned int  u_int;
signed int    s_int;
unsigned char u_char;
signed char   s_char;

s_char = -15;  // s_char contains 11110001
u_char = -15;  // u_char contains 11110001

u_int = s_char;  // u_int contains 11111111_11111111_11111111_11110001 => sign extension
u_int = u_char;  // u_int contains 00000000_00000000_00000000_11110001
s_int = s_char;  // s_int contains 11111111_11111111_11111111_11110001 => sign extension
s_int = u_char;  // s_int contains 00000000_00000000_00000000_11110001
 
Old 03-20-2005, 12:13 PM   #13
alred
Member
 
Registered: Mar 2005
Location: singapore
Distribution: puppy and Ubuntu and ... erh ... redhat(sort of) :( ... + the venerable bsd and solaris ^_^
Posts: 658
Blog Entries: 8

Rep: Reputation: 31
Quote:
however i have never yet seen char declared with a negitive like char testparm = -15;
if i'm not wrong,maybe it is possible to build truly personal computers which only you and your friends can understand,especially those messages that pass between them.You don't even need those ascii 127 positive values.All it takes is just to design a personel charset with symbols or characters correspond to a positive or negative values (negative if you want to do a signed char).

as for the awkward negative values in signed char maybe it is because computer can only store anything in binary numbers and computers can't do a true subtraction ,they can only add :

5-3=2 actually is
5+(-3)=2 when they need to subtract they will do a "two complements"
eg,complement of 3 is -3,complement of 9 is -9 etc. in signed integer.




Really Needs to clarify all these though.
 
Old 03-20-2005, 03:50 PM   #14
Dave Kelly
Member
 
Registered: Aug 2004
Location: Todd Mission Texas
Distribution: Linspire
Posts: 215

Rep: Reputation: 31
Quote:
Originally posted by exvor
Too all my madness here there is a point I'm trying to make. Why would anyone ever declare char to be unsigned when it alredy is unsigned.
A declaration like;
Code:
char c = -15;
is really a constant value and not a variable and writen this way for convience sake. This way you only have to change the value once in the debugging process. etc.
Code:
#include "systacalls.h"
 int getchar ( void )
    {
         char c;
         return ( read(0,&c,1) ==1) ? ( unsigned char ) c : EOF;
     }
In the code snippet, the value c must never have a negative value. Remove the unsigned char cast and play around with the function to see how this is affected.
 
Old 03-20-2005, 04:10 PM   #15
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: Gentoo, LFS, Debian,Ubuntu
Posts: 1,537

Original Poster
Rep: Reputation: 87
I haven't done enough c programming to really understand your function. I need to find another book other then the GNU C programming tutorial to go to.

C programming is really just a hobby that I wanted to get my feet wet in. So im trying to understand variables correctly and I saw unsigned char and was confused on its use. That was the point of my post and I'm not too sure on the answer yet but maybe its just best i stay with basic char variables


In any account thanks for all your input I'm sure ill be able to understand it better in time.

*sigh* Now if i could just get mail to work properly in linux i can attain Nirvana.
 
  


Reply



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
convert unsigned char * to unsigned long int linux_lover2005 Programming 3 04-26-2005 11:38 PM
unsigned char to wchar_t JurajPsycho Programming 6 03-19-2005 04:00 PM
Hex to unsigned char? george_mercury Programming 2 11-27-2004 01:27 PM
invalid conversion from `char' to `const char* bru Programming 6 05-09-2004 03:07 PM
convert from char* to unsigned char* D J Linux - Software 2 02-20-2004 04:09 AM

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

All times are GMT -5. The time now is 06:50 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
Open Source Consulting | Domain Registration