C Type Conversion
When variables of one type are mixed with variables of another type, a type-conversion will occur.
In an assignment, the type conversion rule is easy:
The value of the right side(expression side) of the assignment is converted to the type of the left side (target variable), as illustrated here:
Example Type Conversion
ch = x; // line 1 x = f; // line 2 f = ch; // line 3 f = x // line 4
- In line 1, the left high order bits of the integer variable x are lopped off, leaving ch with the lower 8 bits. If x were between 255 and 0, ch and x would have identical values.
- Otherwise, the value of ch would reflect only the lower-order of bits of x.
- In line 2, x will receive the non-fractional part of f.
- In line 3, f will convert the 8-bit integer value stored in ch to the same value in the floating point format.
- In line 4, except that f will convert an integer value into floating-point format.
Some Important points:
When converting from integers to characters and long integers to integers, the appropriate amount of high order bits will be removed.
In many 16-bit environments, this means that 8 bits will be lost when going from an integer to a character and 16-bits will be lost when going from a long integer to an integer.
For 32-bit environments, 24 bits will be lost when converting from an integer to a character.
C is a general-purpose, imperative computer programming language, supporting structured programming, lexical variable scope and recursion, while a static type system prevents many unintended operations. By design, C provides constructs that map efficiently to typical machine instructions, and therefore it has found lasting use in applications that had formerly been coded in assembly language, including operating systems, as well as various application software for computers ranging from supercomputers to embedded systems.
C was originally developed by Dennis Ritchie between 1969 and 1973 at Bell Labs, and used to re-implement the Unix operating system. It has since become one of the most widely used programming languages of all time, with C compilers from various vendors available for the majority of existing computer architectures and operating systems. C has been standardized by the American National Standards Institute (ANSI) since 1989 and subsequently by the International Organization for Standardization (ISO).
- Lectures 19
- Quizzes 0
- Duration 50 hours
- Skill level All levels
- Language English
- Students 2
- Certificate No
- Assessments Self