Type conversion in C

From Wikipedia, the free encyclopedia
Jump to: navigation, search

In the C programming language, a type conversion is the conversion two different sorts of data type into a common form, in order for them to be manipulated. The two most commonly used conversion types in the C programming language are:

1.Implict

2.Explict

In the C language there are different basic data types, such as int, char, float, double; there are also some user defined data types such as structures, arrays, etc. If the operator is taking operands of different data types, then they are converted to a common data types by certain rules. Generally, automatic conversions are those which can convert a narrower operand into a wider one without loss of information. For example, converting an integer to floating point in examples like float + integer (on 64-bit machine). A char is simply a small integer, so chars may be freely used in arithmetic expressions.

Conversion of char to integer, every character in c corresponds to a particular ASCII value. Anytime two character are values are operated using arithmetic operator their ASCII values(specific numerical value) are picked and evaluated. But when a char is converted to int can it ever produce negative value? The answer varies from machine to machine, reflecting difference in architecture. On some machines a char whose leftmost bit is 1 will be converted to a negative integer("sign extension"). On others, a char is promoted to int by adding zeroes at the left most end, and thus always positive. The definition of C guarantees that any character in the machine's standard printing character set will never be negative, so these characters will always be positive quantities in expressions.

Relational operators like i>j and logical expressions connected by && and || are defined to have values 1 if true, and 0 if false. Thus the assignment

d= c>= '0' && c<=='9' sets d to i if c is a digit, and 0 if not. However, function isdigit() may return any non-zero value for true. In test part of if, while, for, etc. "true" just means any non-zero value, so this makes no difference. Implicit arithmetic conversions work much as expected. In general, if an operator like + or * that take two operands (a binary operator) has operands of different types, than the "lower" is promoted to "higher" type before the operator proceeds. The result is of the type high. Consider the below example.

         float   fVal;
         double  dVal;
         int   iVal;
         unsigned long ulVal;
         dVal = iVal * ulVal; /* iVal converted to unsigned long
                                        * Uses step 4.
                                        * Result of multiplication converted to double 
                                        */
         dVal = ulVal + fVal; /* ulVal converted to float
                                                              * Uses step 3.
                                                              * Result of addition converted to double 
                                                              */

Rules for conversion if there is no unsigned operand involved:

If either operand is long double, convert the other to long double. Otherwise, if either operand is double, convert the other to double. Otherwise, if either operand is float, convert the other to float. Otherwise, convert char and short to int Then, if either operand is long, convert the other to long.

Notice, that floats in an expression are not automatically converted to double; this is a change form the original definition. In general, mathematical functions like those in <math.h> will use the double precision. The main reason for using float is to save storage in large arrays, or, less often, to save time on the machines where double- precision arithmetic is particularly expensive.

Conversion rules are more complicated when unsigned operators are involved. The problem is that comparisons between signed and unsigned values are machine-dependent, because they depend on the size of the various integer types. For example, suppose that int is 16 bits and long is 32 bis. Then -1L < 1U, because 1U which is an int, is promoted to signed long. But -1L > 1UL, because -1L is promoted to unsigned long and thus appears to be large positive number.

Conversion in assignment operator, the value on right side is converted to value of the left hand side. A character is converted to int. Or Longer integer is converted to char(smaller) by dropping higher-order bits. Thus in

         int i;
         char c;
         i=c; 
         c=i;

the value of c is unchanged.

Explicitly, forced conversion can be done also be sone (type-name) expression

         int i;
         (long) i

Conversion to Bool[edit]

When converted to Bool anything that is not 0 or NULL is true. (while all known implementations use 0 for NULL this is not required by the standard)

[1]

References[edit]