Basic rules for C-language data type conversion

Source: Internet
Author: User
Tags function prototype

program statements and expressions should typically use only one type of variable and constant, but if you use a mixed type,

C uses a collection of rules to accomplish automatic conversion of data types.

Here are some basic rules for C data type conversions :

1. When appearing in an expression, both signed and unsigned char and short types are automatically converted to int.

2. In any operation that contains two data types, the lower-level type is converted to another higher-level data type in the operation.

3. The data type level is from high to low in the order of long double, double, float, usigned long long, long long unsigned long, long, unsigned int, One possible exception to int is that when long and int have the same size, the unsigned int level is higher than long,short and Char is promoted to int by rule 1.

4. In an assignment statement, the result of the calculation is converted to the type of the variable to be assigned, which can result in a level increase (the type level of the assigned value is high) or degraded (the type level of the assigned value is low), and the promotion is usually a smooth, lossless process, but the demotion can lead to a real problem.

    

5. When a parameter is passed as a function, char and short are converted to int,float to double, but the automatic promotion can be prevented by specifying the function prototype.

    

Reference: C Primer Plus

Basic rules for C-language data type conversion

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.