The principle of integer automatic conversion in C language __c language

Source: Internet
Author: User
Tags arithmetic
1. Implicit conversion
    C is implicitly converted in the following four situations:
1. In arithmetic expressions, a low type can be converted to a high type.
2. In an assignment expression, the value of the right expression is automatically implicitly converted to the type of the left variable and assigned to him.
3. When the parameter is passed in the function call, the system implicitly converts the argument to the type of the formal parameter and assigns it to the formal parameter.
4. When a function has a return value, the system implicitly converts the return expression type to the return value type and assigns the value to the calling function.

2, an implicit conversion of arithmetic operations
In arithmetic operations, you first have the following type conversion rules:
1. characters must first be converted to integers (C language requires that character type data and integer data be generic).
2. The short type is converted to an int type (same as integral type).
3, float-type data in the operation are converted to double precision (double) type, to improve the accuracy of the operation (the same as the real type).
Second, there are the following rules.

When different types of data are manipulated, they should first be converted to the same data type and then manipulated, and the conversion rules are converted from low-level to advanced. The conversion rules are shown in the following illustration:





The problem of operation between signed and unsigned numbers
The following experiments are run in virual c++6 by using the
this question tests to see if the integer automatic conversion principle in C is understood , and some developers know very little about these things. All operands are automatically converted to unsigned types when there are both signed and unsigned types in the expression. Therefore, in this sense, unsigned number of operations priority is higher than the number of symbols, which should be frequently used in unsigned data types of embedded systems is very important. &NBSP
First Experiment, define a signed int data and unsigned int data, and then size comparison:
    unsigned int a=20;
    signed int b=-130;
A>b? or B>a. Experiments have shown that b>a, that is to say, -130>20, is why such a result occurs.
This is because in the C language operation, if you encounter an operation between an unsigned number and a signed number, the compiler automatically converts to an unsigned number for processing, so a=20,b=4294967166 is, of course, b>a.
to give another example:
 unsigned int a=20
 signed int b=-130;
 std::cout<<a+b<<std::endl ;
The result output is 4294967186, and in the same way, before the operation, the a=20,b is converted to 4294967166, so a+b=4294967186

Subtraction and multiplication are similar to the results of the operation.

If the b=-130,b of the signed int data does not affect the type of B when it is operated between the immediate number, the result of the operation is still the signed int:
signed int b=-130;
std::cout<<b+30<<std::endl;
Output is-100.

For floating-point numbers, floating point numbers (float,double) are actually signed numbers, and the unsigned and signed prefixes cannot be added to float and double, of course there is no question of the conversion between unsigned numbers of symbols.


View Plain #include  <iostream>  /*    All operands are automatically converted to unsigned types when there is a symbol type and an unsigned type in an expression   * /   using namespace std;   Char getchar (int x,int y) {       char c;       unsigned int a=x;        unsigned int b=a+y;        (a+y>10)? (c=1):(c=2);       return c;  }   void main () {    Char c1=getchar (7,4);   Char c2=getchar (7,3);   Char c3=getchar (7,-7);   

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.