C Type Conversion detailed

Source: Internet
Author: User

when several operand types of an operator are inconsistent, they need to be converted to the same type

In general, automatic conversion is to convert "narrower" to "relatively wide" without losing information. For example: F + I convert I to f

Meaningless expressions are not allowed, such as: subscript cannot be a floating-point type

For the conversion of lost intensive reading, the compiler will give a warning: for example, convert int to char type. However, the operation is not illegal.

The C language standard does not specify whether the char type is unsigned or signed, and when the char type is converted to int, the results are not the same for different machines

In some machines, if a char has a maximum bit of 1, it is converted to a complex number, while in other machines the char is always converted at a high of 0.

The C language definition guarantees that the standard print character set of the machine is not negative, so these characters in the expression value positive.

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/5B/E7/wKiom1UVSECihqnOAASAHC6KpLE550.jpg "title=" 9213b07eca8065387d4c671896dda144ad348213.jpg "alt=" Wkiom1uvsecihqnoaasahc6kple550.jpg "/> As on the Ascⅱ Code table, the highest bit is always 0, Avoids the possibility of a negative character set

PS: In order to ensure the consistency of the program, if you want to store non-characters in char, it is best to specify signed or unsigned

In the Windows platform using vc++6.0, char defaults to signed

Implicit conversions:

The principle is to convert the lower type to a higher type, and if there is no unsigned, the following informal rules can be used;

If one is a long double, the other is converted to a long double type

If one is double, the other is converted to double

If one is float, the other is converted to float

Convert char and short to int

If one is long, convert the other to a long

Conversion rules are more complex when unsigned is included

For example: Suppose int is 16 bits, long is 32 bits

then-long<unsigned int, because unsigned will be converted to signed long.

But-long>unsigned long, because-long will be promoted to unsigned long.

specific conversion rules see the end of the article ...


When a longer integer is converted to a shorter integer or char, the part that is exceeded is discarded

When a double is converted to float, it is rounded or truncated depending on the specific implementation of the compiler

Cast:

(Type-name) expressions such as float B = 1.0; int a = (int) b;

Note Correct understanding of casts : It is conceivable that a temporary variable exists, that the expression is first assigned to a temporary variable, and then replaced with the temporary variable (type name) expression, and that the value of the expression after the sentence is run does not change.

Casts are cast when a function prototype exists, for example: Double sqrt (double), sqrt ((double) 2) equivalent to sqrt (2)


C Language Conversion rules:

(1) integers and floating-point types:

When a floating-point type is converted to an integer, the fractional part is omitted and its behavior is undefined if its value cannot be represented by an integral type. Note: Converting a negative floating-point type to a unsigned int result is undefined.

When converting an integral type to a floating-point type, the result may be the next higher or lower possible value if the value is within a range that can be represented.

If the result is outside the range, the behavior is undefined.

(2) floating point type:

When converting a lower intensive-reading float to a higher floating-point type: The value is the same.

When a higher precision is converted to a lower precision: In the range, the value is constant, outside the range, the result is defined

(3) Integral type lifting:

If the original type can be represented by int, it is converted to int, otherwise it is converted to unsigned int

(4) Integral type conversion:

When converting unsigned to signed, if it can be represented in a new type, it means that if it cannot be represented, the result is related to the specific implementation





This article is from the "Useless Uncle" blog, please be sure to keep this source http://aslonely.blog.51cto.com/6552465/1625854

C Type Conversion detailed

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.