C language data type conversion method

藏色散人
Release: 2020-02-26 09:31:31
Original
8792 people have browsed it

C language data type conversion method

C language data type conversion method

Data type conversion is to convert data (variables, values, expressions result, etc.) from one type to another.

Automatic type conversion

Automatic type conversion is a data type conversion performed silently, implicitly, and secretly by the compiler. This conversion does not require programmer intervention. , will happen automatically.

1) Automatic type conversion will occur when one type of data is assigned to another type of variable. For example:

float f = 100;
Copy after login

100 is int type data and needs to be converted to Only float type can be assigned to variable f. Another example:

int n = f;
Copy after login

f is float type data and needs to be converted to int type before it can be assigned to variable n.

In the assignment operation, when the data types on both sides of the assignment number are different, the type of the expression on the right needs to be converted to the type of the variable on the left, which may cause data distortion or reduced accuracy; therefore, automatic typing Conversion is not necessarily safe. Compilers generally give warnings for unsafe type conversions.

2) In mixed operations of different types, the compiler will also automatically convert data types, converting all data involved in the operation to the same type first, and then perform calculations. The conversion rules are as follows:

Conversion is performed in the direction of increasing data length to ensure that the value is not distorted or the accuracy is not reduced. For example, when int and long are involved in operations, the int type data is first converted into long type and then the operation is performed.

All floating point operations are performed in double precision. Even if there is only float type in the operation, it must be converted to double type before operation can be performed.

When char and short participate in operations, they must be converted to int type first.

The following figure describes this conversion rule more vividly:

unsigned is also unsigned int. At this time, you can omit int and just write unsigned.

Automatic type conversion example:

#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;
}
Copy after login

Running result:

s1=78, s2=78.539749
Copy after login

When calculating the expression r*r*PI, both r and PI are converted to double type, expressing The result of the formula is also of double type. However, since s1 is an integer, the result of the assignment operation is still an integer, and the decimal part is discarded, resulting in data distortion.

For more programming related content, please pay attention to the Programming Introduction column on the php Chinese website!

The above is the detailed content of C language data type conversion method. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template