Technorati labels: C language, data type, conversion, implicit, data type, conversion, convert, cast
C Language stipulates that different types of data must be converted to the same type before calculation, hybrid operations can be performed between integer, real, and numeric data through type conversion (but not between all types ).
When different types of variables are mixed for calculation, type conversion may occur.
For data of the same type, there are rules for conversion:
The character must be converted to an integer (the C language specifies that the character type data can be used between integer data and integer data ).
Convert short type to int type (same as integer type ).
When values are assigned, the right value is converted to the left type.
In addition,
When integer data and double-precision data are computed, C first converts the integer data into double-precision data, and then computes the result into double-precision data.
When compute data and real data are computed, C first converts the compute data into real data and then computes the data.
ExampleProgram, Test environment vc6:
/* C language data type conversion in C programming language */
# Include <stdio. h>
Void main ()
{
Int int1 = 1;
Unsigned unsigned1 = 2;
Short short1 = 1;
Char char1 = 'a ';
Long long1 = 1l;
Float float1 = 2.0f;
Double double1 = 3.0l;
printf ("\ n memory used by various data types: \ n");
printf ("sizeof (INT ): % d byte \ n ", sizeof (int1);
printf (" sizeof (unsigned): % d byte \ n ", sizeof (unsigned1 ));
printf ("sizeof (short): % d byte \ n", sizeof (short1);
printf ("sizeof (char ): % d byte \ n ", sizeof (char1);
printf (" sizeof (long): % d byte \ n ", sizeof (long1 ));
printf ("sizeof (float): % d byte \ n", sizeof (float1);
printf ("sizeof (double ): % d byte \ n ", sizeof (double1);
printf ("\ n Calculation for data of the same type: \ n");
printf ("sizeof (INT + INT): % d byte \ n ", sizeof (int1 + int1);
printf ("sizeof (unsigned + unsigned): % d byte \ n", sizeof (unsigned1 + unsigned1 ));
printf ("sizeof (SHORT + SHORT): % d byte \ n", sizeof (short1 + short1 )); /* convert to int */
printf ("sizeof (char + char): % d byte \ n", sizeof (char1 + char1 )); /* convert to int */
printf ("sizeof (long + long): % d byte \ n", sizeof (long1 + long1 ));
printf ("sizeof (float + float): % d byte \ n", sizeof (float1 + float1);
printf ("sizeof (double + double): % d byte \ n ", sizeof (double1 + double1);
Printf ("\ n calculation between different types of data: \ n ");
Printf ("sizeof (INT + unsigned): % d byte \ n", sizeof (int1 + unsigned1);/* convert to int */
Printf ("sizeof (INT + char): % d byte \ n", sizeof (int1 + char1);/* convert to int */
Printf ("sizeof (INT + long): % d byte \ n", sizeof (int1 + long1);/* convert to long */
Printf ("sizeof (INT + float): % d byte \ n", sizeof (int1 + float1);/* convert to float */
Printf ("sizeof (char + float): % d byte \ n", sizeof (char1 + float1);/* convert to float */
Printf ("sizeof (float + double): % d byte \ n", sizeof (float1 + double1);/* convert to double */
}
Running result:
Memory space occupied by various data types:
Sizeof (INT): 4 bytes
Sizeof (unsigned): 4 bytes
Sizeof (short): 2 bytes
Sizeof (char): 1 byte
Sizeof (long): 4 bytes
Sizeof (float): 4 bytes
Sizeof (double): 8 bytes
Perform operations between data of the same type:
Sizeof (INT + INT): 4 bytes
Sizeof (unsigned + unsigned): 4 bytes
Sizeof (SHORT + SHORT): 4 bytes
Sizeof (char + char): 4 bytes
Sizeof (long + long): 4 bytes
Sizeof (float + float): 4 bytes
Sizeof (double + double): 8 bytes
Perform operations between different types of data:
Sizeof (INT + unsigned): 4 bytes
Sizeof (INT + char): 4 bytes
Sizeof (INT + long): 4 bytes
Sizeof (INT + float): 4 bytes
Sizeof (char + float): 4 bytes
Sizeof (float + double): 8 bytes