当前位置:网站首页>C language data type conversion rules (implicit conversion + explicit conversion)
C language data type conversion rules (implicit conversion + explicit conversion)
2022-06-22 05:07:00 【Jiangxueqian】
Type conversion during assignment
If the types on both sides of the assignment operator are inconsistent , But when they are all basic types , Type conversion is required during assignment . Type conversion is performed automatically by the system , The rule of transformation is :
- Convert floating point data ( Including single 、 Double precision ) When you assign an integer variable , First round the floating-point number , That is, the decimal part is discarded , Then give an integer variable .
- Assign integer data to a single 、 Double precision variable , Constant value , But it's stored in a variable as a floating point number .
- Will a double Type data is assigned to float variable , First convert double precision numbers to single precision numbers , That is to say, only 6~7 Significant digits , Store in float Type variable 4 In bytes . It should be noted that the size of the double precision value cannot exceed float The range of values for a variable of type .
- When character data is assigned to an integer variable , The ASCII The code value is assigned to an integer variable .
- Assign an integer data with more bytes to an integer variable or character variable with less bytes ( For example, occupy 4 Bytes of int Type data is assigned to account for 2 Bytes of short Variable or percentage 1 Bytes of char Variable ) when , Only send its low byte intact to the assigned variable ( That is, truncation occurs ).
——《C Programming ( The fifth edition )》61 page
Implicit type conversion
- When it appears in an expression , Signed and unsigned char and short Will be automatically converted to int Participate in operation . When short And int When the size is the same ,unsigned short Will be converted into unsigned int.
- In any operation that contains two data types , Both values are converted to the higher level of the two types ( be called “ promote ”), Then participate in the calculation , Only data of the same type can be directly calculated .
- The order of type level from high to low is :long double 、double、float、unsigned long long、long long、 usinged long、long、unsigned int、int.
- When long and int Have the same size , here unsigned int Than long A higher level . The reason why... Is not involved char and short , Because they have been elevated to int perhaps unsigned int.
- Examples demonstrate
3 + 4 / 5.0f + 6 - 9.0(double)
According to operator priority , To calculate 4 / 5.0f, So first put 4 convert to float 4.0f The calculation result is 0.8f
3 + 0.8f + 6 - 9.0
Then according to the order from left to right :
First calculate 3 + 0.8f, 3 convert to 3.0f The calculation result is 3.8f
Calculate again 3.8f + 6, 6 Convert to 6.0f The calculation result is 9.8f
9.8f - 9.0
9.8f Convert to double 9.8 The calculation result is 18.8(double)
Not all of them are converted to double
- In the assignment statement , The final result of the calculation will be converted to the type of the variable to the left of the assignment operator , This process may be upgraded , May also be degraded , Demotion is converting a value to a lower type .
int a = 2022.22; // double turn int, Downgrade
double b = 22; // int turn double, upgrade
- When passed as an argument to a function ,char and short Will be automatically converted to int,float Will be converted into double, Function prototypes can be used to prevent elevation .
- Ascension is usually a smooth, lossless process , But downgrades can lead to “ truncation ”.
——《C Primer Plus( The fifth edition )》105 page
Explicit type conversion ( Cast )
form :(type) data / expression
22 + 22.22 // Implicit conversion , The result is double type
22 + (int)11.11 // I will put 11.11 convert to 11 And again 22 Add up , The result is int type
边栏推荐
- C语言数据类型转换规则(隐式转换+显式转换)
- Concurrent programming - thread pool
- Go learning (II. Built in container)
- Solutions to MySQL 8.0 public key retrieval is not allowed errors
- zipimport.ZipImportError:
- What problems will be encountered during the implementation of MES management system
- In 2022, the super intern plans to make a breakthrough in the offer of it famous enterprises, and the nine high salary skills help the dream of famous enterprises
- 使用matplotlib实现GUI交互效果
- The yarn deployment mode depends on the pre upload settings
- VirtualBox 6.1.34 release
猜你喜欢

JUC - thread interrupt and thread waiting and wakeup (locksupport)

NLP 的 不可能三角?

C语言变量的存储方式和生存期

Progress information collection for progress control of Zhiyuan project management SPM system

It is easy to analyze and improve R & D efficiency by understanding these five figures

汉诺塔问题
![[details] domestic website filing process and steps](/img/be/be74c1e586ff01403fcafff19a462a.jpg)
[details] domestic website filing process and steps

What problems will be encountered during the implementation of MES management system

Arrangement of soft test subjects in the second half of 2022

非递归打印斐波那契数列
随机推荐
获取DPI函数返回值永远是96 | 获取DPI函数返回值不正确 | GetDpiForMonitor/GetDeviceCaps返回值不正确的原因
flink部署模式(二)- yarn三种部署模式
yarn部署模式依赖预上传设置
【使用指南】清华源的使用
Remote dictionary server (redis) - a kV based NoSQL database management system used as a cache
Software architecture and pattern: structure, component and relationship
numpy库常用知识整理
LeetCode——二叉搜索树的第k大节点(借助中序遍历)
Will swift compile to native code- Does Swift compile to native code?
Debugging wechat built-in browser with chrome
Progress warning and problem management of progress control in Zhiyuan project management SPM system
NHibernate method for viewing generated SQL statements
Reading notes on how MySQL works [updating]
Great! Huaibei and Huaibei enterprises are approved to use special marks for geographical indication products
mysql day02课堂笔记
Exness: ECB president Christine Lagarde reiterated her plan to raise interest rates at the July meeting
Lottie components make animation easier
9. Gateway cross domain processing
NLP 的 不可能三角?
Gateway uses global filter for token verification