Integer promotion in C Language)

Source: Internet
Author: User
In K & r c, the integral promotion is defined:

"A character, a short integer, or an integer bit-field, all either signed or not, or an object of Enumeration type, may be used in an expression wherever an integer maybe used. if an int can represent all the values of the original type, then the value is converted to int; otherwise the value is converted to unsigned Int. this process is called integral promotion."

The above definitions are summarized as follows:

1 ). as long as an integer value is used in an expression, the variables of the type char or short int active integer fields (which can be signed or unsigned) and the objects of the enumeration type are used, can be placed in the position of this integer variable.
2) if the original type Value Field of the variable in 1) can be expressed by INT, the original value is converted to int; otherwise, it is converted to unsigned Int.

As a whole, the above two are converted into integral promotion)

The concept of integer increase is easy to confuse with normal arithmetic type conversion. One of the differences between the two is that the latter occurs when the types of the operands are inconsistent, and eventually the operands are converted to the same type. In arithmetic operations, even if the operands have the same type, integer increase may still occur.

For example:

Char A, B, C;
C = A + B;


In the above process, although the operands of the two operators "+" and "=" are all char type, there is an integer increase in the intermediate calculation process: For expression A + B, A and B are both char type. Therefore, after being upgraded to int type, the "+" operation is executed, and the calculation result (INT type) is assigned to C (char type ), implicit type conversion is also executed.

Is there a sense of unbelief? Me too, but the standard is the standard. The following code proves the process described above.

Printf ("the size of the result of A + B: % d", sizeof (a + B ));

After understanding the concept of integer upgrading, you should not be confused about the FAQ in the C language below.

"Why is the output of the printf (" % d ", sizeof ('A'); Statement 4? Isn't the char length 1 byte? "

In the last sentence, ansi c stipulates that if the compiler can ensure that the operation results are consistent, it can also omit the type escalation operation-this usually occurs when the expression has a constant operand.

I hope my article does not mislead anyone who reads it.

 

Related Article

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.