In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
Most people do not understand the knowledge points of this article "what is the role of C language type conversion and how to use it", so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article, let's take a look at this article "what the role of C language type conversion is and how to use it".
I. conversion between types
Data types in C language can be converted
Forced type conversion
Implicit type conversion
II. Mandatory type conversion
Syntax for casting
(Type) var_name
(Type) value
The result of a cast
The target type can accommodate the target value: the result remains the same
The target type cannot hold the target value: the result will be truncated
Note: not all casts are successful, and when casts cannot be performed, the compiler will generate error messages (such as converting custom data types to basic data types).
Let's take a look at a piece of code that enforces a cast:
# include struct TS {int i; int j;}; struct TS ts; int main () {short s = 0x1122; char c = (char) s; / / 0x22 int I = (int) s; / / 0x00001122 int j = (int) 3.1415; / / 3 unsigned int p = (unsigned int) & ts; / / long l = (long) ts; / / error / / ts = (struct TS) l / / error printf ("s =% x\ n", s); printf ("c =% x\ n", c); printf ("I =% x\ n", I); printf ("j =% x\ n", j); printf ("p =% x\ n", p); printf ("& ts =% p\ n", & ts); return 0;}
The following is the output:
Note that converting custom data types to basic data types will result in an error.
III. Implicit type conversion
Implicit type conversion is a type conversion actively performed by the compiler.
Note:
Implicit type conversion from low type to high type is safe and does not produce truncation.
Implicit type conversion from high type to low type is unsafe and leads to incorrect results
Implicit type conversion in expressions
The occurrence point of implicit type conversion
In arithmetic expressions, low types are converted to high types
In an assignment expression, the value of the expression is converted to the type of the variable on the left
When a function is called, the argument is converted to the type of formal parameter
Function returns a value, and the return expression is converted to the return value type
Secure implicit type conversion:
Let's take a look at an implicit type conversion code:
# include int main () {char c = 'a'; int i = c; / safe unsigned int j = 0x11223344; short s = j; / unsafe printf ("c =% c\ n", c); printf ("I =% d\ n", I); printf ("j =% x\ n", j); printf ("s =% x\ n", s) Printf ("sizeof (c + s) =% d\ n", sizeof (c + s)); return 0;}
The following is the output:
C is char type, s is short type. Before the four operations, each addition narrower than int is automatically raised to int, and the result is also int type, so size is 4.
The above is the content of this article on "what is the function of C language type conversion and how to use it?" I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please follow the industry information channel.
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.