In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces "how to use C language data type conversion". In the daily operation, I believe that many people have doubts about how to use C language data type conversion. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about how to use C language data type conversion! Next, please follow the editor to study!
Data type conversion
Data type conversion is the conversion of data (variables, values, the results of expressions, and so on) from one type to another.
Automatic type conversion
Automatic type conversion is a data type conversion silently, implicitly, and secretly performed by the compiler, which occurs automatically without the intervention of the programmer.
1) automatic type conversion occurs when one type of data is assigned to another type of variable, for example:
Float f = 100
100 is data of type int and needs to be converted to type float before it can be assigned to the variable f. Another example is:
Int n = f
F is data of type float and needs to be converted to type int before it can be assigned to the variable n.
In the assignment operation, the data types on both sides of the assignment sign are different, so it is necessary to convert the type of the expression on the right to the type of variable on the left, which may lead to data distortion (data input and output are inconsistent), or reduced precision; so, automatic type conversion is not necessarily safe. For unsafe type conversions, the compiler generally gives a warning. In different types of mixed operations, the compiler will also automatically convert data types, converting all the data involved in the operation to the same type before calculating. The rules for the conversion are as follows:
The conversion is carried out in the direction of increasing the length of the data to ensure that the value is not distorted or the accuracy is not reduced. For example, when int and long participate in the operation, the data of type int is converted to type long before the operation is performed.
All floating-point operations are performed with double precision, and even if there is only float type in the operation, it must be converted to double type before the operation can be carried out.
When char and short participate in the operation, they must first be converted to the int type.
Example of automatic type conversion:
# include 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;}
Operation result: S1-78, s2-78-78.539749
When evaluating the expression r*r*PI, both r and PI are converted to type double, and the result of the expression is also type double. However, because S1 is an integer, the result of the assignment operation is still an integer, leaving out the decimal part, resulting in the loss of data accuracy.
Forced type conversion
Automatic type conversion is the result of the compiler's own judgment based on the context of the code, and sometimes it is not so "intelligent" that it does not meet all the requirements. If necessary, programmers can also explicitly mention type conversion in their code, which is called forced type conversion.
The format of the cast is:
(type) expression
Type is the target type name and expression is the expression. For example:
(float) a; / / convert the variable a to the float type (int) (xchangy); / / convert the result of the expression xroomy to the int integer type (float) 100; / / convert the numeric value 100 (default to int type) to float type
The following is a classic example that requires a cast:
# include int main () {int sum = 315; / / Total int count = 7; / / number double average; / / average average = (double) sum / count; printf ("Average is% lf!\ n", average); return 0;} output:Average is 44.714286!
Both sum and count are of int type, so if there is no intervention, the operation result of sum / count is also int, and the fractional part will be discarded; although average is the double type, it can accept the fractional part, but the effort is not enough, the fractional part is "castrated" in advance, and it can only receive the integer part, which leads to serious distortion of the result of division operation.
Since average is a double type, why not make full use of it and improve the accuracy of the results as much as possible? To achieve this, we just need to convert one of the sum or count to the double type. In the above code, we cast sum to type double so that the result of sum / count will also be of type double, so that the fractional part can be preserved and the value received by average will be more accurate.
Be careful not to write (double) (sum / count), so the result of writing operation will be 44.000000, still can not retain the decimal part.
At this point, the study on "how to use C language data type conversion" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.