Type implicit conversions in C-language expressions

Source: Internet
Author: User
Take a look at the following procedure, this program is from the "C expert programming":

#include <stdio.h>int array[] = {23,34,12,17,204,99,16}; #define Total_elements (sizeof (array)/sizeof (Array[0]) ) int main (void) {    int d=-1,x;    /*........*/     if (d <= totaltotal_elements-2)        x = array[d+1];    /*........*/    return 0;}

If there is a program like this, you never know what the value of x is, because this assignment statement x=array[d+1]; it will not execute at all. What's the reason? After the debug Discovery program executes until the IF statement is judged, the execution of the following statement is skipped directly. Here's why, because sizeof calculates the type size of the return value is the unsigned int type, and D is the signed INT,IF statement test both sizes, D automatically upgrades to unsigned int,-1 converted to unsigned int is a large positive integer, so the value of the expression is always false, so subsequent assignment statements are never executed. This is the type of conversion caused by the bug, if a little attention, it may have an unpredictable effect on the entire project or project, and this bug is difficult to debug directly can be done.

Type conversion in an expression

Type conversions include forced-type conversions and implicit conversions, which are all discussed implicitly. Let's take a look at the rules for implicit type conversions in traditional C (k&r C):

The first operand of any char, short int type will be converted to type int, and any float type will be converted to double type. If one operand is of type double, the other operand is also converted to double, and the result is double; if one operand is long, the other operand is also converted to long, and the result is a long If one operand is unsigned, the other operand is also converted to unsigned, which evaluates to unsigned.

In the new standard, however, some changes have been made:

1. Integral upgrade: All Char,short int and bit segments are automatically converted to int or unsigned int first. If int can represent all values of the source type, it is converted to int, otherwise it is converted to unsigned int.

2. When evaluating the value of an expression, it is common to first convert a low type (a data type that can represent a small data range) to a high type, and then participate in the calculation. However, one thing to note here is that if float is present in an expression, it is not necessarily converted to a double type and then evaluated. If you have the following code:

float f1,f2;double d;f1 = d*f2;

If a single-precision calculation is used, the final result is the same as the double-precision result, then the F2 may not be converted. This is different from traditional C, but there are few compilers (VCs do not support it) to support this.

When an operand of an unsigned and signed type exists in an expression, if an operand is unsigned long int, the other operand is also converted to unsigned long int, and if one operand is a long int, the other operand is unsigned int. If a long int can represent a range of unsigned int, the other operand is converted to a long int, otherwise two operands are converted to unsigned long int; if one operand is unsigned int and the other operand is int, Then the other operand is converted to unsigned int.

Let's look at an example:

Suppose an int is 16 bits, and a long int is 32 bits.

So for -1l < 1U, because -1L is signed long int, and 1U is unsigned int, because signe long int can fully represent the range of unsigned int, 1U is converted to signed long int;

For -1l>1ul, because -1L is signed long int, and 1UL is a unsigned long int, -1l is converted to unsigned long int.

  • 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.