Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What conversion rules do different types of data in C language follow?

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "what are the conversion rules for different types of data in C language". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn "what conversion rules do different types of data in C language follow?"

1. Automatic type conversion

In C, automatic type conversion follows the following rules:

If ① participates in different types of computation, it will be converted to the same type first, and then the operation will be performed.

② conversion is carried out in the direction of increasing data length to ensure that the accuracy does not decrease. For example, in the operation of int and long, the amount of int is first converted to long before the operation is carried out.

A. if the number of bytes of the two types is different, convert them to the type with a high number of bytes.

B. if the two types have the same number of bytes, and one is signed and the other is unsigned, it is converted to an unsigned type

All floating-point operations in ③ are performed with double precision. Even the expressions of two float single-precision operations have to be converted to double before the operation is performed.

When taking part in the operation, ④ char and hammer must first be converted to int.

⑤ in the assignment operation, when the data types of the two sides of the assignment are different, the type of the right side of the assignment will be converted to the type of the left side.

If the data type length on the right side is longer than that on the left, part of the data will be lost, which will reduce the precision, and the missing part will be rounded forward.

Correction: after feedback from bloggers here, the measured missing parts under the codes VS and Linux are directly rounded rather than rounded.

two。 Forced type conversion

The general format of cast is as follows:

(type name) (expression)

This cast does not change the Operand itself

First, an experiment is conducted to define a signed int data and a unsigned int data, respectively, and then compare the sizes:

Unsigned int a = 20signed int b =-130b? Or b > a? The experiment shows that b > a, that is to say,-130 > 20, why is there such a result?

This is because in C language operations, if you encounter an operation between unsigned and signed numbers, the compiler will automatically convert them to unsigned numbers to deal with them, so a comparison of 20 numbers 4294967166 will certainly be b > a.

The unsigned int a=20signed int output is 4294967186, and by the same token, before the operation, axiom 2010b is converted to 4294967166, so a+b=4294967186

The results of subtraction and multiplication are similar. Uint I asked what the result was

At this point, I believe you have a deeper understanding of "what conversion rules different types of data in C language follow". 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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report