C-Language unusual type conversions (type promotion)

Source: Internet
Author: User

For a little bit of code:

printf ("%d",sizeof('A'));

Are you sure he's printing 1?

After running, the result is 4!

C-Type conversions are more common than most people think , and in expressions involving less than int and double, there is a possibility of a type conversion!

According to the promotion rule: it is converted from char to int. This feature is called type Promotion!

Char   c1,c2;...........c1=c1+c2;

The following table shows a list of common types of promotion:

char-------------------------> int

Enum (enum)-------------------------> int

unsigned char-------------------------> int

Short-------------------------> int

unsigned short-------------------------> int

Bit segment (bit-filed)-------------------------> int

Float-------------------------> Double

' Integer lift ' requires the abstract machine to promote each variable to an int length, then add two int and then crop the result. If the addition of two char does not result in an overflow exception,

Then the actual execution only needs to produce the result of the char type, you can omit the type promotion!

Similar:

In:

float F1,f2; Double d;f1=f2*d;

If the compiler determines that the result of the operation with float is the same as converting to double, you can also use float for multiplication!

The hermit type conversion is a kind of method in language, which originates from the idea of simplifying the original editor.

C-Language unusual type conversions (type promotion)

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.