Home > Backend Development > C#.Net Tutorial > How to perform forced type conversion in C language

How to perform forced type conversion in C language

青灯夜游
Release: 2023-01-05 16:11:14
Original
48360 people have browsed it

In C language, you can perform forced type conversion through the "(new type name) data or expression" statement; for example, "(float) 100" converts the value 100 (default to int type) to Float type, "(int)(x y)" converts the result of the expression "x y" into an integer type.

How to perform forced type conversion in C language

The operating environment of this tutorial: windows7 system, c99 version, Dell G3 computer.

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

C language has two data type methods:

  • Automatic type conversion: It is the data type conversion performed silently, implicitly, and secretly by the compiler. This This conversion does not require programmer intervention and occurs automatically.

  • Forced type conversion

Automatic type conversion is the result of the compiler's own judgment based on the context of the code. Sometimes it is not so "intelligent" ” cannot meet all needs. If necessary, programmers can also explicitly propose type conversion in the code themselves, which is called forced type conversion.

Automatic type conversion is a type conversion performed silently and implicitly by the compiler and does not need to be reflected in the code; forced type conversion is explicitly proposed by the programmer and needs to be performed through code in a specific format. Specifies a type conversion. In other words, automatic type conversion does not require programmer intervention, while forced type conversion requires programmer intervention.

The format of forced type conversion is:

(type_name) expression
Copy after login

type_name is the new type name, and expression is the expression. For example:

(float) a;  //将变量 a 转换为 float 类型
(int)(x+y);  //把表达式 x+y 的结果转换为 int 整型
(float) 100;  //将数值 100(默认为int类型)转换为 float 类型
Copy after login

The following is a classic example that requires forced type conversion:

#include <stdio.h>
int main(){
    int sum = 103;  //总数
    int count = 7;  //数目
    double average;  //平均数
    average = (double) sum / count;
    printf("Average is %lf!\n", average);
    return 0;
}
Copy after login

Running result:

Average is 14.714286!
Copy after login

sum and count are both int types. If no intervention is performed, Then the operation result of sum / count is also of int type, and the decimal part will be discarded; although the average is of double type, it can receive the decimal part, but there is insufficient energy and the decimal part is "castrated" in advance. , it can only receive the integer part, which causes the result of the division operation to be seriously distorted.

Since average is a double type, why not make full use of it and try to improve the accuracy of the operation results? To achieve this goal, we only need to convert one of sum or count to double type. In the above code, we force sum to double type, so that the result of sum / count will also become double type, and the decimal part can be retained, and the value received by average will be more accurate.

In this code, there are two points to note:

  • For division operations, if the divisor and dividend are both integers, then the result of the operation is also an integer and the decimal part will be discarded directly; if either the divisor or the dividend is a decimal, the result of the operation is also a decimal.

  • ( ) has a higher priority than /. For the expression (double) sum / count, it will First execute (double) sum, convert sum to double type, and then perform division operation. In this way, the operation result is also double type and the decimal part can be retained. Be careful not to write (double) (sum / count), otherwise the result of the operation will be 3.000000, and the decimal part still cannot be retained.

Related recommendations: "C Language Video Tutorial"

The above is the detailed content of How to perform forced type conversion in C language. 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