Wednesday, August 29, 2012

c programming - Type Convertions

In c, generally automatic convertions convert a "narrower" operand into a "wider" one without losing data,

Expressions that causes data lose (such as assigning long to int) are allowed but may produce a warning (depends on the compiler)

Here's an interesting situation:
The expression: -1L < 1U evaluates to true,

This is because 1U (which is an int) is converted to a signed long,

Lets take a look at the following expression:
-1L > 1UL

This expression evaluates to true as well, this is because -1L is converted to an unsigned long, which appears as a large positive number.

No comments: