void setup() {
Serial.begin(115200);
char c;
unsigned int val = 0xffff;
c = val;
Serial.println(c, DEC);
}
void loop() {
}
the compiler is performing a silent cast by stuffing a 2-byte int into a 1-byte char, which runs the risk of losing data. With the code above, the monitor shows -1. If I change the value of val to 0xff00, it shows 0, while a value of 0x00ff shows -1 again, which does provide clues about how it chops off the byte during the assignment.
My question is: Should the compiler issue at least a warning when it attempts to assign a larger data type into a small data type and, if not, why not?
That gets rid of the warning. Then you have to work your way through your beloved* String library, eg.
/home/nick/Development/arduino-1.5.8/hardware/arduino/avr/cores/arduino/WString.cpp: In member function 'void String::toLowerCase()':
/home/nick/Development/arduino-1.5.8/hardware/arduino/avr/cores/arduino/WString.cpp:707:18: warning: conversion to 'char' from 'int' may alter its value [-Wconversion]
*p = tolower(*p);
^
And that was in code that didn't even use String!
Or is that "much loved"?
And in a whole lot of other places, eg.
/home/nick/Development/arduino-1.5.8/hardware/arduino/avr/cores/arduino/Arduino.h:106:73: note: in expansion of macro 'bitClear'
#define bitWrite(value, bit, bitvalue) (bitvalue ? bitSet(value, bit) : bitClear(value, bit))
^
/home/nick/Development/arduino-1.5.8/hardware/arduino/avr/cores/arduino/Tone.cpp:159:9: note: in expansion of macro 'bitWrite'
bitWrite(TCCR0A, WGM01, 1);
^
/home/nick/Development/arduino-1.5.8/hardware/arduino/avr/cores/arduino/Arduino.h:105:39: warning: conversion to 'uint8_t {aka unsigned char}' from 'long unsigned int' may alter its value [-Wconversion]
#define bitClear(value, bit) ((value) &= ~(1UL << (bit)))
^
/home/nick/Development/arduino-1.5.8/hardware/arduino/avr/cores/arduino/Arduino.h:106:73: note: in expansion of macro 'bitClear'
#define bitWrite(value, bit, bitvalue) (bitvalue ? bitSet(value, bit) : bitClear(value, bit))
^
/home/nick/Development/arduino-1.5.8/hardware/arduino/avr/cores/arduino/Tone.cpp:160:9: note: in expansion of macro 'bitWrite'
bitWrite(TCCR0B, CS00, 1);
^
/home/nick/Development/arduino-1.5.8/hardware/arduino/avr/cores/arduino/Arduino.h:105:39: warning: conversion to 'uint8_t {aka unsigned char}' from 'long unsigned int' may alter its value [-Wconversion]
#define bitClear(value, bit) ((value) &= ~(1UL << (bit)))
I did know about the Preferences option and turning warnings on. However, as everyone knows, that causes an avalanche of error messages which is worse that no warnings. Rock...hard place...