what is char i=0x80 and why overflow did not happen in bit shifting

In C, a char is an integer type used to store character data, typically 1 byte.

The value stored in i is 0x80 a hexidecimal constant that is equal to 128.

An arithmetic operation on two integer types (such as i << 1) will promote to the wider type, in this case to int, since 1 is an int constant. In any case, integer function arguments are promoted to int.

Then you send the result to printf, with a %d format specifier, which mean "print an integer".


I think that K&R have the best answer to this question:

2.7 Type Conversions When an operator has operands of different types, they are converted to a common type according to a small number of rules. In general, the only automatic conversions are those that convert a narrower'' operand into awider'' one without losing information, such as converting an integer into floating point in an expression like f + i. Expressions that don't make sense, like using a float as a subscript, are disallowed. Expressions that might lose information, like assigning a longer integer type to a shorter, or a floating-point type to an integer, may draw a warning, but they are not illegal. A char is just a small integer, so chars may be freely used in arithmetic expressions.

So i<<1 converts i to int before it is shifted. Ken Vanerlinde has it right.

Tags:

C