In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "what is the method of implicit type conversion and forced type conversion in C language". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "what is the method of implicit type conversion and forced type conversion in C language?"
Type conversion
There are different types of data, and the problem of type conversion is inevitably involved in the mixed operation between different types of data.
Conversions include implicit type conversions and cast types.
The principle of type conversion: types that occupy a small number of bytes of memory (small range) are converted to types that occupy a large number of bytes of memory (large range) to ensure that the accuracy is not reduced.
Implicit type conversion
Implicit conversion, also known as automatic conversion, follows certain rules and is automatically completed by the compiler.
The integer arithmetic of C is always performed with at least the precision of the default integer type. in order to obtain this precision, the characters and short shaping operands in the expression are converted to normal integers before they are used, which is called integer promotion.
Integer promotion: general CPU is difficult to directly achieve the direct addition of two 8-bit operations. So all kinds of integer values in the expression that may be less than the int length must be converted to int or unsigned int before they can be sent to CPU to perform the operation.
Code example 1:
Int main () {char a = 5, b = 126 / / 5char 00000000000000000000000 00000101 / / 126char 000000000000000001111110 / / when an and b are added together, it is char type, and integer promotion / / int c = 00000000000000000000101 + 000000000000001111110 / char c = 10000011 (integer truncation) / / print again in memory Then print the original code / / int c = 11111111111111111111111110000011 (complement) / / print the original code: 1000000000000000000000000011111101 char c = a + b Printf ("% d\ n", c); return 0;}
How to promote an integer: promote according to the symbolic bits of the data type of the variable.
Code example 2:
/ negative integer promotion char S1 =-1; there are only eight bits in the binary complement of variable S1 because char is signed, so when the integer is promoted, the high-order supplementary symbol bit is: 11111111 11111111 11111111 / positive integer promotion char S2 = 1; in the binary complement of variable S2, there are only eight bits 00000001 because char is signed, so when the integer is promoted, the high supplementary symbol bit is: 0000000000000000000000000001
Integer lifting of unsigned numbers, high-order direct complement 0
Code example 3:
Implicit conversion
Int main () {int num = 5; printf ("S1% d\ n", num / 2); printf ("s2=%lf\ n", num / 2.0); return 0;} s1=2s2=2.500000 Please press any key to continue. . . Forced type conversion
Cast refers to the use of cast operators to convert a variable or expression to the desired type
Code example 1:
# include int main () {float x = 0; int I = 0; x = 3.6f; I = x; / x is real, I is integer, direct assignment will have warning I = (int) x; / / use cast printf ("x% f, iCy% d\ n", x, I); return 0 At this point, I believe you have a deeper understanding of "what is the method of implicit type conversion and forced type conversion in C language". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.