system
October 10, 2013, 2:20am
1
I recently found that when I pass an integer to Serial.println() it interprets it as a char (I think). Here is my code:
int numbers[] = {32767, 32768};
void setup(){
Serial.begin(9600);
Serial.println(numbers[0]);
Serial.println(numbers[1]);
}
void loop(){
}
The output:
32767
-32768
I'm a new C programmer so I have a hunch that the problem might also have something to do with my array declaration, but that seems unlikely. Any ideas regarding what's going wrong?
32768 is too big for an int so it rolls over to -32768 which is an int.
guix
October 10, 2013, 2:38am
3
32767 is the max value of a signed int. So use unsigned int instead, it's min value is 0 and max value is 65535. If you want bigger numbers, use long.
Did you choose those numbers for some particular reason ?
If you got them from an example code somewhere, then the "error" was probably intentional.
system
October 11, 2013, 1:45pm
5
Ah, that makes sense. I had thought an int was 4 bytes, but I guess that's not true for C. Thanks for the help!
ARDUINO UNO TYPES
byte = 8 bit unsigned!
char = 8 bit signed !
int = 16 bit
unsigned int = 16 bit
long = 32 bit
unsigned long = 32 bit
long long = 64 bit (seldom used, slow)
unsigned long long = 64 bit
float = 4 byte IEEE754 format
double = 4 byte
uint8_t
uint16_t
uint32_t