C language data type conversion Instance Code _c language

Source: Internet
Author: User

Data type conversion is the conversion of data (the result of a variable, an expression) from one type to another. For example, to save decimals you can convert a variable of type int to a double type.

The general format for data type conversions is:

(type_name) expression

Type_name for the type of data to be converted to, expression is an expression. For example:

(float) A; Convert A to a solid (
int) (x+y);//Convert the result of X+y to Integer
(float) 100;//Convert a constant to a solid type

Example converts an integer to a floating-point number:

#include <stdio.h>
int main () {
 int sum =, count = 5;
 double mean;
 mean = (double) sum/count;
 printf ("Value of Mean:%f\n", mean);
 return 0;
}

Run Result:

Value of mean:3.400000

Note that the type conversion operator () has a precedence higher than / ,(double) Sum/count converts sum to double, and then division. If writing (double) (sum/count) , then the run result is 3.000000.

This transformation, which is explicitly performed by programmers, is called coercion type conversion . In addition to enforcing type conversions, the compiler implicitly makes data type conversions in mixed operations of different data types, called automatic type conversions .

Automatic type conversions Follow the following rules:

1. If the data types participating in the operation are different, they are converted to the same type and then the operation is performed.

2. The conversion is in the direction of increasing the length of the data to ensure the accuracy is not reduced. For example, int and long operations, the int is converted to a long type before the operation.

3. All floating-point operations are done in double precision, even if only the expression containing the float single precision operation is converted to double, and then the operation is performed.

When the 4.char and the short types participate in the operation, they must first be converted to an int type.

5. In assignment operations, the data types on both sides of the assignment number are not the same, and the type of the right expression needs to be converted to the type of the left variable. If the right expression has a data type longer than the left, it loses part of the data, which reduces the precision.

The following figure shows the rules for type auto conversion:

Sample automatic data type conversion.

#include <stdio.h>
int main () {
 float pi=3.14159;
 int S1, r=5;
 Double S2;
 S1 = R*r*pi;
 s2 = r*r*pi;
 printf ("s1=%d, s2=%f\n", S1, S2);
 return 0;
}

Run Result:

s1=78, s2=78.539753

When you evaluate an expression R*r*pi, both R and PI are converted to the double type, and the result of the expression is also the double type. However, because the S1 is integral type, the result of the assignment operation is still integral type, and the small number part is given away.

Note that the fractional portion is dropped directly, rather than rounded forward by rounding.

Whether it is a cast or an automatic conversion, it is only a temporary conversion of the data length of a variable for the purposes of this operation, without changing the type defined on the variable when the data is described.

Take a look at the following example:

#include <stdio.h>
int main () {
 float f=5.75;
 printf ("(int) f=%d, f=%f\n", (int) F, f);
 return 0;
}

Run Result:

(int) f=5, f=5.750000

The above is the C language data type conversion examples of detailed, hope to help learn the basic knowledge of C language students.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.