How to get a warning, if the C Code comparing different datatypes?
== causes both operands to to go though an implicit conversion per their relative integer conversion rank. To get a warning, do a comparison that after integer promotions, the operands differ in sign-ness.
If an int can represent all values of the original type (as restricted by the width, for a bit-field), the value is converted to an int; otherwise, it is converted to an unsigned int. These are called the integer promotions. C11dr §6.3.1.1 2
A key concept to remember is that integer promotions never changes the value, only the type. With no value change, the compare happens with no "surprises".
It is conversion that may change the value. E.g. int -1 --> unsigned UINT_MAX.
I cannot see any error or warning, Why?
With foo8 == bar below, both foo8 and bar are promoted to int. There is nothing significant to warn about. The compare is well defined for all value combinations of bar, foo8 - no surprises. That is C.
uint8_t foo8 = 0;
int16_t bar = -1;
uint32_t foo32 = 0;
int main(void) {
if (foo8 == bar) return 1;
if (foo32 == bar) return 2;
}
With foo32 == bar, foo32 becomes unsigned (or unsigned long) and bar is promoted to int. With various compiler options, you can readily get a warning like
warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
In this later case, to compare an int over its [INT_MIN...INT_MAX] range to an unsigned over its [0...UINT_MAX] range and make sure comparing -1, UINT_MAX fails, code could use:
// if (foo32 == bar) return 2;
if (bar >= 0 && foo32 == (unsigned)bar) return 2;