ProgrammingThis 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.
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.
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.
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.
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.
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??
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789
Rep:
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).
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.
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:
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.
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.
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.