unsigned char in c

Go To Last Post
25 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

 

I have doubt on char variable in c language. Let's suppose micro-controller takes 8 bits to store single integer  and takes 8 bits to store single character

 

size of integer variable is 8 bit. 

 

int x  // Integer variable X can store value  from 0 to 255, -128, 0, 127 

 

signed int x // Integer variable X can store value  from 0 to 255, -128, 0, 127 

 

unsigned int x // Integer variable X can store value  from 0 to 255, only positive Number 

 

size of character  variable is 8 bit. character value store ascii value of character

 

char y; 

unsigned char y; 

 

What is the limit of variable y, which char it can store, which it can't store?

This topic has a solution.
Last Edited: Thu. Feb 27, 2020 - 03:55 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

As I understand it for GCC

 

uint8_t x = Unsigned byte, 0 - 255

int x = standard 16 bit integer with range of -32768 to +32768

uint16_t = 16bit integer with range of 0 - 65536

 

char is the same as an unsigned integer, but it depends on the compiler.  CodeVision uses char for unsigned 8 bit integers.

 

I am probably wrong on this but others are sure to point that out if I am.

 

JIm

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

 

"The critical shortage here is not stuff, but time." - Johan Ekdahl

 

"Step N is required before you can do step N+1!" - ka7ehk

 

"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman

"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?"  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB, RSLogix user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

None is this secret knowlege - a bit of Googling will yield an answer.  You need to brush up on your Google-fu.

 

https://www.nongnu.org/avr-libc/...

 

 

Sizes and limits are specified. 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

jgmdesign wrote:

As I understand it for GCC

 

uint8_t x = Unsigned byte, 0 - 255

int x = standard 16 bit integer with range of -32768 to +32768

uint16_t = 16bit integer with range of 0 - 65536

 

char is the same as an unsigned integer, but it depends on the compiler.  CodeVision uses char for unsigned 8 bit integers.

 

I am probably wrong on this but others are sure to point that out if I am.

 

JIm

 

okay forget and lets assume that there is no compiler, we have only hardware There is only one 8-bit register that stores the ASCII values. 

 

I have character from 'A' to 'Z', '1' to '9' , '@', '!'

 

from the above list Which character register can store and Which character  register can not  store ?

Last Edited: Thu. Feb 27, 2020 - 02:09 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Which character register can store and Which character  register can not  store ?

An 8-bit byte can store any ascii symbol (from 256 different types)...why do you think there are some that can't be stored?  Have you looked at an ASCII table?

 

http://www.asciitable.com/

 

 

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

sky33 wrote:

jgmdesign wrote:

As I understand it for GCC

 

uint8_t x = Unsigned byte, 0 - 255

int x = standard 16 bit integer with range of -32768 to +32768

uint16_t = 16bit integer with range of 0 - 65536

 

char is the same as an unsigned integer, but it depends on the compiler.  CodeVision uses char for unsigned 8 bit integers.

 

I am probably wrong on this but others are sure to point that out if I am.

 

JIm

 

okay forget and lets assume that there is no compiler, we have only hardware There is only one 8-bit register that stores the ASCII values. 

 

I have character from 'A' to 'Z', '1' to '9' , '@', '!'

 

from the above list Which character register can store and Which character  register can not  store ?

 

Short answer is the above mentioned datatypes are NOT registers, but VARIABLES.  And the answer is and unsigned 8bit VARIABLE

 

JIm

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

 

"The critical shortage here is not stuff, but time." - Johan Ekdahl

 

"Step N is required before you can do step N+1!" - ka7ehk

 

"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman

"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?"  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB, RSLogix user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

avrcandies wrote:

Which character register can store and Which character  register can not  store ?

An 8-bit byte can store any ascii symbol (from 256 different types)...why do you think there are some that can't be stored?  Have you looked at an ASCII table?

 

http://www.asciitable.com/

 

 

 

What is difference between  signed char and unsigned char ? I am sure they both are not same 

 

unsigned char means we can store decimal value from 0  to 255  Each decimal value represents a char.

 

from table  A

decimal : 65

hex : 0x 41

 

unsigned char alphabat = 65 // It store 'A' 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

sky33 wrote:
What is difference between  signed char and unsigned char ? I am sure they both are not same 

 

In terms of storing ASCII characters, there is no difference. If you wish to do arithmetic on the values, then the sign becomes significant.

 

ASCII 'A"  or 65 decimal is the same whether the variable is signed or unsigned.

 

 

Lucky we're not talking about unicode......

 

 

Your learning is a bit backwards - usually one learns about binary math, variable sizes etc first. You started with linked lists. 

This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 2

Important fact: a character data type does not really store ONLY characters. That is because a character is indistinguishable from any other 8-bit thing. Ditto signed or unsigned 8-bit integer.

 

Example: 0b10101010 can be interpreted as (signed) decimal -86 or (unsigned) 170 or a character that is NOT "legal ASCII" 

Example: 0b01010101 can be interpreted as (signed) decimal 85 or (unsigned) 85 or the character 'U'.

 

The ONLY function of the char data type is to remind you that the contents should be interpreted as a character. The ONLY function of the signed int data type is to remind you that the contents should be interpreted as a signed value. The only difference between (char)0x54, (unsigned char)0x54, (signed char) 0x54, (uint8_t)0x54, and (int8_t)0x54 is how the number is to be INTERPRETED. The memory location contains exactly the same bit pattern in all cases.

 

Sadly, this was not always thus. Just a few years ago, char was commonly used as a generic 8-bit byte that could hold numbers or characters. Fortunately, that use is decreasing, but there is plenty of code in common use, right now, that uses char, unsigned char, and signed char that way.

 

Jim

Jim Wagner Oregon Research Electronics, Consulting Div. Tangent, OR, USA http://www.orelectronics.net

Last Edited: Thu. Feb 27, 2020 - 03:48 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

 

ka7ehk wrote:
Example: 0b10101010 can be interpreted as (signed) decimal -86 or (unsigned) 170 or a character that is NOT "legal ASCII" 

Thanks This answer clears my doubt. So we need a valid ascii value of character.This means we should always use the  unsigned char in program 

 

 

Kartman wrote:
Your learning is a bit backwards - usually one learns about binary math, variable sizes etc first. You started with linked lists. 

 

It's not like that I have read c language and i am practicing. while practicing I had a doubt   and I think Its valid question

Last Edited: Thu. Feb 27, 2020 - 03:58 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

Forget unsigned char. Use char if you intend to store characters. For characters, "signed" or "unsigned" are nonsense. What does "-&" or "+&" mean? char is char. Use uint8_t for unsigned 8-bit and int8_t for signed 8-bit.

 

This also brings up, in my opinion, an abomination that seems to be rampant on this forum. Please, "doubt" does not mean "confused". Doubt means that you have been told something or have read something, and believe that it might not be true. Confused means that you do not understand what you have read or have been told. They are not the same. 

 

Jim

Jim Wagner Oregon Research Electronics, Consulting Div. Tangent, OR, USA http://www.orelectronics.net

Last Edited: Thu. Feb 27, 2020 - 04:47 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

sky33 wrote:
This means we should always use the  unsigned char in program 

 

Good practice would suggest you use uint8_t. This is explicit in the size of the variable. Similary, there is uint16_t,uint32_t,uint64_t. No room for mis-interpretation.

 

Jim - the misuse of 'doubt' is common in certain parts of the world. Just like there is Americanisms, Australianisms etc. 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Jim - the misuse of 'doubt' is common in certain parts of the world.

Also, saying:  "I, no doubt, am confused"    is a fun one to consider.  In that case, you are certain you are confused!

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

ka7ehk wrote:
Forget unsigned char. Use char if you intend to store characters. For characters, "signed" or "unsigned" are nonsense.
And that is one of the key points. There are THREE types of char. "char", "unsigned char" and "signed char". For the latter two it is far better to use "uint8_t" and "int8_t" as they are unequivocal about what you are referring to. So you might have:

uint8_t unsignedArray[] = { 1, 17, 53, 96, 103, 217, 0, 243, 255 };

int8_t signedArray[] = { 1, 0, -23, 49, -117, 127, -128 };

char charArray[] = "Hello world, nice ere innit?";

On any CPU and any compiler a uint8_t will hold 0 .. 255, an int8_t will hold -128 .. +127 and a char will hold characters.

 

But remember that in terms of bytes of memory a typical computer has groups of 8 bits as its fundamental storage unit and any such storage location can hold any bit pattern from 00000000 to 11111111. Whether what is stored there is int8_t, uint8_t, char, unsigned char, signed char or whatever is purely a question of interpretation.

 

For example a one byte memory location may hold 0b10100101 (0xA5). If you read it as uint8_t (aka "unsigned char") you see it as 165, if you read it as int8_t (aka signed char) you see it as -91 and if you read it as char then it kind of depends what you are using to look at it and what character set/font is being used but you might typically see it as '¥' (Yen) or perhaps 'Ñ' or something else - the fact is that only character codes from 0 .. 127 are really standardized but 128 .. 255 depend on the character set in use (you may want to google "code page switching")

 

(Oh and even 0 .. 127 are not set in concrete. ASCII is the common interpretation so for example 61 is typically 'A' etc but there's also things like EBCIDIC which is a completely different character ordering)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

ka7ehk wrote:
For characters, "signed" or "unsigned" are nonsense. What does "-&" or "+&" mean? char is char. Use uint8_t for unsigned 8-bit and int8_t for signed 8-bit.

 

Sometimes you do need to perform arithmetic on char variables. For example, if you want to convert a character representation of a digit to its value you need to subtract '0':

unsigned char c = '2';
int value = c - '0';

I feel safer using unsigned char (or uint8_t) here.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Wonder what '£' + 7 is ? ;-)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:
Wonder what '£' + 7 is ? ;-)

I give up -- the Euro symbol?

 

"Pound and seven" is 27 shillings, right?

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0


clawson wrote:

Wonder what '£' + 7 is ? ;-)

https://www.ascii-code.com/

"Experience is what enables you to recognise a mistake the second time you make it."

"Good judgement comes from experience.  Experience comes from bad judgement."

"Wisdom is always wont to arrive late, and to be a little approximate on first possession."

"When you hear hoofbeats, think horses, not unicorns."

"Fast.  Cheap.  Good.  Pick two."

"We see a lot of arses on handlebars around here." - [J Ekdahl]

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

It might well be a feminine ordinal in one particular character set where £ happens to be 0xA3 but my point was that because (apart from a certain amount of ASCII predictability from 0x20 to 0x7F) there's no fixed standard for character interpretations so that doing 'char something' + N is maybe not the best idea in the world as where you get to completely depends on the encoding of "char something" and also what happens to be beyond that at +N in this particular character set.

 

Sure I guess you can probably guarantee that '3'+ 5 is generally '8' but outside that limited character space I'd suggest "character arithmetic" is something of a crap shoot !

 

I think in EBCDIC the 10 digits are adjacent so '9' minus 4 probably is always '5' but aren't the ABCD's in a few groups so 'J' + 11 may not necessarily be 'U' (in fact 'U' - 'J' would appear to be 19 in EBCIDIC !)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I should have put a smiley.
Here it is now ;-)

"Experience is what enables you to recognise a mistake the second time you make it."

"Good judgement comes from experience.  Experience comes from bad judgement."

"Wisdom is always wont to arrive late, and to be a little approximate on first possession."

"When you hear hoofbeats, think horses, not unicorns."

"Fast.  Cheap.  Good.  Pick two."

"We see a lot of arses on handlebars around here." - [J Ekdahl]

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Depending on the compiler, toolchain, etc there should be helpful constants you can reference, e.g. INT_MAX and friends. You may need to include a header file e.g. limits.h

 

e.g. on a 32-bit ESP32, the size and capacity of a signed int are:

 

  printf("sizeof(int) = %d", sizeof(int));
  printf("INT_MAX = %d", INT_MAX);
 

  sizeof(int) = 4

  INT_MAX = 2147483647
 

http://www.cplusplus.com/referen...

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Well, this is the charset people use this days:  🤔 😏 😴 👿 💣 🤘 🍨 🚽

I wouldn't do math with those, that's for sure 😋

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

El Tangas wrote:
people
Not this "people" ;-)

 

(and, yes, I see the irony!)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

He misspelt 'ancient egyptians'.

 

Neil

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Yeah, back to ideographs, I guess. There is nothing new under the sun...

🌏

👨‍👨‍👦‍👦

♻️