Maker Pro
Maker Pro

Unsiged Char

Hello forum :)

I have decided to start programming my pics with C. Ive gotton most of it down except for unsigned char. Signed char(char) gives you a range of -128 to 127 and unsigned char which is 0 to 255. What is the difference?
 
Both char and unsigned char represent the same number of values but unsigned char is purely positive. If you're doing math and the result is 150 and you assign that to a signed char your micro will think it represents -22, clearly a problem. If you need to represent a bigger number you'd use an int or unsigned int (-32.768-32.767 vs 65535).
 
unsigned char

Its just a little more confusing when i saw
Code:
#include <htc.h>
void eetest(void) {
unsigned char value = 1;
unsigned char address = 0;
// write value to EEPROM address
eeprom_write(address, value);
// read from EEPROM at address
value = eeprom_read(address);
}

Why would they assign the memory address as a unsigned char?
 
unsigned char

Thats where the confusion comes in... what if we need to save something in eeprom address 23. char only holds 1 character now we have 2 or does it go buy value 0-255...which in that case can only read 255 address spaces? If that is correct.. after 255 address space then we use unsigned int?
unsigned char is 1 byte size. If we are only using 0-255 values there is no need to call for int which takes 2 bytes right?
 
Last edited:

(*steve*)

¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd
Moderator
You MUST use:

1) a datatype legal for the function
2) a datatype which can hold the value you require
 
or does it go buy value 0-255...which in that case can only read 255 address spaces? If that is correct.. after 255 address space then we use unsigned int?
unsigned char is 1 byte size. If we are only using 0-255 values there is no need to call for int which takes 2 bytes right?

Correct, you're on the right track.
 
Thank you

You have both been very helpful...I also hope someone will find these threads helpful. Is there a way to give thumbs up or rep?
 
Top