当前位置:网站首页>Automatic conversion and cast
Automatic conversion and cast
2022-07-28 16:20:00 【Foam o0】
The types of variables can be converted to each other , Conversion is divided into automatic conversion and forced conversion .
Automatic conversion rules :
- 1、 If the type of computation involved is different , First convert to the same type , And then you do the math .
- 2、 The conversion proceeds in the direction of increasing data length , To ensure that the accuracy does not decrease . Such as int The type and long Type operation , The first int Quantity into long Type and then do the operation .
a、 If the number of bytes of the two types is different , Convert to a type with a high number of bytes
b、 If the number of bytes of the two types is the same , And a sign , An unsigned , Then convert it to Unsigned type
- 3、 All floating-point operations are performed with double precision , Even if it only includes float Expression of single precision operation , It also needs to be converted into double type , Do some more calculations .
- 4、char The type and short When type is involved in the operation , It must be converted to int type .
- 5、 In assignment operations , The data types of the quantities on both sides of the assignment number are different , Assignment number The type of the right quantity will be converted to the type of the left quantity .
If the data type length of the quantity on the right is longer than that on the left , Some data will be lost , This will reduce the accuracy :
int a=1; double b=2.5; a=b; cout << a; // Output is 2, Missing decimal part
int a = 1; double b = 2.1; cout << "a + b = " << a + b << endl; // Output is a + b = 3.1
Cast rules :
Casts are enforced through Type conversion operations To achieve .
Its general form is :( Type specifier )( expression ) Its function is to cast the operation result of an expression into the type represented by a type specifier .
int a = 1; double b = 2.1; cout << "a + b = " << a + (int)b << endl; // Output is a + b = 3
边栏推荐
- R language ggplot2 visually draws line plots, and uses gghighlight package to highlight the lines that meet the combination judgment conditions in the line graphs (satisfies both condition a and b)
- Ask if you don't understand, and quickly become an advanced player of container service!
- 便携式钻孔测斜仪数据采集仪测量原理与测斜探头的连接及使用方法
- Event express | Apache Doris Performance Optimization Practice Series live broadcast course is open at the beginning. You are cordially invited to participate!
- leetcode 题目
- Paging query in applet
- 跳表的实现
- Implementation of skip table
- Installation points and precautions of split angle probe
- 记录一下 clearfix 清除浮动
猜你喜欢

js 链表 02

Common problems and precautions of remote serial port server (adapter) uart/i2c/1-wire/spi PS304

Remote serial port server (adapter) UART to 1-wire application

Ask if you don't understand, and quickly become an advanced player of container service!

One channel encoder, two channels Di speed measurement, RS485 serial port connected to one channel do alarm module ibf151

Multipurpose mixed signal 8ai/4di/do to serial port rs485/232modbus acquisition module ibf30

光学雨量计应用降雨量检测

JS stack

Thermistor PT100, NTC to 0-10v/4-20ma converter

How to effectively conduct the review meeting (Part 1)?
随机推荐
Paging query in applet
2021 Yahong pen test questions
Dynamic programming -- digital statistics DP
不懂就问,快速成为容器服务进阶玩家!
Darknet training yolov4 record
High precision absolute angle sensor application high speed angle monitoring
Advantages of optical rain gauge over tipping bucket rain gauge
[Multisim Simulation] LM339 zero crossing circuit simulation
2.855 billion yuan! Qingdao Xinen completed the capital increase: Xingcheng Jidian became the largest shareholder, holding 57.10%
Deeply understand the fusing configuration of istio traffic management
Using SYSTEMd to manage services
js中的for循环总结
LabVIEW LINX Toolkit控制Arduino设备(拓展篇—1)
How to quickly access the unified authentication system
[live broadcast reservation] a new challenge under the evolution of data architecture - Shanghai railway station
How to measure the vibrating wire sensor by vibrating wire acquisition module?
Zhaoqi science and technology innovation and entrepreneurship competition talent introduction platform, mass entrepreneurship and entrepreneurship competition high-level talent introduction
【微信小程序开发(七)】订阅消息
远距离串口服务器( 适配器)UART 转 1-Wire 应用
2021 肯特面试题1