C-language data type conversion

Source: Internet
Author: User

The data type of the variable can be converted. There are two ways of converting, one is auto-conversion and one is casting.

Automatic conversion

Automatic conversion occurs when the volume blending operation of different data types is done automatically by the compilation system. Automatic conversions Follow these rules:

    1. If the type of the participating operands is different, it is first converted to the same type and then the operation is performed.
    2. Conversions are performed in the direction of increased data length to ensure that the accuracy is not reduced. such as int and long operation, the int is converted into a long type before the operation.
    3. All floating-point operations are performed in double-precision, even if the expression containing only float single-precision arithmetic is converted into double type and then calculated.
    4. Char and short participate in the operation, you must first convert to int type.
    5. In an assignment operation, the type of the value on the right side of the assignment number is converted to the type of the left quantity, not the same as the data type of the quantity on either side of the assignment number. If the right amount of data type is longer than the left, part of the data is lost, which reduces precision and the missing part is rounded forward.


Represents a rule for type auto-conversion.


"Example 3-12" Automatic data type conversion

  1. #include<stdio.h>
  2. int main(){
  3. float PI=3.14159;
  4. int s, r=5;
  5. s=r*r*pi;
  6. printf("s=%d\ n", S);
  7. return 0;
  8. }

In this case, pi is the real type; S,r is integer. When executing the S=R*R*PI statement, both R and Pi are converted to double, and the result is a double type. But because S is an integral type, the result of assignment is still integer, and the decimal part is given away.

Forcing type conversions

Coercion of type conversions is accomplished through type conversion operations. The general form is:
(type specifier) (expression)
Its function is to cast the result of an expression into a type that indicates the type represented descriptor.

For example:

    1. (float) a; / * Convert A to a real type * /
    2. (int) (x+y); / * Convert the results of x+y to integral type * /


You should be aware of the following issues when using casts:

    • Both the type specifier and the expression must be enclosed in parentheses (a single variable can be without parentheses), such as (int) (X+Y) written (int) x+y is converted to an int and then added to Y.
    • Either a cast or an automatic conversion is just a temporary conversion of the data length of the variable for the purpose of this operation, without changing the type defined for the variable at the time the data is described.


"Example 3-13" forcing data type conversions

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


This example shows that although F is coerced to int, it only works in the operation, it is temporary, and the type of F itself does not change. Therefore, the value of (int) F is 5 (the decimal is deleted) and the value of F is still 5.75.

C-language data type conversion

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.